• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 566
  • Last Modified:

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
0
ALawrence007
Asked:
ALawrence007
  • 3
  • 2
2 Solutions
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now