R
ryguy7272
Mail Merge, Data Source, and UserForm with ControlButton
In my office, we use Mail Merge to ‘import’ data from an Excel file. This
works fine, using the mail Merge technique, but we are trying to modify the
process so that a user is able to run a macro, search through a list of items
(viewable through a TextBox embedded in a UserForm), and then click a
ControlButton to import all the appropriate information to each MergeField (I
think it would be a standard Mail Merge). It is easy to run through the
items using the standard Mail Merge toolbar, but we are trying to get this to
work through the process described above. It would be so much easier to get
certain ID numbers to show in the TextBox, select the ID that is of interest,
and import all data that pertains to this ID number directly into each
appropriate MergeField.
I am trying to modify some code that Doug Robbins gave me over the summer:
I have the following in the textbox:
***notice, one of the main problems is that I am no longer using a bookmark
named “Addressee†instead, I am trying to use about 30 different
MergeFields. This seems to be the obvious way to go with this, as they seem
to be more intuitive, and give more control over what is imported, at least
in terms of formatting, etc. I’ve named the Excel file “Source Data†and
I’ve named the range of data, and assigned the name “List†to that range. My
main question is how do I import all of this data, after identifying the
appropriate from the TextBox?
Private Sub CommandButton1_Click()
Dim i As Integer, Addressee As String
Addressee = ""
For i = 1 To ListBox1.ColumnCount
ListBox1.BoundColumn = i
Addressee = Addressee & ListBox1.Value & vbCr
Next i
ActiveDocument.Bookmarks("Addressee").Range.InsertAfter Addressee
UserForm1.Hide
End Sub
Sub UserForm_Initialize()
Dim i As Integer, Addressee As String
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim NoOfRecords As Long
' Open the database
Set db = OpenDatabase("C:\Word\Source Data.xls", False, False, "Excel
8.0")
' Retrieve the recordset
Set rs = db.OpenRecordset("SELECT * FROM `List`")
' Determine the number of retrieved records
With rs
.MoveLast
NoOfRecords = .RecordCount
.MoveFirst
End With
' Set the number of Columns = number of Fields in recordset
ListBox1.ColumnCount = rs.Fields.Count
' Load the ListBox with the retrieved records
ListBox1.Column = rs.GetRows(NoOfRecords)
'ListBox1.List = rs.GetRows(NoOfRecords) 'Transposed List
' Cleanup
rs.Close
db.Close
'AddressBlock = db
Set rs = Nothing
Set db = Nothing
End Sub
Sub Macro2()
ActiveDocument.MailMerge.MainDocumentType = wdFormLetters
ActiveDocument.MailMerge.OpenDataSource Name:= _
"C:\Word\ Source Data.xls", ConfirmConversions:=False, ReadOnly _
:=False, LinkToSource:=True, AddToRecentFiles:=False,
PasswordDocument:= _
"", PasswordTemplate:="", WritePasswordDocument:="", _
WritePasswordTemplate:="", Revert:=False, Format:=wdOpenFormatAuto, _
Connection:= _
"Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data
Source=C:\Word\Source Data.xls;Mode=Read;Extended
Properties=""HDR=YES;IMEX=1;"";Jet OLEDB:System database="""";Jet
OLEDB:Registry Path="""";Jet OLEDBatabase Password="""";Jet OLEDB:Engin" _
, SQLStatement:="SELECT * FROM `Source Data`", SQLStatement1:="",
SubType:= _
wdMergeSubTypeAccess
ActiveDocument.MailMerge.ViewMailMergeFieldCodes = wdToggle
End Sub
Sub UserFormInitialize()
UserForm1.Show
End Sub
Regards,
Ryan---
Oh, maybe I should mention this, some of the MergeFields are as follows:
«MERGEFIELD "Proj_Name"»
«MERGEFIELD "Product"»
Reference Number: «MERGEFIELD "ID_Number"»
***this ID number is the one of interest. I want to get this to load into
the TextBox, select it, click the ControlButton, and have all data related to
this ID number load into the word document. Any help would be SINCERELY
appreciated.
In my office, we use Mail Merge to ‘import’ data from an Excel file. This
works fine, using the mail Merge technique, but we are trying to modify the
process so that a user is able to run a macro, search through a list of items
(viewable through a TextBox embedded in a UserForm), and then click a
ControlButton to import all the appropriate information to each MergeField (I
think it would be a standard Mail Merge). It is easy to run through the
items using the standard Mail Merge toolbar, but we are trying to get this to
work through the process described above. It would be so much easier to get
certain ID numbers to show in the TextBox, select the ID that is of interest,
and import all data that pertains to this ID number directly into each
appropriate MergeField.
I am trying to modify some code that Doug Robbins gave me over the summer:
I have the following in the textbox:
***notice, one of the main problems is that I am no longer using a bookmark
named “Addressee†instead, I am trying to use about 30 different
MergeFields. This seems to be the obvious way to go with this, as they seem
to be more intuitive, and give more control over what is imported, at least
in terms of formatting, etc. I’ve named the Excel file “Source Data†and
I’ve named the range of data, and assigned the name “List†to that range. My
main question is how do I import all of this data, after identifying the
appropriate from the TextBox?
Private Sub CommandButton1_Click()
Dim i As Integer, Addressee As String
Addressee = ""
For i = 1 To ListBox1.ColumnCount
ListBox1.BoundColumn = i
Addressee = Addressee & ListBox1.Value & vbCr
Next i
ActiveDocument.Bookmarks("Addressee").Range.InsertAfter Addressee
UserForm1.Hide
End Sub
Sub UserForm_Initialize()
Dim i As Integer, Addressee As String
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim NoOfRecords As Long
' Open the database
Set db = OpenDatabase("C:\Word\Source Data.xls", False, False, "Excel
8.0")
' Retrieve the recordset
Set rs = db.OpenRecordset("SELECT * FROM `List`")
' Determine the number of retrieved records
With rs
.MoveLast
NoOfRecords = .RecordCount
.MoveFirst
End With
' Set the number of Columns = number of Fields in recordset
ListBox1.ColumnCount = rs.Fields.Count
' Load the ListBox with the retrieved records
ListBox1.Column = rs.GetRows(NoOfRecords)
'ListBox1.List = rs.GetRows(NoOfRecords) 'Transposed List
' Cleanup
rs.Close
db.Close
'AddressBlock = db
Set rs = Nothing
Set db = Nothing
End Sub
Sub Macro2()
ActiveDocument.MailMerge.MainDocumentType = wdFormLetters
ActiveDocument.MailMerge.OpenDataSource Name:= _
"C:\Word\ Source Data.xls", ConfirmConversions:=False, ReadOnly _
:=False, LinkToSource:=True, AddToRecentFiles:=False,
PasswordDocument:= _
"", PasswordTemplate:="", WritePasswordDocument:="", _
WritePasswordTemplate:="", Revert:=False, Format:=wdOpenFormatAuto, _
Connection:= _
"Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data
Source=C:\Word\Source Data.xls;Mode=Read;Extended
Properties=""HDR=YES;IMEX=1;"";Jet OLEDB:System database="""";Jet
OLEDB:Registry Path="""";Jet OLEDBatabase Password="""";Jet OLEDB:Engin" _
, SQLStatement:="SELECT * FROM `Source Data`", SQLStatement1:="",
SubType:= _
wdMergeSubTypeAccess
ActiveDocument.MailMerge.ViewMailMergeFieldCodes = wdToggle
End Sub
Sub UserFormInitialize()
UserForm1.Show
End Sub
Regards,
Ryan---
Oh, maybe I should mention this, some of the MergeFields are as follows:
«MERGEFIELD "Proj_Name"»
«MERGEFIELD "Product"»
Reference Number: «MERGEFIELD "ID_Number"»
***this ID number is the one of interest. I want to get this to load into
the TextBox, select it, click the ControlButton, and have all data related to
this ID number load into the word document. Any help would be SINCERELY
appreciated.