J
Julia
Hi,
I have a template (for creating letters) that I have set up for my company.
I have all the employees names, direct dial, fax, email, etc. in a table in a
separate document. The letterhead template has been set up as merge file.
There is a form with a list box that the user uses to select their name.
After they select their name, their personal information from the table
should be inserted into the letterhead document.
The following macro works except, no matter which name I select from the
listbox, I get the last name in the list. Can anyone tell me what I'm doing
wrong??
---------------------------------------------
Private Sub Document_New()
Dim oDoc As Document
Dim ofrmLetterhead As frmLetterhead
Set ofrmLetterhead = New frmLetterhead
Set oDoc = ActiveDocument
Load frmLetterhead
frmLetterhead.lstNames.Clear
frmLetterhead.lstNames.SetFocus
DoEvents
oDoc.MailMerge.DataSource.ActiveRecord = wdFirstRecord
Do
frmLetterhead.lstNames.AddItem
oDoc.MailMerge.DataSource.DataFields("Name")
nCurrRecord = oDoc.MailMerge.DataSource.ActiveRecord
oDoc.MailMerge.DataSource.ActiveRecord = wdNextRecord
Loop Until nCurrRecord = oDoc.MailMerge.DataSource.ActiveRecord
frmLetterhead.lstNames.ListIndex = -1
frmLetterhead.Show
If btnOKClicked = True Then
ActiveDocument.Bookmarks("bkName").Range.Text =
oDoc.MailMerge.DataSource.DataFields("Name")
ActiveDocument.Bookmarks("bkDD").Range.Text =
oDoc.MailMerge.DataSource.DataFields("DD")
ActiveDocument.Bookmarks("bkEMail").Range.Text =
oDoc.MailMerge.DataSource.DataFields("Email")
ActiveDocument.Bookmarks("bkFax").Range.Text =
oDoc.MailMerge.DataSource.DataFields("Fax")
ActiveDocument.Bookmarks("bkSig").Range.Text =
oDoc.MailMerge.DataSource.DataFields("Sig")
Else
ActiveDocument.Close wdDoNotSaveChanges
End If
' Destroy the instance of the ofrmLetterhead object
Unload ofrmLetterhead
Set ofrmLetterhead = Nothing
Selection.GoTo What:=wdGoToBookmark, Name:="bkStop"
End With
End Sub
I have a template (for creating letters) that I have set up for my company.
I have all the employees names, direct dial, fax, email, etc. in a table in a
separate document. The letterhead template has been set up as merge file.
There is a form with a list box that the user uses to select their name.
After they select their name, their personal information from the table
should be inserted into the letterhead document.
The following macro works except, no matter which name I select from the
listbox, I get the last name in the list. Can anyone tell me what I'm doing
wrong??
---------------------------------------------
Private Sub Document_New()
Dim oDoc As Document
Dim ofrmLetterhead As frmLetterhead
Set ofrmLetterhead = New frmLetterhead
Set oDoc = ActiveDocument
Load frmLetterhead
frmLetterhead.lstNames.Clear
frmLetterhead.lstNames.SetFocus
DoEvents
oDoc.MailMerge.DataSource.ActiveRecord = wdFirstRecord
Do
frmLetterhead.lstNames.AddItem
oDoc.MailMerge.DataSource.DataFields("Name")
nCurrRecord = oDoc.MailMerge.DataSource.ActiveRecord
oDoc.MailMerge.DataSource.ActiveRecord = wdNextRecord
Loop Until nCurrRecord = oDoc.MailMerge.DataSource.ActiveRecord
frmLetterhead.lstNames.ListIndex = -1
frmLetterhead.Show
If btnOKClicked = True Then
ActiveDocument.Bookmarks("bkName").Range.Text =
oDoc.MailMerge.DataSource.DataFields("Name")
ActiveDocument.Bookmarks("bkDD").Range.Text =
oDoc.MailMerge.DataSource.DataFields("DD")
ActiveDocument.Bookmarks("bkEMail").Range.Text =
oDoc.MailMerge.DataSource.DataFields("Email")
ActiveDocument.Bookmarks("bkFax").Range.Text =
oDoc.MailMerge.DataSource.DataFields("Fax")
ActiveDocument.Bookmarks("bkSig").Range.Text =
oDoc.MailMerge.DataSource.DataFields("Sig")
Else
ActiveDocument.Close wdDoNotSaveChanges
End If
' Destroy the instance of the ofrmLetterhead object
Unload ofrmLetterhead
Set ofrmLetterhead = Nothing
Selection.GoTo What:=wdGoToBookmark, Name:="bkStop"
End With
End Sub