Printable Version of Topic

Click here to view this topic in its original format

UtterAccess Forums _ Microsoft Word _ Update Merge Field Depending On Entry In Text Box

Posted by: davideyles Apr 30 2020, 04:15 AM

Hi all
Might be going round this the wrong way but here goes

I have a document that I need to print with each consignment of animals I send. What I have tried to do is to automate the process by having the form filled out in word with auto updating of dates etc and have just added a macro to run on opening of the document ask for the number of animals.

ActiveDocument.FormFields("NumberOfLambs").Result = InputBox("Enter number of lambs")

This works fine, but what I would like to do is after the Text Box Field have the word lamb or lambs depending on the number on the form.

I have added a merge field with the IF function

IF NumberOfLambs>1,"Lambs","Lamb" \* MERGEFORMAT \* MERGEFORMAT

but I can't get it to change to 1 Lamb or 2 Lambs

What am I doing wrong?

David

Posted by: dashiellx2000 Apr 30 2020, 09:19 AM

You need to evaluate the inputbox return before setting the form's field.

Something like:

Dim x as integer

x = InputBox("Enter number of lambs")

If x = 1 then
ActiveDocument.FormFields("NumberOfLambs").Result = x & " lamb"
Else
ActiveDocument.FormFields("NumberOfLambs").Result = x & " lambs"
End if

hth

Posted by: davideyles Apr 30 2020, 09:56 AM

Ideal thumbup.gif . Thank you very much.


David

Posted by: davideyles Apr 30 2020, 10:17 AM

Hi
Just adding to the macro a little more, it is now like this.

Private Sub Document_Open()
Dim x As Integer

x = InputBox("Enter number of lambs")

If x = 1 Then
ActiveDocument.FormFields("NumberOfLambs").Result = x & " lamb"
Else
ActiveDocument.FormFields("NumberOfLambs").Result = x & " lambs"
End If

ActiveDocument.PrintOut

'Application.Quit SaveChanges:=wdDoNotSaveChanges


End Sub

When I remove the "apostrophe" on the last line (in red), the print (in blue) doesn't happen. Is that because the Application.Quit happens before the document has finished spooling? Do I need a delay of some sort?


David

Posted by: dashiellx2000 Apr 30 2020, 10:21 AM

To be honest, Word isn't my area of expertise, but I would suggest that you create a new post for that question since it is separate from your previous one. Someone will more knowledge about event timing in Work can help better.

Did the evaluation work for getting your form's field set correctly?

Posted by: davideyles Apr 30 2020, 11:02 AM

Found out it was due to background printing in Word


added

ActiveDocument.PrintOut Background:=False

and problem solved! guiness.gif

David