JElster
asked on
Word 2007 - Macro - The requested member of the collection does not exist
Hi..
I recorded a macro that inserts a header - but get the following error
THE REQUESTED MEMBER OF THE COLLECTION DOES NOT EXIST
How to modify my macro to make it work on all documets? I've read about moving it to NORMAL.DOT but don't understand how or why this will help. thanks
Selection.MoveDown Unit:=wdLine, Count:=5
Selection.MoveLeft Unit:=wdCharacter, Count:=20
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Copy
Selection.MoveDown Unit:=wdLine, Count:=43
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveUp Unit:=wdLine, Count:=2
Selection.MoveLeft Unit:=wdCharacter, Count:=1
If ActiveWindow.View.SplitSpe cial <> wdPaneNone Then
ActiveWindow.Panes(2).Clos e
End If
If ActiveWindow.ActivePane.Vi ew.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.Vi ew.Type = wdPrintView
End If
ActiveWindow.ActivePane.Vi ew.SeekVie w = wdSeekCurrentPageHeader
** ERROR HERE
ActiveDocument.AttachedTem plate.Buil dingBlockE ntries(" Blank").Insert _
Where:=Selection.Range, RichText:=True
Selection.PasteAndFormat (wdPasteDefault)
ActiveDocument.AttachedTem plate.Buil dingBlockE ntries("Pl ain Number"). _
Insert Where:=Selection.Range, RichText:=True
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.TypeText Text:="Page "
ActiveWindow.ActivePane.Vi ew.SeekVie w = wdSeekMainDocument
I recorded a macro that inserts a header - but get the following error
THE REQUESTED MEMBER OF THE COLLECTION DOES NOT EXIST
How to modify my macro to make it work on all documets? I've read about moving it to NORMAL.DOT but don't understand how or why this will help. thanks
Selection.MoveDown Unit:=wdLine, Count:=5
Selection.MoveLeft Unit:=wdCharacter, Count:=20
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Copy
Selection.MoveDown Unit:=wdLine, Count:=43
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveUp Unit:=wdLine, Count:=2
Selection.MoveLeft Unit:=wdCharacter, Count:=1
If ActiveWindow.View.SplitSpe
ActiveWindow.Panes(2).Clos
End If
If ActiveWindow.ActivePane.Vi
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.Vi
End If
ActiveWindow.ActivePane.Vi
** ERROR HERE
ActiveDocument.AttachedTem
Where:=Selection.Range, RichText:=True
Selection.PasteAndFormat (wdPasteDefault)
ActiveDocument.AttachedTem
Insert Where:=Selection.Range, RichText:=True
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.TypeText Text:="Page "
ActiveWindow.ActivePane.Vi
ASKER
I removed the blank - no diff.
It can not find the following -
ActiveDocument.AttachedTem plate.Buil dingBlockE ntries("Bl ank").Inse rt _
Where:=Selection.Range, RichText:=True
It can not find the following -
ActiveDocument.AttachedTem
Where:=Selection.Range, RichText:=True
Yes. It means that there isn't a such an entry. BuildingBlockEntries are new to 2007, so I can't see if I have them.
I have tried to work out what your macro does. I thik this will do the same thing.
I have tried to work out what your macro does. I thik this will do the same thing.
Private Sub NumberinHeaders()
Dim rng As Range
Set rng = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Duplicate
rng.Text = "Page "
rng.Collapse wdCollapseEnd
ActiveDocument.Fields.Add rng, wdFieldPage
End Sub
ASKER
I'm trying to copy the first line in a document - The person it's addressed to - and add that name
to the header on the next pages and add the page #.
thanks
to the header on the next pages and add the page #.
thanks
ASKER
Hi.. This is what I have so far.. thanks
' Text to Copy to Header
Selection.MoveDown Unit:=wdLine, Count:=3
Selection.MoveLeft Unit:=wdCharacter, Count:=20, Extend:=wdExtend
Selection.Copy
Set sel = Selection
Selection.MoveDown Unit:=wdLine, Count:=65
' Create Header
Set rng = ActiveDocument.Sections(1) .Headers(w dHeaderFoo terPrimary ).Range.Du plicate
' ???
' Paste Selection and add carriage return and add 'Page' and number
' Add Para betweeen Selection and Page Number
Selection.PasteAndFormat (wdPasteDefault)
rng.Text = Paragraph & "Page "
'Close Header
rng.Collapse wdCollapseEnd
ActiveDocument.Fields.Add rng, wdFieldPage
' Text to Copy to Header
Selection.MoveDown Unit:=wdLine, Count:=3
Selection.MoveLeft Unit:=wdCharacter, Count:=20, Extend:=wdExtend
Selection.Copy
Set sel = Selection
Selection.MoveDown Unit:=wdLine, Count:=65
' Create Header
Set rng = ActiveDocument.Sections(1)
' ???
' Paste Selection and add carriage return and add 'Page' and number
' Add Para betweeen Selection and Page Number
Selection.PasteAndFormat (wdPasteDefault)
rng.Text = Paragraph & "Page "
'Close Header
rng.Collapse wdCollapseEnd
ActiveDocument.Fields.Add rng, wdFieldPage
I'm not sure it that works. If not, try this
Dim rng As Range
ActiveDocument.Range(0, 0).Select
Selection.MoveDown Unit:=wdLine, Count:=3
Selection.MoveLeft Unit:=wdCharacter, Count:=20, Extend:=wdExtend
Selection.Copy
Set rng = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Duplicate
' ???
' Paste Selection and add carriage return and add 'Page' and number
rng.PasteAndFormat (wdPasteDefault)
rng.Collapse wdCollapseEnd
' Add Para betweeen Selection and Page Number
rng.Text = vbCr & "Page "
rng.Collapse wdCollapseEnd
ActiveDocument.Fields.Add rng, wdFieldPage
ASKER
Works.. but the header is double spaced - so it show up like
George Washington
Page 2
how can I make is single spaced.. thanks again
George Washington
Page 2
how can I make is single spaced.. thanks again
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
If I remove it
I get
'George Washington Page 2'
any final ideas.. thanks again
I get
'George Washington Page 2'
any final ideas.. thanks again
ASKER
It appears the header is double spaced.. by default ?
How I programmaticall switch it to single
thanks
How I programmaticall switch it to single
thanks
Try setting the line spacing for the header to Single:
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Paragraphs.Space1
ASKER
Where do I add this?
thanks
thanks
I think that it would work anywhere in the macro, but, to be sure, put it at the end.
ASKER
It's doesn't seem to work... any more ideas.. thanks
ASKER
Thanks again
Perhaps it is SpaceBefore or SpaceAfter.
Try these at the end of the macro:
Try these at the end of the macro:
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Paragraphs.SpaceBefore = 0
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Paragraphs.SpaceAfter = 0
ASKER
That's it! Thanks!
That's good news. Thanks for letting me know. Next time that spacing question arises I'll try all three straight-away
Before 2007, recorded macros go in the Normal template by default. I guess that it is still the same.