Home > Mobile >  What could be causing text to exist outside of all Sections of a Word document for only one specific
What could be causing text to exist outside of all Sections of a Word document for only one specific

Time:11-05

We have a Word template that has been running fine for years, but recently a user encountered an error that prevented them from running the template correctly. The template still works for everyone else. The error message reported was:

Run-time error '5941': The Requested member of the collection does not exist.

After debugging the code on the user's machine, we found that the error was triggered at ActiveDocument.Sections(sectionNo) in the following VBA:

Dim sectionNo As Long
Selection.GoTo what:=wdGoToBookmark, Name:=myBookmarkName
sectionNo = Selection.Information(wdActiveEndSectionNumber)
ActiveDocument.Sections(sectionNo).Range.Delete

We confirmed that the string in myBookmarkName points to a valid bookmark, but we found that Selection.Information(wdActiveEndSectionNumber) returned -1. So for this one particular user, and no one else, the bookmark was not contained within a Section.

What could be causing text to exist outside of all Sections of a Word document for only one specific user?

CodePudding user response:

When Web Layout is selected in the View tab of the ribbon, the document no longer contains Sections. Therefore, only users with Web Layout selected will encounter the error.

To fix the issue in your VBA, first change the view type to Print View before processing the document to force Sections back into the document. Then, after processing, change the view type back so the user can still have their preferred view type selected. For example:

Dim userViewType As WdViewType
userViewType = ActiveWindow.View.Type
ActiveWindow.View.Type = wdPrintView

'code to process document goes here

ActiveWindow.View.Type = userViewType
  • Related