M
Matthew
I am doing a mail merge, and wanted to put some info in the header.
I have the code to put the info in the header of the active window:
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.TypeText Text:="American Woodworker"
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
How can I apply this to make changes to the correct document? I don't know
how to set the focus.
Thanks!
Matthew
Mail merge macro:
Private Sub MailMerge()
Dim oApp As Word.Application
Dim oDoc As Word.Document
'Start a new document in Word
Set oApp = CreateObject("Word.Application")
Set oDoc = oApp.Documents.Add
With oDoc.MailMerge
'Insert the mail merge fields temporarily so that
'you can use the range containing the merge fields as a layout
'for your labels -- to use this as a layout, you can add it
'as an AutoText entry.
With .Fields
.Add oApp.Selection.Range, "FirstName"
oApp.Selection.TypeText (" ")
.Add oApp.Selection.Range, "LastName"
oApp.Selection.TypeParagraph
.Add oApp.Selection.Range, "Company"
oApp.Selection.TypeParagraph
.Add oApp.Selection.Range, "Address1"
oApp.Selection.TypeParagraph
.Add oApp.Selection.Range, "Address2"
oApp.Selection.TypeParagraph
.Add oApp.Selection.Range, "City"
oApp.Selection.TypeText ", "
.Add oApp.Selection.Range, "State"
oApp.Selection.TypeText " "
.Add oApp.Selection.Range, "ZipPostalCode"
End With
Dim oAutoText As Word.AutoTextEntry
Set oAutoText =
oApp.NormalTemplate.AutoTextEntries.Add("MyLabelLayout", oDoc.Content)
oDoc.Content.Delete 'Merge fields in document no longer needed now
'that the AutoText entry for the label layout
'has been added so delete it.
'Set up the mail merge type as mailing labels and use
'a tab-delimited text file as the data source.
.MainDocumentType = wdMailingLabels
.OpenDataSource Name:="C:\My Documents\Sales Leads\American
Woodworker\2003 12 24.csv" 'Specify your data source here
'Create the new document for the labels using the AutoText entry
'you added -- 5160 is the label number to use for this sample.
'You can specify the label number you want to use for the output
'in the Name argument.
oApp.MailingLabel.CreateNewDocument Name:="5160", Address:="", _
AutoText:="MyLabelLayout", LaserTray:=wdPrinterManualFeed
'Execute the mail merge to generate the labels.
.Destination = wdSendToNewDocument
.Execute
'Delete the AutoText entry you added
oAutoText.Delete
End With
'Close the original document and make Word visible so that
'the mail merge results are displayed
oDoc.Close False
oApp.Visible = True
'Prevent save to Normal template when user exits Word
oApp.NormalTemplate.Saved = True
End Sub
I have the code to put the info in the header of the active window:
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.TypeText Text:="American Woodworker"
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
How can I apply this to make changes to the correct document? I don't know
how to set the focus.
Thanks!
Matthew
Mail merge macro:
Private Sub MailMerge()
Dim oApp As Word.Application
Dim oDoc As Word.Document
'Start a new document in Word
Set oApp = CreateObject("Word.Application")
Set oDoc = oApp.Documents.Add
With oDoc.MailMerge
'Insert the mail merge fields temporarily so that
'you can use the range containing the merge fields as a layout
'for your labels -- to use this as a layout, you can add it
'as an AutoText entry.
With .Fields
.Add oApp.Selection.Range, "FirstName"
oApp.Selection.TypeText (" ")
.Add oApp.Selection.Range, "LastName"
oApp.Selection.TypeParagraph
.Add oApp.Selection.Range, "Company"
oApp.Selection.TypeParagraph
.Add oApp.Selection.Range, "Address1"
oApp.Selection.TypeParagraph
.Add oApp.Selection.Range, "Address2"
oApp.Selection.TypeParagraph
.Add oApp.Selection.Range, "City"
oApp.Selection.TypeText ", "
.Add oApp.Selection.Range, "State"
oApp.Selection.TypeText " "
.Add oApp.Selection.Range, "ZipPostalCode"
End With
Dim oAutoText As Word.AutoTextEntry
Set oAutoText =
oApp.NormalTemplate.AutoTextEntries.Add("MyLabelLayout", oDoc.Content)
oDoc.Content.Delete 'Merge fields in document no longer needed now
'that the AutoText entry for the label layout
'has been added so delete it.
'Set up the mail merge type as mailing labels and use
'a tab-delimited text file as the data source.
.MainDocumentType = wdMailingLabels
.OpenDataSource Name:="C:\My Documents\Sales Leads\American
Woodworker\2003 12 24.csv" 'Specify your data source here
'Create the new document for the labels using the AutoText entry
'you added -- 5160 is the label number to use for this sample.
'You can specify the label number you want to use for the output
'in the Name argument.
oApp.MailingLabel.CreateNewDocument Name:="5160", Address:="", _
AutoText:="MyLabelLayout", LaserTray:=wdPrinterManualFeed
'Execute the mail merge to generate the labels.
.Destination = wdSendToNewDocument
.Execute
'Delete the AutoText entry you added
oAutoText.Delete
End With
'Close the original document and make Word visible so that
'the mail merge results are displayed
oDoc.Close False
oApp.Visible = True
'Prevent save to Normal template when user exits Word
oApp.NormalTemplate.Saved = True
End Sub