Home |
Search |
Today's Posts |
#1
|
|||
|
|||
Macro to run at time of print...
I'm trying to get a single word document to print out a number of pages with
a sequentially numbered field using a bookmark and implementing the below macro code that I found at this link: http://word.mvps.org/FAQs/MacrosVBA/...piesOf1Doc.htm 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. NumCopies = Val(InputBox(Message, Title, Default)) SerialNumber = System.PrivateProfileString("C:\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("C:\Settings.txt", "MacroSettings", _ "SerialNumber") = SerialNumber 'Recreate the bookmark ready for the next use. With ActiveDocument.Bookmarks .Add Name:="SerialNumber", Range:=Rng1 End With ActiveDocument.Save My problem is that when I add the code to either the FilePrint Command or by the method below, word freezes. Private Sub oApp_DocumentBeforePrint(ByVal Doc As Document, _ Cancel As Boolean) 'Your code here End Sub Am I going about this in the correct manner? Should I be creating a word template or document for the macro to run in? Im quite confused and any help would be great! Thanks. |
#2
|
|||
|
|||
OK after a bit of fiddling i've got some output problem is that it printed
no's 1 & 2 but won't go past 2 - im getting a runtime error and directed at this line : Set Rng1 = ActiveDocument.Bookmarks("SerialNumber").Range Also when I closed and reopened my document it told me that macros are disabled!! how do i reenable them?! Thanks again! |
#3
|
|||
|
|||
Further update - the error i'm getting is 'the requested member of the
collection does not exist' i've also noticed that when the document has printed once, the bookmark disappears - could this cause the error? I'm running it of a normal document, would a template help? thanks. "Chr1551" wrote: I'm trying to get a single word document to print out a number of pages with a sequentially numbered field using a bookmark and implementing the below macro code that I found at this link: http://word.mvps.org/FAQs/MacrosVBA/...piesOf1Doc.htm 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. NumCopies = Val(InputBox(Message, Title, Default)) SerialNumber = System.PrivateProfileString("C:\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("C:\Settings.txt", "MacroSettings", _ "SerialNumber") = SerialNumber 'Recreate the bookmark ready for the next use. With ActiveDocument.Bookmarks .Add Name:="SerialNumber", Range:=Rng1 End With ActiveDocument.Save My problem is that when I add the code to either the FilePrint Command or by the method below, word freezes. Private Sub oApp_DocumentBeforePrint(ByVal Doc As Document, _ Cancel As Boolean) 'Your code here End Sub Am I going about this in the correct manner? Should I be creating a word template or document for the macro to run in? Im quite confused and any help would be great! Thanks. |
#4
|
|||
|
|||
I just ran the code from the website and also the code from your post and it
works fine for me. The fact that the bookmark is deleted after the first document is printed is of no consequence as it is the Rnge1 that is being used as the location for the insertion of the numbers. I think the problem is your trying to invoke the macro by use of the FilePrint command. What is probably happening is that the macro is being fired again by the ActiveDocument.PrintOut command and at that point there would not be a bookmark to set the Rng1 to. I would not recommend that you invoke the macro in this way as if you did, it would run for all documents that you print. -- Hope this helps. Please reply to the newsgroup unless you wish to avail yourself of my services on a paid consulting basis. Doug Robbins - Word MVP "Chr1551" wrote in message ... Further update - the error i'm getting is 'the requested member of the collection does not exist' i've also noticed that when the document has printed once, the bookmark disappears - could this cause the error? I'm running it of a normal document, would a template help? thanks. "Chr1551" wrote: I'm trying to get a single word document to print out a number of pages with a sequentially numbered field using a bookmark and implementing the below macro code that I found at this link: http://word.mvps.org/FAQs/MacrosVBA/...piesOf1Doc.htm 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. NumCopies = Val(InputBox(Message, Title, Default)) SerialNumber = System.PrivateProfileString("C:\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("C:\Settings.txt", "MacroSettings", _ "SerialNumber") = SerialNumber 'Recreate the bookmark ready for the next use. With ActiveDocument.Bookmarks .Add Name:="SerialNumber", Range:=Rng1 End With ActiveDocument.Save My problem is that when I add the code to either the FilePrint Command or by the method below, word freezes. Private Sub oApp_DocumentBeforePrint(ByVal Doc As Document, _ Cancel As Boolean) 'Your code here End Sub Am I going about this in the correct manner? Should I be creating a word template or document for the macro to run in? Im quite confused and any help would be great! Thanks. |
#5
|
|||
|
|||
Thanks very much for getting back to me - would you be able to recommend an
alternative way to invoke the macro? Being a beginner and having spent the night searching for a solution - I still haven't been able to get this to work in the correct manner! Thanks again. "Doug Robbins" wrote: I just ran the code from the website and also the code from your post and it works fine for me. The fact that the bookmark is deleted after the first document is printed is of no consequence as it is the Rnge1 that is being used as the location for the insertion of the numbers. I think the problem is your trying to invoke the macro by use of the FilePrint command. What is probably happening is that the macro is being fired again by the ActiveDocument.PrintOut command and at that point there would not be a bookmark to set the Rng1 to. I would not recommend that you invoke the macro in this way as if you did, it would run for all documents that you print. -- Hope this helps. Please reply to the newsgroup unless you wish to avail yourself of my services on a paid consulting basis. Doug Robbins - Word MVP "Chr1551" wrote in message ... Further update - the error i'm getting is 'the requested member of the collection does not exist' i've also noticed that when the document has printed once, the bookmark disappears - could this cause the error? I'm running it of a normal document, would a template help? thanks. "Chr1551" wrote: I'm trying to get a single word document to print out a number of pages with a sequentially numbered field using a bookmark and implementing the below macro code that I found at this link: http://word.mvps.org/FAQs/MacrosVBA/...piesOf1Doc.htm 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. NumCopies = Val(InputBox(Message, Title, Default)) SerialNumber = System.PrivateProfileString("C:\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("C:\Settings.txt", "MacroSettings", _ "SerialNumber") = SerialNumber 'Recreate the bookmark ready for the next use. With ActiveDocument.Bookmarks .Add Name:="SerialNumber", Range:=Rng1 End With ActiveDocument.Save My problem is that when I add the code to either the FilePrint Command or by the method below, word freezes. Private Sub oApp_DocumentBeforePrint(ByVal Doc As Document, _ Cancel As Boolean) 'Your code here End Sub Am I going about this in the correct manner? Should I be creating a word template or document for the macro to run in? Im quite confused and any help would be great! Thanks. |
#6
|
|||
|
|||
Just give it some other name and invoke it via the ToolsMacro menu item.
-- Hope this helps. Please reply to the newsgroup unless you wish to avail yourself of my services on a paid consulting basis. Doug Robbins - Word MVP "Chr1551" wrote in message ... Thanks very much for getting back to me - would you be able to recommend an alternative way to invoke the macro? Being a beginner and having spent the night searching for a solution - I still haven't been able to get this to work in the correct manner! Thanks again. "Doug Robbins" wrote: I just ran the code from the website and also the code from your post and it works fine for me. The fact that the bookmark is deleted after the first document is printed is of no consequence as it is the Rnge1 that is being used as the location for the insertion of the numbers. I think the problem is your trying to invoke the macro by use of the FilePrint command. What is probably happening is that the macro is being fired again by the ActiveDocument.PrintOut command and at that point there would not be a bookmark to set the Rng1 to. I would not recommend that you invoke the macro in this way as if you did, it would run for all documents that you print. -- Hope this helps. Please reply to the newsgroup unless you wish to avail yourself of my services on a paid consulting basis. Doug Robbins - Word MVP "Chr1551" wrote in message ... Further update - the error i'm getting is 'the requested member of the collection does not exist' i've also noticed that when the document has printed once, the bookmark disappears - could this cause the error? I'm running it of a normal document, would a template help? thanks. "Chr1551" wrote: I'm trying to get a single word document to print out a number of pages with a sequentially numbered field using a bookmark and implementing the below macro code that I found at this link: http://word.mvps.org/FAQs/MacrosVBA/...piesOf1Doc.htm 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. NumCopies = Val(InputBox(Message, Title, Default)) SerialNumber = System.PrivateProfileString("C:\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("C:\Settings.txt", "MacroSettings", _ "SerialNumber") = SerialNumber 'Recreate the bookmark ready for the next use. With ActiveDocument.Bookmarks .Add Name:="SerialNumber", Range:=Rng1 End With ActiveDocument.Save My problem is that when I add the code to either the FilePrint Command or by the method below, word freezes. Private Sub oApp_DocumentBeforePrint(ByVal Doc As Document, _ Cancel As Boolean) 'Your code here End Sub Am I going about this in the correct manner? Should I be creating a word template or document for the macro to run in? Im quite confused and any help would be great! Thanks. |
Reply |
Thread Tools | |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Table in a Form | Tables | |||
Macro Button Won't Stay on Toolbar | Microsoft Word Help | |||
Print 3" x 5" cards, Word 2002 | New Users | |||
Possible bug when recording a Word Macro | Microsoft Word Help | |||
macro to enter data and print doc | Mailmerge |