M
MeWivFree
I wanted to add a button to a sub form that would automatically produce a
letter in Word using a combination of fields that are located on both the
main and sub forms.
I was trying to use this:
http://support.microsoft.com/Default.aspx?kbid=210271 as an example but it
seems to assume that all fields are on a single form and I think I am trying
to reference the fields I need from the sub form (actually located on a tab
control) incorrectly.
Can somebody point me in the right direction? (This is my first leap into
the world of VBA so you might have to speak slowly!!)
Thanks very much - code follows!
Ali
Private Sub LetterButton_Click()
On Error GoTo Err_LetterButton_Click
Dim objWord As Word.Application
'Start Microsoft Word.
Set objWord = CreateObject("Word.Application")
With objWord
'Make the application visible.
..Visible = True
'Open the document.
..Documents.Open ("C:\Printstation Contract Letter.dot")
'Move to each bookmark and insert text from the form.
..ActiveDocument.Bookmarks("Title").Select
..Selection.Text = (CStr(Forms!MainForm!Contacts!Title))
..ActiveDocument.Bookmarks("Forename").Select
..Selection.Text = (CStr(Forms!MainForm!Contacts!Forename))
..ActiveDocument.Bookmarks("Surname").Select
..Selection.Text = (CStr(Forms!MainForm!Contacts!Surname))
..ActiveDocument.Bookmarks("Address1").Select
..Selection.Text = (CStr(Forms!MainForm!Address1))
..ActiveDocument.Bookmarks("Address2").Select
..Selection.Text = (CStr(Forms!MainForm!Address2))
..ActiveDocument.Bookmarks("Address3").Select
..Selection.Text = (CStr(Forms!MainForm!Address3))
..ActiveDocument.Bookmarks("City").Select
..Selection.Text = (CStr(Forms!MainForm!City))
..ActiveDocument.Bookmarks("County").Select
..Selection.Text = (CStr(Forms!MainForm!County))
..ActiveDocument.Bookmarks("PostCode").Select
..Selection.Text = (CStr(Forms!MainForm!Postcode))
..ActiveDocument.Bookmarks("Dear").Select
..Selection.Text = (CStr(Forms!MainForm!Contacts!Dear))
End With
'Print the document in the foreground so Microsoft Word 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 and release the object variable.
objWord.Quit
Set objWord = Nothing
Exit_LetterButton_Click:
Exit Sub
Err_LetterButton_Click:
MsgBox Err.Description
Resume Exit_LetterButton_Click
End Sub
letter in Word using a combination of fields that are located on both the
main and sub forms.
I was trying to use this:
http://support.microsoft.com/Default.aspx?kbid=210271 as an example but it
seems to assume that all fields are on a single form and I think I am trying
to reference the fields I need from the sub form (actually located on a tab
control) incorrectly.
Can somebody point me in the right direction? (This is my first leap into
the world of VBA so you might have to speak slowly!!)
Thanks very much - code follows!
Ali
Private Sub LetterButton_Click()
On Error GoTo Err_LetterButton_Click
Dim objWord As Word.Application
'Start Microsoft Word.
Set objWord = CreateObject("Word.Application")
With objWord
'Make the application visible.
..Visible = True
'Open the document.
..Documents.Open ("C:\Printstation Contract Letter.dot")
'Move to each bookmark and insert text from the form.
..ActiveDocument.Bookmarks("Title").Select
..Selection.Text = (CStr(Forms!MainForm!Contacts!Title))
..ActiveDocument.Bookmarks("Forename").Select
..Selection.Text = (CStr(Forms!MainForm!Contacts!Forename))
..ActiveDocument.Bookmarks("Surname").Select
..Selection.Text = (CStr(Forms!MainForm!Contacts!Surname))
..ActiveDocument.Bookmarks("Address1").Select
..Selection.Text = (CStr(Forms!MainForm!Address1))
..ActiveDocument.Bookmarks("Address2").Select
..Selection.Text = (CStr(Forms!MainForm!Address2))
..ActiveDocument.Bookmarks("Address3").Select
..Selection.Text = (CStr(Forms!MainForm!Address3))
..ActiveDocument.Bookmarks("City").Select
..Selection.Text = (CStr(Forms!MainForm!City))
..ActiveDocument.Bookmarks("County").Select
..Selection.Text = (CStr(Forms!MainForm!County))
..ActiveDocument.Bookmarks("PostCode").Select
..Selection.Text = (CStr(Forms!MainForm!Postcode))
..ActiveDocument.Bookmarks("Dear").Select
..Selection.Text = (CStr(Forms!MainForm!Contacts!Dear))
End With
'Print the document in the foreground so Microsoft Word 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 and release the object variable.
objWord.Quit
Set objWord = Nothing
Exit_LetterButton_Click:
Exit Sub
Err_LetterButton_Click:
MsgBox Err.Description
Resume Exit_LetterButton_Click
End Sub