Visual Studio Tools for Office Add Page number to Word Doc

Hi to all,

I am trying to do what I thought would be easy, but it is not. I have a Visual Studio 2008 VB.Net application. I am using Microsoft Word 2007 and I am trying to use the word object model to insert a building block Page Number. There are no samples online that I can find that works. I am desperate!

All I want to do is to add page numbers to word documents. It HAS to be automated and that is why I am doing it this way.

Any help would be appreciated

Thanks
ALawrence007Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GrahamSkanRetiredCommented:
I don't use VB.net much, but I am currently downloading VS2008 Express to try to help you.

Meanwhile, I can tell you that you have to put a { PAGE } field where you need the page numbers to show, usually in a Header or Footer.

This VBA code adds a field into the last paragraph of the prime footer of the first section. Hopefully, you only have a prime footer and you only have one section, or that later sections are linked to the first.
    Dim objDoc As Word.Document
    Dim rng As Word.Range
    Dim para As Word.Paragraph
    
    '...
    Set rng = objDoc.Sections(1).Footers(wdHeaderFooterPrimary).Range
    Set para = rng.Paragraphs.Last
    objDoc.Fields.Add para.Range, wdFieldPage

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GrahamSkanRetiredCommented:
Hmm. There doesn't seem to be any way of creating a VSTO project, so I'll just have to stick to helping you understand the Word object model.
0
ALawrence007Author Commented:
GrahamSkan,
Your first post actually set me on the right path. I used your code and found some samples online. I modified the code a little and are now able to add page numbers. One small issue that I have. It is not adding it to the centre of the page. Maybe you can help me with that as well.
Thanks

wordApplication = New ApplicationClass()
            wordDocument = wordApplication.Documents.Open(paramDocPath)
            '...

            Dim section As Section
            For Each section In wordDocument.Sections

                section.Footers(WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Fields.Add( _
                section.Footers(WdHeaderFooterIndex.wdHeaderFooterPrimary).Range, _
                WdFieldType.wdFieldEmpty, "Page Num", True)

            Next

            wordDocument.Save()

Open in new window

0
GrahamSkanRetiredCommented:
There are two ways to centre - tab stops and paragraph alignment. If there is nothing else in the paragraph, the latter is easier.
objDoc.Sections(1).Footers(wdHeaderFooterPrimary).Range.ParagraphFormat.Alignment = wdAlignParagraphCenter

0
ALawrence007Author Commented:
Thanks Graham!
Added this line:
section.Footers(WdHeaderFooterIndex.wdHeaderFooterPrimary) _
                .Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.