Set Focus

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
 
J

Jezebel

If you're working from VBA, better is to go directly to the range you want
to change:

Set MyDoc = Documents(x) 'x is the document number, or use
ActiveDocument
MyDoc.Sections(1).Headers(wdHeaderFooterPrimary).Range = "American
Woodworker"
 
M

Matthew

Jezebel,

This looks good. How do I know what the document number is? Can I assign
one when I create it?

Matthew
 
J

Jezebel

There are several ways to get a reference to a document.

Dim MyDoc as Word.Document

Set MyDoc = ActiveDocument - the document currently active
for the user
Set MyDoc = Documents.Add - Create a new document -- you can
specify which template
Set MyDoc = Documents.Open(FileName:= "Mydoc.doc") - Open an
existing document

The Documents() collection contains all open documents. You can refer to
them by number -- the most-recently opened/created document is Documents(1).
You can also refer to them by name. Experiment in the immediate window:

? Documents.Count, ? Documents(1).Name, etc
 
D

Doug Robbins - Word MVP - DELETE UPPERCASE CHARACT

Hi Matthew,

If this information is to go into the header of all documents created by the
mailmerge, why don't you just put it into the main document?

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
M

Matthew

Good thought.

However, I am generating the Mail Merge document in the macro each time.

Jezebel had the solution; I was just too sleepy to see it :)

Thanks for your time!

Matthew

"Doug Robbins - Word MVP - DELETE UPPERCASE CHARACTERS FROM EMAIL ADDRESS"
 
M

Matthew

Jezebel,

Thanks for your help. I just realized that I already had a reference to my
document in the macro.

Thanks for your help; it works correctly now.

Matthew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top