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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Macro in Word 97 2

Status
Not open for further replies.

Gill1978

Programmer
Jun 12, 2001
277
GB
Hi,

I'm very new to macro;s, and I need to create a macro that runs when a document is printed. The macro should increment a form field in a word document. However I haven't a clue how to do this.

So far the VBA for the macro looks like this:

Code:
Sub ReferenceCounter()
'
' ReferenceCounter Macro
' Macro created 23/08/05 by Any Authorised User
'
If ActiveDocument.FormFields("Text94").Result >= 1 Then ActiveDocument.FormFields("Text94") = ActiveDocument.FormFields("Text94").Result + 1
Else
ActiveDocument.FormFields("Text94") = 0

End Sub

Can anyone help?

Thanks in advance

Julie
 
what is the code doing that you do not want it to do?

What is the question?

Chris
 
Julie,

There is a DocumentBeforePrint event procedure that will do exactly what you are looking for. However, Application-level events require the use of a class module. Here are the steps:
[ul]
[li]From the VBE menu, Insert|Class Module. Rename this CApp (you can name it whatever you like but note where this name is used below ).[/li]
[li]Add the following code to the class module:
Code:
Public WithEvents WordApp As Word.Application

Private Sub WordApp_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)

  If Doc Is ThisDocument Then
    ReferenceCounter
  End If
 End Sub
[/li]
[li]In your standard code module (where your ReferenceCounter macro resides), at the top, place this declaration:
Code:
Public WD As New CApp
[/li]
[li]In the ThisDocument code module, add the following procedure:
Code:
Private Sub Document_Open()
  Set WD.WordApp = Word.Application
End Sub
[/li]
[/ul]

When the user prints the document, your ReferenceCounter procedure will run, incrementing the FormField.


Regards,
Mike
 
Nice explanation and instructions Mike! Tight and clean. Have a star.

Gerry
 
Hi,

I'm getting an error on the Class Module when I open the word document on the line Public WD As New CApp - Compile error: User defined type not defined.

Have I put the code in the wrong place (I haven't renamed the "CApp") is that it should I have used WordApp in the top line of the standard code module?

Thanks for the help

Julie
 
Ok ... so i've renamed the class to WordApp and that works fine. However the macro isn't launched at all ... the number in the field remains the same (i.e 1) ... i've added breakpoints and the code isn't stopped.

This is what i've done:
The class module named WordApp:
Code:
Public WithEvents WordApp As Word.Application

Private Sub WordApp_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)

  If Doc Is ThisDocument Then
    ReferenceCounter
  End If
 End Sub


The standard code module called NewMacros:

Code:
Public WD As New WordApp

Sub ReferenceCounter()
'
' ReferenceCounter Macro
' Macro created 23/08/05 by Any Authorised User
'
If ActiveDocument.FormFields("Text94").Result >= 1 Then ActiveDocument.FormFields("Text94") = ActiveDocument.FormFields("Text94").Result + 1
Else
ActiveDocument.FormFields("Text94") = 0

End Sub


In ThisDocument code module (under Microsoft Word Objects):

Code:
Private Sub Document_Open()
  Set WD.WordApp = Word.Application
End Sub

Can you see where i've gone wrong
 
Thanks Gerry.

Julie,

