Debug Macro in Word 2007

S

Sheila D

I have recorded a macro which inserts header & footer & address Autotext
entries but when I try to run it bombs on the first line & comes up with
error 'Run time error 5941 Requested member of the collection does not exist'
My autotext entries are CIMSHeader, CIMSFooter & CIMSAddress - I have no idea
what Plain Number 1 is referring to.....

I don't know anything about VB so no idea what the problem is and any help
much appreciated - code as follows

Sub CIMSInvoice()
'
' CIMSInvoice Macro
'
'
ActiveDocument.AttachedTemplate.BuildingBlockEntries("Plain Number 1"). _
Insert Where:=Selection.Range, RichText:=True
NormalTemplate.BuildingBlockEntries("CIMSHeader").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
NormalTemplate.BuildingBlockEntries("CIMSHeader").Insert
Where:=Selection. _
Range, RichText:=True
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
ActiveDocument.AttachedTemplate.BuildingBlockEntries("Plain Number 1"). _
Insert Where:=Selection.Range, RichText:=True
NormalTemplate.BuildingBlockEntries("CIMSFooter").Insert
Where:=Selection. _
Range, RichText:=True
WordBasic.ViewFooterOnly
NormalTemplate.BuildingBlockEntries("CIMSFooter").Insert
Where:=Selection. _
Range, RichText:=True
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
ActiveDocument.AttachedTemplate.BuildingBlockEntries("Plain Number 1"). _
Insert Where:=Selection.Range, RichText:=True
NormalTemplate.BuildingBlockEntries("CIMSAddress").Insert
Where:=Selection _
.Range, RichText:=True
NormalTemplate.BuildingBlockEntries("CIMSAddress").Insert
Where:=Selection _
.Range, RichText:=True
ActiveWindow.ActivePane.VerticalPercentScrolled = 0
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
Selection.MoveDown Unit:=wdLine, Count:=3
Selection.TypeBackspace
WordBasic.GoToFooter
Selection.MoveDown Unit:=wdLine, Count:=4
Selection.TypeBackspace
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub
 
G

Graham Mayor

Word 2007 stores autotext entries differently from earlier versions and the
macro recorder does not record entries that are stored in
buildingblocks.dotx. In fact the macro recorder makes a complete hash of
recording anything to do with autotext entries. You will have to modify the
code by hand

If your macro crashes on the line

ActiveDocument.AttachedTemplate.BuildingBlockEntries("Plain Number 1"). _
Insert Where:=Selection.Range, RichText:=True

then the entry is not stored in the document template where the line is
looking for it.

Either use the BuildingBlocks Organizer to move it to the document template
or
to normal.dot and then use

NormalTemplate.BuildingBlockEntries("Plain Number 1").Insert
Where:=Selection.Range, _
RichText:=True

As for the rest, I suspect

Sub CIMSInvoice()
With ActiveWindow
.ActivePane.View.Type = wdPrintView
.ActivePane.View.SeekView = wdSeekCurrentPageHeader
NormalTemplate.BuildingBlockEntries("CIMSHeader"). _
Insert Where:=Selection.Range, RichText:=True
.ActivePane.View.SeekView = wdSeekCurrentPageFooter
NormalTemplate.BuildingBlockEntries("CIMSFooter"). _
Insert Where:=Selection.Range, RichText:=True
.ActivePane.View.SeekView = wdSeekMainDocument
NormalTemplate.BuildingBlockEntries("Plain Number 1"). _
Insert Where:=Selection.Range, RichText:=True
NormalTemplate.BuildingBlockEntries("CIMSAddress"). _
Insert Where:=Selection.Range, RichText:=True
End With
End Sub

is closer to what you required.
 
G

Greg Maxey

As Graham has said, the recorder makes of a mash of AutoText. For the
following code to work you need to create the AutoText in the
"AutoText" gallery and save in the "Attached Template" (Normal.dotm if
you don't have a customized template)

If you place a bookmark named "Address" in the the place in the
document you want the address to appear then you can get more control
of the the Autotext placement:


Sub ScratchMacro()
Dim oRng As Word.Range
Dim oDoc As Word.Document
Set oDoc = ActiveDocument
Set oRng = oDoc.Sections(1).Headers(wdHeaderFooterPrimary).Range
oDoc.AttachedTemplate.AutoTextEntries("CMISHeader").Insert
Where:=oRng, RichText:=True
Set oRng = oDoc.Sections(1).Footers(wdHeaderFooterPrimary).Range
oDoc.AttachedTemplate.AutoTextEntries("CMISFooter").Insert
Where:=oRng, RichText:=True
Set oRng = oDoc.Bookmarks("Address").Range
oRng.Text = oDoc.AttachedTemplate.AutoTextEntries("CMISAddress")
oDoc.Bookmarks.Add "Address", oRng
End Sub
 
S

Sheila D

Thanks to you both for your help, I've now managed to get it working. It
seems 2007 does not cope to well with a few things we 'enjoyed' in the
earlier versions, maybe a service pack will come along soon!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top