Reply
 
Thread Tools Display Modes
  #1   Report Post  
Chr1551
 
Posts: n/a
Default 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   Report Post  
Chr1551
 
Posts: n/a
Default

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   Report Post  
Chr1551
 
Posts: n/a
Default

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   Report Post  
Doug Robbins
 
Posts: n/a
Default

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   Report Post  
Chr1551
 
Posts: n/a
Default

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   Report Post  
Doug Robbins
 
Posts: n/a
Default

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

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Table in a Form HiDbLevel Tables 12 February 27th 06 12:59 PM
Macro Button Won't Stay on Toolbar caleb Microsoft Word Help 2 June 14th 05 11:59 PM
Print 3" x 5" cards, Word 2002 Jim E New Users 10 May 25th 05 04:23 AM
Possible bug when recording a Word Macro Raven95 Microsoft Word Help 4 April 30th 05 09:49 PM
macro to enter data and print doc David Gladstone Mailmerge 4 January 30th 05 10:41 AM


All times are GMT +1. The time now is 10:50 AM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"