Share via

Run-time Error "91"

Anonymous
2021-11-15T20:47:59+00:00

Get run-time error 91 "Object variable or With block variable not set" for the following code (running Word 2010):

Selection.GoTo What:=wdGoToSection, Which:=wdGoToFirst, Count:=3, Name:=""
With ActiveDocument.ActiveWindow.View
    .Type = wdPrintView
    .SeekView = wdSeekCurrentPageFooter
End With
Selection.HeaderFooter.Range.Select <---------------This line is highlighted when I select Debug in the error dialog
Selection.Collapse
Selection.TypeText "HERE"
Selection.Extend Character:="R"
Selection.Extend Character:="R"
Selection.Delete
Selection.Collapse
Selection.TypeText vbTab
Selection.TypeText vbTab

What am I doing wrong (that is, what do I need to do to correct this and get rid of the error)?

Thank you!

Microsoft 365 and Office | Word | For business | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

HansV 462.6K Reputation points
2021-11-17T22:08:50+00:00

Like this:

Dim rng As Range  
With ActiveDocument.Sections(3).Footers(wdHeaderFooterPrimary)  
    .Range.Text = "Preliminary" & vbTab & vbTab  
    Set rng = .Range  
End With  
rng.ParagraphFormat.Borders(wdBorderTop).LineStyle = wdLineStyleSingle  
rng.Collapse Direction:=wdCollapseEnd  
ActiveDocument.Fields.Add Range:=rng, Type:=wdFieldPage

Was this answer helpful?

0 comments No comments

Answer accepted by question author

HansV 462.6K Reputation points
2021-11-15T22:42:21+00:00

Try this:

Dim rng As Range  
With ActiveDocument.Sections(3).Footers(wdHeaderFooterPrimary)  
    .Range.Text = vbTab & vbTab  
    Set rng = .Range  
End With  
rng.Collapse Direction:=wdCollapseEnd  
ActiveDocument.Fields.Add Range:=rng, Type:=wdFieldPage

Was this answer helpful?

0 comments No comments

7 additional answers

Sort by: Most helpful
  1. Anonymous
    2021-11-15T22:28:25+00:00

    I prompt the user for some info and put it into various spots on a cover page (in section 1), then skip the table of contents page (in section 2) and go to section 3, where I select and delete a couple of paragraphs based on the user's previous choice. I then want to go into the footer for section 3, select some text (through the second "R"), delete it, and insert 2 tabs (to put the page number that's already there over to the far right (there's already a center tab stop and a right tab stop in the footer to begin with.) (I, too, had no problems with the code in a simple document with just 3 plain-vanilla sections. I am beginning to think Word hates me.) Thank you!

    Was this answer helpful?

    0 comments No comments
  2. Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
    2021-11-15T22:13:55+00:00

    What does that Header contain before you run the macro.

    Was this answer helpful?

    0 comments No comments
  3. HansV 462.6K Reputation points
    2021-11-15T21:06:07+00:00

    I tried the code and didn't get an error.

    But if you describe what you want to accomplish, it might be possible to help.

    Was this answer helpful?

    0 comments No comments