Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA Question

Status
Not open for further replies.

mary555

Programmer
Nov 9, 2005
185
CA
I need a form made in MS Word to automatically increment a particular field after being printed. For example, the form is 3 pages long. If the user presses 'print' and chooses to print 20 pages, I need the particular field on the first page to automatically increment by 1 for every printout. The user can add the 1st value but from there, it must increment each time the document is printed. Does anyone have any hints to do this?
 
If no one cal help with this, then can someone tell me how to simply update a cell in a table in MS Word
 

If you want to know what is printed you must intercept all print requests and control the whole print process from your own code.

I played with this some time ago and can't remember all the details but there are some 'funnies' when you try and Exceute the Print Dialog and then extract the user input from it, so it is probably best to create your own UserForm to gather print-related input and issue the print command using it, and then save the data you want.

If you want to save data in a document there are lots of options (Document Properties, Variables, etc.) but you will have to explicitly save the document when you've done it. This will mean that user changes will be saved as well, whether wanted or not; otherwise the user can quit without saving after you've done.

I'm not sure what you're asking about table cells. Something like
Code:
ActiveDocument.Tables(1).Range.Cells(1).Range.Text = [i]whatever[/i]

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
I am confused a little.
For example, the form is 3 pages long. If the user presses 'print' and chooses to print 20 pages, I need the particular field on the first page to automatically increment by 1 for every printout. The user can add the 1st value but from there, it must increment each time the document is printed.

1. The form is 3 pages long....how can the user print 20 pages?

2. The user can add WHAT value (whether it is "1st" or not)?

3. This "particular" field is....what? A formfield? A Control textbox? A regular Word field? What? Why would you want a field that is automatically incremented - presumably to have accurate data - that is also editable by the user? Say you have done your automatic increments and it states - this document has been printed 23 times. The user comes along and edits it to be 4. Of what possible use is theinformation if it can be changed freely?

In any case, Tony is right of course. To properly do this you must intercept all print commands and extract data. This can be done.

Gerry
 
What I meant was the form is 3 pages long but if the user goes to Print and chooses 20 copies then I guess it would be 60 pages in total. Each for (the first of the 3 pages) will have an ID field. If the user enters the number 6 in the ID field, then the first printout will have 6 in it, the 2nd printout will have 7 as ID, the 3rd printout will have 8 as ID...etc...
I can make this field anything, as long as it can have one single number value in it.
 
Please be very careful when posting. 20 pages - which is what you wrote - is very different from 20 copies - which is what you meant.

I am sure this can be done. I can think of a couple of ways. But let's run this through again, and sorry I gotta know these things because it drives me crazy if I don't...

Document is 3 pages long.
User sets Print for 20 copies.

WHY would they enter 6? Of what significance is the number? What does it mean? If it is user enter-able...what if they enter 459832? What if they enter 341faht? I could understand a field there that the users enters the number of copies they want. That is fine. You code takes that number and uses it (as a counter) to send to Print. Each copy is identified by an incremented number. Can be done. But I am trying to figure out what you really want.

Gerry
 
Just another thought. The key issue is:
automatically increment a particular field after being printed.
This is not accurate - if I understand correctly (which I may not...), You want to automatically increment a particular field WHILE a print job is in process. This you can not do. If you send a "make 20 copies" instruction off....then it will make 20 copies. The print instruction is spooled. You can not separate each copy and update soemthing IN the document.

However, you can in fact separate the print jobs. Here are a some ways to do this.

1. Make a field, and .Update it between print instructions.
2. Make a bookmark beside some text: "Print Copy: " X, and increment X.
3. Use a formfield. Here is possibly some starting code for using a formfield. I am going with this route as you seem to want to be able to have the user enter a number into something.....why I have no idea.
Code:
Sub IncrementMe()
Dim NumPrintCopies As Integer
Dim myCheck
Dim var
[COLOR=red]' if protected for forms, unprotect it
' use a proper password if there is one[/color red]
If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
    ActiveDocument.Unprotect Password:=""
End
[COLOR=red]' check that formfield content is in fact numeric[/color red]
If myCheck = IsNumeric(ActiveDocument.FormFields("text1").Result) Then
    NumPrintCopies = ActiveDocument.FormFields("text1").Result
End If
[COLOR=red]' print individual copies to the count entered
' resetting the formfield content for current copy number[/color red]
For var = 1 To NumPrintCopies
  ActiveDocument.FormFields("text1").Result = var
[COLOR=red]' you can likely get rid of some of these parameters[/color red]
     Application.PrintOut FileName:="", Range:=wdPrintAllDocument, Item:= _
     wdPrintDocumentContent, Copies:=1, Pages:="", PageType:=wdPrintAllPages, _
     ManualDuplexPrint:=False, Collate:=True, Background:=True, PrintToFile:= _
     False, PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
     PrintZoomPaperHeight:=0
Next
[COLOR=red]' final formfield content = var - the original amount
' if that is indeed what you want....which I doubt.
' it seems silly to me...however....
' reprotect the document[/color red]
ActiveDocument.Protect wdAllowOnlyFormFields, NoReset:=True, Password:=""
End Sub

Gerry
 
You need to place a form text control on your document. Note: You cannot place a form text control in the header or footer, so you would have to place it on each page where you want it to print. You could then, place this code in the close event of your document or you could call it from the close event and place it in a module:

Public Sub PrintFile()
Dim intStartNumber As Long
Dim intCopies As Long

intCopies = InputBox("Enter the number of copies that you wish to print", _
"Number of Copies", 0)
If intCopies = 0 Then Exit Sub

intStartNumber = InputBox("Enter the starting number for the first copy:", _
"Starting Number", 1)

Dim intCounter As Long

For intCounter = intStartNumber To intStartNumber + intCopies
ActiveDocument.FormFields("text1").Result = intCounter
Application.PrintOut
Next intCounter
End Sub


This assumes that you name your field "text1". Change the field name in the For loop to correspond to your field name if different.
 
TheComputerDude - Nice. The post actually asked for data entry into the field, but you got a nice way to get around that. I also like that used default numbers for the inputboxes.

Why are you using Long for the numbers?

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top