Reputation: 2791
I am currently using a macro to add an incrementing document number to the footer of a document. The problem is it uses a global "settings.txt" file.
I want to apply this macro to many documents and each much have its own record of the incrementing number.
I am not a pro a VBA/Macro writing so I do need some pretty clear instructions or copy and pasteable code.
Sub SerialNumber()
'
' SerialNumber Macro
'
'
Dim Message As String, Title As String, Default As String, NumCopies As Long
Dim Rng1 As Range
' Set prompt.
Message = "Enter the number of copies that you want to print"
' Set title.
Title = "Print"
' Set default.
Default = "1"
' Display message, title, and default value.
Dim SerialNumber As String
NumCopies = Val(InputBox(Message, Title, Default))
SerialNumber = System.PrivateProfileString("W:\settings.txt", _
"MacroSettings", "SerialNumber")
If SerialNumber = "" Then
SerialNumber = 1
End If
Set Rng1 = ActiveDocument.Bookmarks("SerialNumber").Range
Counter = 0
While Counter < NumCopies
Rng1.Delete
Rng1.Text = SerialNumber
ActiveDocument.PrintOut
SerialNumber = SerialNumber + 1
Counter = Counter + 1
Wend
'Save the next number back to the Settings.txt file ready for the next use.
System.PrivateProfileString("W:\settings.txt", "MacroSettings", _
"SerialNumber") = SerialNumber
'Recreate the bookmark ready for the next use.
With ActiveDocument.Bookmarks
.Add Name:="SerialNumber", Range:=Rng1
End With
End Sub
Question:
How can I convert this to save the "settings.txt" file in the same location as the document location automatically. (I'm not 100% sure but I think I have to create setting.txt manually, I'd like the creation to be automatic)
If I am going about this the wrong way, please let me know
My thought process would be:
If "document_name.txt" exists then do run macro
If "settings/document_name.txt" DOESN'T exists then do create document_name.txt in current open files location
and use current document name.
Note: I also opened a bounty on this question to have the macro run automatically after user hits print as per normal. Currently they have to manually run macro to print.
Running a macro before printing a word document
Upvotes: 0
Views: 1433
Reputation: 13505
In that case, you could use a macro like:
Sub FilePrint()
Dim i As Long, j As Long
With ActiveDocument
j = CLng(InputBox("How many copies to print?", "Print Copies"))
For i = 1 To j
With .CustomDocumentProperties("Counter")
.Value = .Value + 1
End With
.Fields.Update
.PrintOut
Next
.Save
End With
End Sub
coupled with a Custom Document Property named 'Counter', whose value you initially set to 0. Should you need to re-set the counter for some reason, simply edit the 'Counter' Custom Document Property accordingly.
Then, wherever you want the count to appear, add a DOCPROPERTY field that references the 'Counter' property. You can do this via Insert|Quick Parts|Field, or by pressing Ctrl-F9 to create a pair of field braces (ie '{}') and typing 'DOCPROPERTY Counter' between them, so that you end up with '{DOCPROPERTY Counter}'. You can use a numeric picture switch in the DOCPROPERTY field to format the number to have a particular lenght (e.g. 4 diigits). In that case the field code would appear as '{DOCPROPERTY Counter # 0000}'. When you're done, press F9 to update the field. You can then copy & paste the field to wherever else you might want also it to appear in your document.
If the documents you want this to apply to share a common template, but that template isn't used for other documents also, you could simply add the macro to that template; otherwise you might need to add it to each of the documents concerned and save them in the docm or doc format.
Upvotes: 2