Results 1 to 3 of 3

Thread: Macro error

  1. #1
    New Lounger
    Join Date
    Feb 2001
    Location
    Gouda, Netherlands
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Macro error

    I have a very annoying problem with a macro I have been using for years with earlier versions of Word. At my work, I now use Word 2007 xp3 and Windows 7.
    What I want to do is:
    1. Insert a header based on the article name
    2. Add a hyphen
    3. Add a page number.
    1 en 2 work perfect, but then the macro stops. I can then add the page number and after that close the header, but this is annoying as I use this macro a couple of times a day.
    The macro-editor highlights where things go wrong: (“Alleen nummer” in Dutch means “only number”) (See also attached picture)

    The full macro:
    Code:
    If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
            ActiveWindow.Panes(2).Close
        End If
        If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
            ActivePane.View.Type = wdOutlineView Then
            ActiveWindow.ActivePane.View.Type = wdPrintView
        End IfActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
        Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
            "FILENAME  ", PreserveFormatting:=True
        Selection.TypeText Text:="  --  "
    
    AND THEN THESE TWO LINES ARE HIGHLIGHTIED IN THE MACRO EDITOR:
        ActiveDocument.AttachedTemplate.BuildingBlockEntries("Alleen nummer"). _
            Insert Where:=Selection.Range, RichText:=True
    
    
    If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
            ActiveWindow.Panes(2).Close
        End If
     If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
            ActivePane.View.Type = wdOutlineView Then
            ActiveWindow.ActivePane.View.Type = wdPrintView
        End If
        ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
        ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
    End Sub
    Any suggestions, please?
    Attached Images Attached Images
    Last edited by RetiredGeek; 2014-09-11 at 06:53. Reason: Added Code Tags

  2. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    I would be very surprised if you had .BuildingBlockEntries working in versions of Word prior to 2007 (when building blocks were introduced). Earlier versions of Word called these AutoTextEntries. Are you sure the template contains a building block called "Alleen nummer"?

    Since you are using a Building Block, I would recommend you save all the header contents as the building block instead of constructing the header on the fly. You can have the content adjust automatically to include things like the document title, author name, keywords etc by using Content Controls in your header. You can also be more specific with the name of the building block if you include the type and category information.

    Code:
      With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary)
        .Range.Delete
        ActiveDocument.AttachedTemplate.BuildingBlockTypes(wdTypeHeaders).Categories("MyCat").BuildingBlocks("MyHeader").Insert .Range
        .Range.Paragraphs(.Range.Paragraphs.Count).Range.Delete
      End With
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  3. #3
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Try:
    Code:
    Sub Demo()
    With Selection.Sections(1).Headers(wdHeaderFooterPrimary).Range
      .Fields.Add Range:=.Characters.Last, Type:=wdFieldEmpty, Text:="FILENAME", PreserveFormatting:=False
      .InsertAfter "  --  "
      .Fields.Add Range:=.Characters.Last, Type:=wdFieldEmpty, Text:="PAGE", PreserveFormatting:=False
    End With
    End Sub
    Cheers,

    Paul Edstein
    [MS MVP - Word]

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
  •