Results 1 to 13 of 13
  1. #1
    New Lounger
    Join Date
    May 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    In Office 2007 How to make a Macro for Page number in footer.

    I am trying to make a macro wich I can use in the footer of all my word documents.
    I have tried the following:
    VIEW → MACROS → MACRO1 → ENTER → INSERT → FOOTER → PAGENUMBER →
    BOTTOM OF PAGE → PAGE X OF Y → BOLD NUMBER 3 → CLOSE HEADER AND FOOTER → VIEW → MACRO → STOP RECORDING.

    After this recording the doc I am working on while I am making the Macro print in the footer the page number and amount of pages.

    The next time I try to run the macro it gives me an error:
    MS Visual Basic Run-Time error '5941' The requested member of the collection does not exist.

    Please can you help me. Thanks in advance for your efforts.

  2. #2
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    Willow Grove, Pennsylvania, USA
    Posts
    205
    Thanks
    4
    Thanked 49 Times in 40 Posts
    This kind of problem happens because the macro recorder is dumber than a box of rocks. (More on this subject at http://www.word.mvps.org/FAQs/Macros...ordedMacro.htm.) The code that results from the steps you listed is

    Code:
    Sub Macro1()
    '
    ' Macro1 Macro
    '
    '
        WordBasic.ViewFooterOnly
        ActiveDocument.AttachedTemplate.BuildingBlockEntries("Bold Numbers 3"). _
            Insert Where:=Selection.Range, RichText:=True
        ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
    End Sub
    But the "Bold Numbers 3" building block isn't in the ActiveDocument.AttachedTemplate (which is probably Normal.dotm). Instead, it's in the Building Blocks.dotx template, which is why you're told that the "requested member... doesn't exist".

    The next wrinkle is that Word doesn't load the Building Blocks.dotx template until you ask to insert a building block or to open the Building Blocks Organizer, in order to start up faster. In a macro, we can force the template to load by calling the command Templates.LoadBuildingBlocks. Once it's loaded, that template is usually given the first slot in the templates list, but you can never quite be sure. The cure for that is to have a function to run through the templates looking for one named "Building Blocks".

    Finally, it isn't necessary—in fact, it can be disruptive—to move the cursor into the footer in order to insert something there. The macro can insert the block into the footer without ever moving the cursor.

    With all that, the code you need is this (see http://www.gmayor.com/installing_macro.htm if needed):

    Code:
    Sub Macro2()
        Dim templateNum As Integer
        templateNum = GetBuildingBlocksTemplate
        If templateNum = 0 Then
            Templates.LoadBuildingBlocks
            templateNum = GetBuildingBlocksTemplate
        End If
        Templates(templateNum).BuildingBlockEntries("Bold Numbers 3").Insert _
            Where:=ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range, _
            RichText:=True
    End Sub
    
    Function GetBuildingBlocksTemplate() As Integer
        Dim i As Integer
        For i = 1 To Templates.Count
            If InStr(LCase(Templates(i).FullName), "building blocks") Then
                GetBuildingBlocksTemplate = i
                Exit Function
            End If
        Next
        ' not present -- return 0
        GetBuildingBlocksTemplate = 0
    End Function

  3. The Following User Says Thank You to jjfreedman For This Useful Post:

    Gary Frieder (2011-05-07)

  4. #3
    New Lounger
    Join Date
    May 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thank You to jjfreedman For This Useful Post:

    I have taken all the information from your reply and it does work perfectly in all the documents I tried.

    Thanks for making my life easier.
    Carottemi

  5. #4
    New Lounger
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes thank you jjfreedman! This was very helpful.
    I have 2 more questions I was hoping your could help with.

    1. Could you please tell me how to add the Filename field to the header footer info?
    When I try to record a macro it just generates the following code but doesn't put it in the header/footer.

    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
    "FILENAME ", PreserveFormatting:=True
    Selection.TypeText Text:=" - "

    2. How can I position the header 0.02" from the top? When I record a macro while doing this it doesn't do anything.

    Any help you could offer would be very much appreciated.

    Thanks!

  6. #5
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    Willow Grove, Pennsylvania, USA
    Posts
    205
    Thanks
    4
    Thanked 49 Times in 40 Posts
    Mr President,

    Before I can tell you how to modify the macro, you'll have to clarify a few things:

    - Exactly where do you want the FILENAME field to be placed? In the footer, along with the page number, or in the header? At the left margin, or somewhere else? Do you need the dash shown in your recorded code (the Text:=" - " part)?

    - What do you mean by "position the header 0.02" from the top"? Is that measured from the top of the page or from the top margin?

  7. #6
    New Lounger
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi jjfreedman,
    Thanks for the reply. I got lucky and stumbled on the answer but now I'm stuck with the last part of my macro.
    I need a macro that will save a word doc and a PDF doc to 2 different locations. Location 1 c:\pdf, location 2 c:\docs. The source file is just a .txt file that I open and format in Word 2007. After I open it I run the macro I created which formats it but I would also like the macro to save a PDF and a .docx file of the formatted doc.

    Whenever I try to record this it makes the file name absolute but I need the macro to save the file using the existing file name. Example, I need myfile.txt to be saved as myfile.pdf and myfile.docx BUT when I open the next .txt, format it, then try to save using the macro I need differentfile.txt to be saved as differentfile.pdf and differentfile.docx. The way my macro is now it always saves the file with the myfile.pdf/docx name instead of using the current open file name.

    I tried editing the activefile.saveas:="myfile.txt" to be activefile.saveas:=DocName but it didn't work. (that might not be the actual syntax from the macro. I'm typing this on my phone right now so I don't have the code with me. I figured you would probably know what I meant tho)

    I thought this would be the easy part but I'm stuck. Can you help?

  8. #7
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    Willow Grove, Pennsylvania, USA
    Posts
    205
    Thanks
    4
    Thanked 49 Times in 40 Posts
    Before you waste time reinventing the wheel, see if the add-in at http://www.gmayor.com/SaveInTwoPlacesAddIn.htm does what you want.

    If not, then please try to make your needs clearer. Give a real example of the full path and filenames of one complete set of text, document, and PDF. Also, post the code of just the document-saving section of your macro as it is now, so we can see how far you've gotten.
    Last edited by jjfreedman; 2012-03-11 at 22:30.

  9. #8
    New Lounger
    Join Date
    Jul 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi Mr.Freedman,

    I tried your code, but i face few issues in execution itself

    Templates(templateNum).BuildingBlockEntries("Bold Numbers 3").Insert _
    Where:=ActiveDocument.Sections(1).Footers(wdHeader FooterPrimary).Range, _
    RichText:=True

    In this part of code, i get the below error
    "Interface 'Microsoft.Office.Interop.Word.BuildingBlockEntrie s' cannot be indexed because it has no default property "

  10. #9
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Frankly, I have to wonder why you wouldn't simply add the required content to the template footer and be done with it. All documents based on the template would automatically have the required footer. If you want to have the footer in some, but not others, simple: use two templates. No macros needed.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  11. #10
    New Lounger
    Join Date
    Jul 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi,

    To be exact i am doing a word automation using VB 2008.

    Where i need to add a Table in the header and i need to update a column with Page Number.

    Code:
        MyWord = CreateObject("Word.Application")
                    MyWord.Visible = True
                    doc = MyWord.Documents.Open(TextBox3.Text)
                    Directory = System.IO.Path.GetDirectoryName(TextBox9.Text)
                    Variants = NumericUpDown1.Text
                    Dim Selection As Word.Selection
                    Selection = MyWord.Selection
                    MyWord.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekCurrentPageHeader
                    CurrentHeaderTable = MyWord.ActiveDocument.Tables.Add(Range:=Selection.Range, NumRows:=2, NumColumns:= _
                    4, DefaultTableBehavior:=Word.WdDefaultTableBehavior.wdWord9TableBehavior, AutoFitBehavior:= _
                    Word.WdAutoFitBehavior.wdAutoFitFixed)
                    Selection.MoveRight(Unit:=Word.WdUnits.wdCell, Count:=2)
    
                    MyWord.ActiveDocument.AttachedTemplate.BuildingBlockEntries("Bold Numbers"). _
                    Insert(Where:=Selection.Range, RichText:=True)
    But i am not successful in this.. , this is the reason why i recorded a macro from MS Word 2007 and tried adapt the syntax. But i am facing the same issue.

    Regards,
    Manikandan Sankar

  12. #11
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Is there a reason for not using a template or document that already has the required table & page # field? Be that as it may, try something based on:
    Code:
    MyWord = CreateObject("Word.Application")
                    MyWord.Visible = True
                    Doc = MyWord.Documents.Open(TextBox3.Text)
                    Directory = System.IO.Path.GetDirectoryName(TextBox9.Text)
                    Variants = NumericUpDown1.Text
                    With Doc.Sections.First.Headers(wdHeaderFooterPrimary)
                      Doc.Tables.Add Range:=.Range, NumRows:=2, NumColumns:=4, _
                        DefaultTableBehavior:=Word.WdDefaultTableBehavior.wdWord9TableBehavior, _
                        AutoFitBehavior:=Word.WdAutoFitBehavior.wdAutoFitFixed
                        .Range.Tables(1).Cell(Row:=2, Column:=4).Range.Text = " "
                      Doc.Fields.Add Range:=.Range.Tables(1).Cell(Row:=2, Column:=4).Range.Characters.First, _
                        Type:=wdFieldEmpty, Text:="PAGE", PreserveFormatting:=False
                    End With
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  13. The Following User Says Thank You to macropod For This Useful Post:

    smk1984_ae (2012-07-19)

  14. #12
    New Lounger
    Join Date
    Jul 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi Paul,

    It worked!!!..... Thank you soo much for enlightening me...

    Regards,
    Manikandan Sankar

  15. #13
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    Willow Grove, Pennsylvania, USA
    Posts
    205
    Thanks
    4
    Thanked 49 Times in 40 Posts
    Manikandan, of course Paul's solution is the better approach. I just want to answer your question about why your code failed.

    Automating Word from outside with VB or C# or another language has additional restrictions and requirements that VBA inside Word doesn't have. In this case, VBA can address a particular building block entry by using the short syntax .BuildingBlockEntries("<name>") or the long syntax .BuildingBlockEntries.Item("<name>"), but programs from the outside must use the .Item syntax. This is a result of the way the interop interface is defined.

Posting Permissions

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