Full Version: Help with code and a word document
UtterAccess Forums > Microsoft® Access > Access Forms
attachments
One last question I guess...I don't want to automatically print this as the code calls for, but when I delete that line, it gives me an error. How can I change it to make it stay open on the print preview but not print automatically? My code is as follows (this is just a sample that I am playing with so only have one field):
Private Sub MergeButton_Click()
On Error GoTo MergeButton_Err
Dim objWord As Word.Application
' Copy the Photo control on the Employees form.
' Remove the following comment in Microsoft Access 97.
' DoCmd.RunCommand acCmdCopy
' Remove the following comment in Microsoft Access 7.0.
' DoCmd.DoMenuItem acFormBar, acEditMenu, acCopy, , acMenuVer70
' Start Microsoft Word 97.
Set objWord = CreateObject("Word.Application")
With objWord
' Make the application visible.
.Visible = True
' Open the document.
.Documents.Open ("c:\NEWTESTPAGE.doc")
' Move to each bookmark and insert text from the form.
.ActiveDocument.Bookmarks("CUSTOMERNAME").Select
.Selection.Text = (CStr(Forms!customer!CustomerName))
End With
' Print the document in the foreground so Microsoft Word 97
' will not close until the document finishes printing.
objWord.ActiveDocument.PrintOut Background:=False
' Close the document without saving changes.
objWord.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
' Quit Microsoft Word 97 and release the object variable.
objWord.Quit
Set objWord = Nothing
Exit Sub
MergeButton_Err:
' If a field on the form is empty
' remove the bookmark text and continue.
If Err.Number = 94 Then
objWord.Selection.Text = ""
Resume Next
' If the Photo field is empty.
ElseIf Err.Number = 2046 Then
MsgBox "Please add a photo to this record and try again."
Else
MsgBox Err.Number & vbCr & Err.Description
End If
Exit Sub
End Sub
strive4peace
Print Preview is:

objWord.ActiveDocument.PrintPreview

the problem, of course, is that you cannot pause your code for user input ... you would have to leave Word open and end it there

you could have another button that released the variables if you dimension them on a module level
~~~~~~~~~~

when you post code, please be sure to use the Code tags around it -- look below message pane for tag to click on before you paste your code -- then put your code between the opening and closing tags ...using the "code" tags above and below your codeblock makes it much easier to read and preserves the indents and, therefore, makes it easier for others understand

{code} then your code then {/code}

-- substitute [ ] for { }

couldn't put square brackets in here or my message would have turned into a codeblock!
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.