Link to home
Start Free TrialLog in
Avatar of ALawrence007
ALawrence007

asked on

Word Automation inserting Page Numbers

Hi to all,

I created a Visual Basic .Net application using Visual Studio 2008. This application creates one word document from files in a selected folder and then numbers the pages. All is working just fine, but I am having issues numbering the pages correctly. The code below shows what I am doing to number the pages, but some pages are not correct. For instance: after page 4 comes page 6 and so on. Can anyone please let me know what I am doing wrong and if possible give me code to use the built in "Building Blocks" that comes with office 2007.

Thanks
Public Shared Sub InsertPageNumbers(ByVal myWordApplication As Word.Application)
            myWordApplication.ActiveDocument.Repaginate()
            
            Dim NumberOfSections As Integer = myWordApplication.ActiveDocument.Sections.Count
            Dim i As Integer = 1
            Do Until i = NumberOfSections
                Dim rng As Word.Range = myWordApplication.ActiveDocument.Sections(i).Footers _
                (Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range
                rng.Fields.Add(rng, , "NumPages", False)
                rng.Fields.Add(rng, , "        Page ", False)
                rng.InsertBefore(vbTab & "        Page ")
                rng.Collapse(Word.WdCollapseDirection.wdCollapseStart)
                i += 1
            Loop
        End Sub

Open in new window

Avatar of dlc110161
dlc110161
Flag of United States of America image

You'll find some good code samples here:  http://blogs.msdn.com/microsoft_office_word/archive/2009/02/03/managing-and-administrating-building-blocks.aspx

Hopefully that will help

Dawn Crosier-Bleuel
Word MVP
Avatar of ALawrence007
ALawrence007

ASKER

dlc110161,

Thanks for the reply. The link you posted is very helpful and I used the contents in that post, but I keep on getting the following error when I use that (or any building blocks) code.

'Microsoft.Office.Interop.Word.BuildingBlockTypes' cannot be indexed because it has no default property.

Do you maybe have an idea what might cause this?

Thanks


Do you have more than one set of building blocks in your building blocks folder? If so, you may need to loop until you find the proper one.

You might also want to post the code that you are using to see whether there is something that might stick out.

Dawn Crosier-Bleuel
Word MVP
Templates.LoadBuildingBlocks
 
 
i = 1
For Each atemp In Templates
    If atemp = "Building Blocks.dotx" Then Set strMasterTemplate = Templates(i)
    Debug.Print atemp
    i = i + 1
Next atemp

Open in new window

I just ran through Jodie's code and did not get any errors. So it's got to be something extra that you are doing in your code. I am using straight Word, and not .Net.

Dawn Crosier-Bleuel
Word MVP
Dawn,

Thanks for the reply. The only thing I can think of is the fact that all the documents are located on a file server on a share. This file server has word 2007 installed. If that is not the issue, I included my code using building blocks below:

Public Shared Sub BuildingPageNumbers(ByVal myWordApplication As Word.Application)
myWordApplication.ActiveDocument.Repaginate()
'myWordApplication.Templates.LoadBuildingBlocks()
'With myWordApplication
'    .ActiveDocument.AttachedTemplate.BuildingBlockEntries("Plain Number 2").Insert _
'    (myWordApplication.Selection.Range, True)
'    .Selection.TypeText("Page ")
'    .ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekMainDocument
'End With
 
'MSDN Code
Dim wordDocument As Word.Document = Nothing
Dim wordTemplate As Word.Template = Nothing
Dim wordBuildingBlock As Word.BuildingBlock
Dim paramBBType As Word.WdBuildingBlockTypes = Word.WdBuildingBlockTypes.wdTypePageNumberBottom
Dim paramBBCategory As String = "Simple"
Dim paramBBName As String = "Plain Number 2"
myWordApplication.Templates.LoadBuildingBlocks()
myWordApplication.Templates.LoadBuildingBlocks()
wordTemplate = myWordApplication.Templates(1)
Dim TemplateName As String = wordTemplate.Name
wordBuildingBlock = _
wordTemplate.BuildingBlockTypes.Item(paramBBType) _
.Categories.Item(paramBBCategory).BuildingBlocks.Item(paramBBName)
wordBuildingBlock.Insert(wordDocument.Sections(1) _
.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range)
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ALawrence007
ALawrence007

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Very Cool! Thanks for sharing that. I really need to get with it with XML :)

Dawn Crosier-Bleuel
Word MVP