Results 1 to 9 of 9

Thread: [RESOLVED] Port VBA to .NET (Word Automation)

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2011
    Posts
    10

    Resolved [RESOLVED] Port VBA to .NET (Word Automation)

    I am working on a program to automate MS Word from VB .NET. Most of the application is complete; however I need to automaticaly add the page number to the header. this is a section of my code.

    Macro Recorder VBA:
    Code:
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    ActiveDocument.AttachedTemplate.BuildingBlockEntries("Plain Number").Insert Where:=Selection.Range, RichText:=True
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
    My Code In VB .NET:
    Code:
    oWord.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekCurrentPageHeader
    oWord.ActiveDocument.AttachedTemplate.BuildingBlockEntries("Plain Number").Insert(Where:=oWord.Selection.Range, RichText:=True)
    oWord.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekMainDocument
    Im not sure if I need to load the BuildingBlocks from a template or how but the error I get is:
    The requested member of the collection does not exist. (I'm sure this is in reference to "Plain Number")

    Thank You For Your Help,
    SGT Larry G. Ransom III
    USA INF RET

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Port VBA to .NET (Word Automation)

    i would avoid using all references active anything

    try like
    vb Code:
    1. odoc = oword.documents("somedoc.doc")
    2. odoc.Sections(1).Headers(1).PageNumbers.Add   ' arguments here for alignment and start
    1 to 3 for header (normal, first page and odd pages)
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2011
    Posts
    10

    Re: Port VBA to .NET (Word Automation)

    This works Great!
    Thank You.
    Im not sure why I felt like I needed to load building blocks but i would be interested to know how to do it that way also, just in case there are other building blocks I need to load...

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Port VBA to .NET (Word Automation)

    Im not sure why I felt like I needed to load building block
    sorry can not help you as i have no idea about building blocks, try posting a question about building blocks in the dot net forum

    if this question has been solved, pls mark thread resolved (thread tools)
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2011
    Posts
    10

    Re: Port VBA to .NET (Word Automation)

    I will be sure to do that after my question about the building blocks is answered.

    Thank You,
    SGT Larry G. Ransom III
    USA INF RET

  6. #6

    Thread Starter
    New Member
    Join Date
    Jun 2011
    Posts
    10

    Re: Port VBA to .NET (Word Automation)

    I figured it out.
    If anyone else wants to know how to add building blocks to their Word Document, this is what I did:

    In Order To Find The Param Types And Names Do This:
    Open Microsoft Word
    In The Ribbon Click 'Insert' (Or Any Other Menu That Has Building Blocks In It...
    Select A Building Block From The Ribbon
    Under 'Headers & Footers' Click 'Page Number'
    Click 'Current Position'
    Right Click On One Of The Building Blocks And Click 'Edit Properties'
    There You Will See 'Name' and 'Category'
    Code:
        Sub InsertPageNumberBuildingBlock()
            Dim oTemplate As Template
            Dim oBuildingBlock As BuildingBlock
            
            'This Path Will Need To Be Changed Based On Operating System
            'Or You Can Loop Through The Templates, There Are Several Examples Posted On How To Do This
            Dim pBBPath As String = "C:\Program Files (x86)\Microsoft Office\Office12\Document Parts\1033\Building Blocks.dotx"
            Dim pBBType As WdBuildingBlockTypes = WdBuildingBlockTypes.wdTypePageNumber
            Dim pBBCategory As String = " Simple"
            Dim pBBName As String = "Plain Number"
    
            Try
                'Load Building Blocks 
                oWord.Templates.LoadBuildingBlocks()
                oTemplate = oWord.Templates(1)
    
                oBuildingBlock = oTemplate.BuildingBlockTypes.Item(pBBType).Categories.Item(pBBCategory).BuildingBlocks.Item(pBBName)
    
                'Go To Header And Insert Page Number
                oWord.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekPrimaryHeader
                oBuildingBlock.Insert(oDoc.Sections(1).Headers(WdHeaderFooterIndex.wdHeaderFooterPrimary).Range)
    
            Catch ex As Exception
                MsgBox(ex.Message, vbOKOnly, "error")
            Finally
                oBuildingBlock = Nothing
                oTemplate = Nothing
            End Try
    
            oWord.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekMainDocument
    
        End Sub

    Thank You,
    SGT Larry G. Ransom III
    USA INF RET

  7. #7
    New Member
    Join Date
    Jul 2012
    Posts
    3

    Re: [RESOLVED] Port VBA to .NET (Word Automation)

    Hi Larry,

    I have used the same code but i am getting few errors before debug itself...

    1. Dim MyBuildingBlock As BuildingBlock
    Error Type: Type Building block is not defined

    2. Dim pBBType As WdBuildingBlockTypes = WdBuildingBlockTypes.wdTypePageNumber
    Error Type: Type WdBuildingBlockTypes is not defined

    3. MyWord.Templates.LoadBuildingBlocks()
    Error Type: LoadBuildingBlocks is not a member of "Microsoft.Office.Interop.Word.Templates"

    4. MyBuildingBlock = MyTemplate.BuildingBlockTypes.Item(pBBType).Categories.Item(pBBCategory).BuildingBlocks.Item(pBBName)
    Error Type: BuildingBlockTypes is not a member of "Microsoft.Office.Interop.Word.Templates"

    I have also imported the below at the start of the Code.
    Imports Word = Microsoft.Office.Interop.Word
    Imports Microsoft.Office.Interop.Word

    Still i dont find the solution to Automate the Page Numbers in MS Word 2007

    Kindly Reply for this message... And suggest me what should i do ?

    Best Regards,
    Manikandan Sankar

  8. #8
    New Member
    Join Date
    Jul 2012
    Posts
    3

    Re: [RESOLVED] Port VBA to .NET (Word Automation)

    Hi Larry,

    I tried the below code... But its not working

    But i hope that, if this code is improvised it would solve the Purpose...

    Do you have any idea of what is happening in the below code?

    Code:
    MyWord.NormalTemplate.BuildingBlockEntries.Add(Name:="Plain Number 1", Type:=Word.WdBuildingBlockTypes.wdTypeCustomPageNumberTop, Category:=" Simple", Range:=Selection.Range)
    Regards,
    Manikandan Sankar

  9. #9
    New Member
    Join Date
    Jul 2012
    Posts
    3

    Thumbs up Re: [RESOLVED] Port VBA to .NET (Word Automation)

    Hi,

    I figured it out , by the following way...

    HTML Code:
    doc.Fields.Add(Range:=Selection.Range, Type:=Word.WdFieldType.wdFieldEmpty, Text:="PAGE", PreserveFormatting:=False)
    Instead trying to load the Template using Building Block Entries, i tried by using the document field add function itself... and it worked well

    Now i am pretty happy, i can move forward

    Regards,

    Manikandan Sankar

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width