The issue is not the class name, and it may be a bit confusing giving this the same name as the variable. The error message you were getting leads me to believe that VBA is not seeing CApp (or WordApp) as a class name (therefore, it is interpreting it as a user-defined data type that hasn't been defined). Are you sure you have actually created a class module? Click on the module and look at the Properties window. You should see Module Name ClassModule where Module Name is CApp (or WordApp) in this case.

Otherwise, I'm at a loss since the code you posted looks OK. I have tested this successfully.

Regards,
Mike
 
Hi Julie,

Here's a couple of approaches that might help:

1. The following macro updates a bookmarked counter (instead of a formfield) with a new value. To use it, you would simply type the number '1' (or whatever other number you want as your starting number) into the document at the desired point, then bookmark it with the name 'Counter'. You would then call this macro from your 'DocumentBeforePrint' macro.

Sub UpdateCounter()
Dim BmkNm As String
Dim rngTemp As Range
BmkNm = "Counter"
With ActiveDocument.Bookmarks
If .Exists(BmkNm) Then
Set rngTemp = ActiveDocument.Bookmarks(BmkNm).Range
With rngTemp
.Text = .Text + 1
End With
.Add BmkNm, rngTemp
Else
MsgBox "Bookmark " & BmkNm & " not found."
End If
End With
Set rngTemp = Nothing
End Sub

If you need to reset the counter, simply change the bookmarked value to the required number.

2. Perhaps simplest of all, requiring no macros at all, is to create a counter field in the form of:
{Quote{SET Counter {=Counter+1}}{Counter}}
and make sure the 'update fields' option is checked under Tools|Options|Print. Note that the field braces '{}' are created in pairs via Ctrl-F9 - you can't simply paste/type them in from here. If you need to reset the counter to 1, simply go into Insert|Bookmark and delete the 'Counter' bookmark - it will automatically start at 1 next time it's updated. Alternatively, if you need to re-set the counter to some other value, create a 'SET' field, in the form of:
{SET Counter n}
where n is 1 less than the required number in an empty paragraph before the counter field, then select both fields and press F9, after which you should delete the empty para with the 'SET' field.

Cheers
 
Hi Mike ...

Clicking on the class ... the dropdown has WordApp ClassModule.

Could it have something to the how i've set the actual field and macro up?

Double clicking on the field
The Text Form Field Options dialog box has

"Text94" in the Bookmark field in the Field Settings grou[ box.

The type is Number (thats all the settings I set).

The macro name (ReferenceCounter) is attached to this one document named (E Checklist.doc)

Does that give you any clues ...??

Thanks again
Julie
 
Julie,

The problem is not in your handling of the formfield (I've set mine up like yours). I still believe the problem is in where the procedures are located. Let's take a step back; change the name of the class module back to CApp (as well as in the declaration of WD). This should cause the error you reported to re-appear. When we get this code working, that error will go away.

OK. All of the code I've presented should be part of the document file that contains ReferenceCounter (EChecklist.doc). All three code modules, NewMacros, ThisDocument and CApp should appear under
Project (EChecklist)
in the VBE Project Explorer. Is that the case?


Mike
 
I think i know why its because the word document is in Word 97 ... this can't handle the DocumentBeforePrint event can it???

 
Hi Mike

I made the changes and tried the document on a Windows 2000 machine and it works a treat ... however the users will all be on Office 97 ... is there Word 97 version of DocumentBeforePrint event?

what to do now?


 
Not sure. I don't have access to WD97 right now and online resources are miniscule. But, you can check this yourself:

Select the class module. In the left-hand dropdown of the code window select WordApp. In the right-hand dropdown you will find a listing of all available events.


Mike
 
I doubt it, but don't know for sure. Again you could check as above with the document running on a WD97 machine.
Assuming a similar event is not available, my suggestion would be to hook the Print menuitem and run ResourceCounter from a custom procedure.

Mike
 
Right just changed it to :

Code:
Sub FilePrint()
'
' FilePrint Macro
' Prints the active document
'
    Dialogs(wdDialogFilePrint).Show
Dim FieldVal As Integer

FieldVal = ActiveDocument.FormFields("Text94").Result

If ActiveDocument.FormFields("Text94").Result >= 0 Then
    ActiveDocument.FormFields("Text94").Result = ActiveDocument.FormFields("Text94").Result + 1
Else
    ActiveDocument.FormFields("Text94").Result = 0
End If

End Sub

Now all I need to do is autosave the doc once the counters gone up ...

Thanks for all that help ... was doing my nut in ...

Julie
 
Julie,

Did that work for you? I'm thinking it won't because when OK is selected in the print dialog you call, printing will commence, then the rest of your code will run (i.e., the counter increment). Also, if the user cancels, your increment code still runs; probably not what you want. Here is a modified version or FilePrint that should take care of this (also saves the document with the incremented counter):
Code:
Sub FilePrint()
Dim bResult As Boolean
Dim FieldVal As Integer

   bResult = Dialogs(wdDialogFilePrintSetup).Display
   If Not bResult Then Exit Sub

   FieldVal = ActiveDocument.FormFields("Text94").Result

   If ActiveDocument.FormFields("Text94").Result >= 0 Then
     ActiveDocument.FormFields("Text94").Result = ActiveDocument.FormFields("Text94").Result + 1
   Else
     ActiveDocument.FormFields("Text94").Result = 0
   End If
   With ActiveDocument
     .PrintOut
     .Save
   End With
   
End Sub

Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top