PDA

View Full Version : Header/footer Word macro



zonkd
05-11-2013, 08:54 AM
I'm hoping to use a header and footer on regular Word reports. This is what I have but there is at least one error here for above my message, I get - [Type text] Which I want to remove.

Very grateful for guidance, experts.
My macro is -

Sub AhHeadAnFeet()
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
MsgBox "We are running the new head and footers macro, gracias!"
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Application.Templates( _
"C:\Users\paul\AppData\Roaming\Microsoft\Document Building Blocks\1033\14\Built-In Building Blocks.dotx" _
).BuildingBlockEntries(" Blank").Insert Where:=Selection.Range, RichText _
:=False
Selection.TypeText Text:="weekly statistics | numbers for this week in May 2013 | "
Application.Templates( _
"C:\Users\paul\AppData\Roaming\Microsoft\Document Building Blocks\1033\14\Built-In Building Blocks.dotx" _
).BuildingBlockEntries("Plain Number").Insert Where:=Selection.Range, _
RichText:=True
Selection.TypeText Text:=" of "
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"NUMPAGES ", PreserveFormatting:=True
Selection.TypeText Text:=Chr(11) & _
"..........................................................."
Selection.TypeText Text:= _
"............................................................"
Selection.TypeText Text:="..................."
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Font.Name = "Bradley Hand ITC"
End Sub

macropod
05-11-2013, 11:21 PM
Why don't you create a template with the required content and formatting? No code required.

zonkd
05-12-2013, 01:00 AM
Thanks, Paul. Yes, a good idea, but isn't it too easy? Shouldn't I find out what's wrong with my VBA? I haven't used it for a few years, but thought I should get it going again, and macros seem to be a good place to start.

And this is a useful macro I could often use. The error must be quite simple, but I can't find it.

Thanks very much for the good alternative. Cheers paul

macropod
05-12-2013, 02:48 AM
a good idea, but isn't it too easy? Shouldn't I find out what's wrong with my VBA?
There's an old adage: who cares what the problem was when you've already got the answer.

FWIW:
Sub AhHeadAnFeet()
With ActiveDocument.Sections.First.Headers(wdHeaderFooterPrimary).Range
.Font.Name = "Bradley Hand ITC"
.Text = "weekly statistics | numbers for this week in May 2013 | " & vbTab & "Page "
.Fields.Add Range:=.Characters.Last, Type:=wdFieldEmpty, Text:="PAGE", Preserveformatting:=False
.InsertAfter " of "
.Fields.Add Range:=.Characters.Last, Type:=wdFieldEmpty, Text:="NUMPAGES", Preserveformatting:=False
.InsertAfter Chr(11) & "..........................................................." & _
"........................................................................... ...."
End With
End Sub

fumei
05-12-2013, 12:50 PM
Why on earth do you want to use code to put in a lot of spaces..................................................................... ........................................................................... ........................................................................... ........................................................................... .................................................................and why do you want to put in a lot of spaces at all?

macropod
05-12-2013, 02:26 PM
Hi Gerry,

They're periods ...

Maybe the zonkd is inputting them because he/she has never heard of tab stops with dot leaders.

fumei
05-12-2013, 03:39 PM
Ah. Ooops, I did not notice the string literals. In some ways it is even more odd.