ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Creating a Word Document from Excel Data (https://www.excelbanter.com/excel-programming/309886-creating-word-document-excel-data.html)

Sheldon

Creating a Word Document from Excel Data
 
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

Dick Kusleika[_3_]

Creating a Word Document from Excel Data
 
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

--
Dick Kusleika
MVP - Excel
Excel Blog - Daily Dose of Excel
www.dicks-blog.com

"Sheldon" wrote in message
...
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





All times are GMT +1. The time now is 06:28 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com