Results 1 to 3 of 3
Thread: Macro error
2014-09-11, 06:51 #1
- Join Date
- Feb 2001
- Gouda, Netherlands
- Thanked 0 Times in 0 Posts
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:
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
Last edited by RetiredGeek; 2014-09-11 at 06:53. Reason: Added Code Tags
2014-09-11, 19:50 #2
- Join Date
- Jan 2001
- Melbourne, Victoria, Australia
- Thanked 266 Times in 243 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.
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 WithAndrew Lockton, Chrysalis Design, Melbourne Australia
2014-09-11, 20:20 #3
- Join Date
- May 2002
- Canberra, Australian Capital Territory, Australia
- Thanked 429 Times in 353 Posts
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 SubCheers,
[MS MVP - Word]