vba code for going into Word header

K

kurt

I'm writing a macro that uses the following code, but at this point I get an
error, and my macro does not allow going into the header.

It works perfectly on a PC at my home, but not on the PC at my office.

The error I get is: Object variable or WithBlock variable not set (Run time
error 91)

Any ideas?

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:=2
Selection.PasteAndFormat (wdPasteDefault)

ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

THANKS!

kurt
 
J

Jean-Guy Marcil

kurt was telling us:
kurt nous racontait que :
I'm writing a macro that uses the following code, but at this point I
get an error, and my macro does not allow going into the header.

It works perfectly on a PC at my home, but not on the PC at my office.

The error I get is: Object variable or WithBlock variable not set
(Run time error 91)

Any ideas?

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:=2
Selection.PasteAndFormat (wdPasteDefault)

ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

Looks like you are using the macro recorder...

This is fine to get an idea, but you should refine it by hand, see

for more on this.
http://word.mvps.org/FAQs/MacrosVBA/ModifyRecordedMacro.htm

Meanwhile, your macro could be simplified like this:
'_______________________________________
With Selection.Sections(1).Headers(wdHeaderFooterPrimary) _
.Range.Paragraphs(3).Range

.PasteAndFormat wdPasteDefault

End With
'_______________________________________

No Selection object and no need to "physically" go into the header.

You have to make sure the current header has at least three paragraphs and
you may want to change "wdHeaderFooterPrimary" to match your type of header.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
K

kurt

Bonjour Jean-Guy,

Merci pour votre aide!

Can you suggest a book or two for learning VBA in Word?

Merci mille fois.

Kurt
 

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