Creating a Word Document from Excel Data

G

Guest

I have managed to create a work document containing all the formulas in a
selected worksheet. However, when I try in Excel VBA to add a footer to the
word document only part of the footer is created. The code I am using, which
I got from running a macro in Word is

.Selection.TypeText Text:="Page "
.Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldPage
.Selection.TypeText Text:=" of "
.Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldNumPages
.Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter

The code lines that start with ".Selection.Fields.Add Range:" does NOT
work.

Any suggestions???

Thanks
 
D

Dick Kusleika

Sheldon

I don't know a lot about this, but this seems to work

Sub makewdfooter()

Dim wd As Word.Application
Dim doc As Word.Document

Set wd = New Word.Application
Set doc = wd.Documents.Add

With doc.Sections(1).Footers(wdHeaderFooterPrimary)
wd.NormalTemplate.AutoTextEntries("Page X of Y").Insert .Range
End With

wd.Visible = True

End Sub
 

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