Solved

Word 2007 - Macro - The requested member of the collection does not exist

Posted on 2008-10-31
18
1,372 Views
Last Modified: 2012-05-05
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.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

** ERROR HERE

    ActiveDocument.AttachedTemplate.BuildingBlockEntries(" Blank").Insert _
        Where:=Selection.Range, RichText:=True
       
    Selection.PasteAndFormat (wdPasteDefault)
    ActiveDocument.AttachedTemplate.BuildingBlockEntries("Plain Number"). _
        Insert Where:=Selection.Range, RichText:=True
   
    Selection.MoveLeft Unit:=wdCharacter, Count:=1
    Selection.TypeText Text:="Page "
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
0
Comment
Question by:JElster
  • 10
  • 8
18 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 22850337
There is a extra space before the word Blank in " Blank". Perhaps that is the problem.

Before 2007, recorded macros go in the Normal template by default. I guess that it is still the same.
0
 
LVL 1

Author Comment

by:JElster
ID: 22850396
I removed the blank - no diff.

It can not find the following -    
   ActiveDocument.AttachedTemplate.BuildingBlockEntries("Blank").Insert _
        Where:=Selection.Range, RichText:=True
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 22851312
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.
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

Open in new window

0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 1

Author Comment

by:JElster
ID: 22851649
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
0
 
LVL 1

Author Comment

by:JElster
ID: 22918149
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(wdHeaderFooterPrimary).Range.Duplicate
       
       
   ' ???
   ' 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

   
   
   
   
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 22918379
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

Open in new window

0
 
LVL 1

Author Comment

by:JElster
ID: 22918441
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
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 22919987
I can't be sure why it is double-spaced, but I would guess that the line containing the name already has a pargraph mark or a newline character to end it. In that cse we don't need to add on in the code.
 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)
 
   ' Add Page text and number to next paragraph
    rng.Collapse wdCollapseEnd
    rng.Text = "Page "
 
    rng.Collapse wdCollapseEnd
    ActiveDocument.Fields.Add rng, wdFieldPage

Open in new window

0
 
LVL 1

Author Comment

by:JElster
ID: 22921316
If I remove it

I get

'George Washington Page 2'

any final ideas.. thanks again
0
 
LVL 1

Author Comment

by:JElster
ID: 22921353
It appears the header is double spaced.. by default ?
How I programmaticall switch it to single
thanks
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 22921438
Try setting the line spacing for the header to Single:

    ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Paragraphs.Space1

Open in new window

0
 
LVL 1

Author Comment

by:JElster
ID: 22921876
Where do I add this?
thanks
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 22922893
I think that it would work anywhere in the macro, but, to be sure, put it at the end.
0
 
LVL 1

Author Comment

by:JElster
ID: 22924121
It's doesn't seem to work... any more ideas.. thanks
0
 
LVL 1

Author Closing Comment

by:JElster
ID: 31512019
Thanks again
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 22924309
Perhaps it is SpaceBefore or SpaceAfter.

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

Open in new window

0
 
LVL 1

Author Comment

by:JElster
ID: 22924377
That's it! Thanks!
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 22924600
That's good news. Thanks for letting me know. Next time that spacing question arises I'll try all three straight-away
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Batch convert .doc to .docx 13 271
VBA/SQL - Connect to SQL server and pull data 4 107
Word 2010 mail merge 3 79
Can I use VBA in Word to make a Table AutoFit to contents? 7 34
Like many others, we try and discourage users from printing documents unnecessarily and instead send or share them electronically. However, this doesn't always work and documents are still printed. With this simple solution, if the user tries to …
If you work with Word a lot, you probably use styles. If you use styles a lot, you've probably balled your fist more often than not when working with the ribbon. In Word 2007/2010, one of the things that I find missing when using styles is a quic…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now