The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,816 other followers

Some notes on Word automation and showing Bookmarks

Posted by jpluimers on 2014/08/27

Though I’ve done this automation in Delphi, this applies to automation from any development platform. In this particular project, the documents used Bookmarks. Those have changed over time, so has the Word support for it.

From Word 2000 until Word you could disable the showing of Bookmarks by setting the ShowBookmarks property to False like this:

<br />ActiveDocument.ActiveWindow.View.ShowBookmarks := False;<br />

Well, when doing this, Office 2013 can throw an error like this:

EOleException: The ShowBookmarks method or property is not available because this command is not available for reading
ErrorCode: 0x800A11FD
Source: Microsoft Word
HelpFile: wdmain11.chm

The full error message does not give any useful search results. But a partial search finds a Word 2013 issue towards the top of the results:

sometimes Words opens the document in Reading layout. Reading layout does not allow all operations in Word 2013.

If a document is protected, and you try to change something you should not, you get an error message like this:

This method or property is not available because the document is a protected document.

Usually, Cindy Meister is very accurate. However this time here code

...ActiveWindow.View = wdPrintView

should have been like

...ActiveWindow.View.Type = wdPrintView

Of course you also have to save/restore this property while you are enabling the ShowBookmarks property.

So you get Delphi code like this:

var
  CurrentView: View;
  OldViewType_: WdViewType;
begin
  CurrentView := ActiveDocument.ActiveWindow.View;
  try
    CurrentView.ShowBookmarks := False;
    // Word 2013 might raise a EOleException: The ShowBookmarks method or property is not available because this command is not available for reading
  except
    on E: EOleException do
    begin
      if E.ErrorCode = $800A11FD8 then
          begin
            OldViewType_ := CurrentView.type_;
            try
              CurrentView.type_ := wdPrintView;
              CurrentView.ShowBookmarks := False;
            finally
              CurrentView.type_ := OldViewType_;
            end;
          end
          else
            raise;
        end; // E: EOleException
      end;
    end;

This probably gives similar errors in other mistakes when you want to alter the document in Office 2013: basically the wdReadingView mode is readonly, the wdPrintView is read-write.

Another solution is for all your users to disable the Reading View mode in Word 2013.

Below is a table with the relevant objects, properties and method documentation links for the various Office versions (documentation for Office XP and Office 2000 is not available any more):

Office 2013 release. Microsoft Office 2010. Microsoft Office 2007. Microsoft Office 2003. Microsoft Office XP. Microsoft Office 2000. Remarks
Document Object (Word). Document Object (Word). Document Object [Word 2007 Developer Reference]. Document Object.
Document.ActiveWindow Property (Word). Document.ActiveWindow Property (Word). ActiveWindow Property [Word 2007 Developer Reference]. ActiveWindow Property.
Window Object (Word). Window Object (Word). Window Object [Word 2007 Developer Reference]. Window Object.
Window.View Property (Word). Window.View Property (Word). View Property [Word 2007 Developer Reference]. View Property.
View Object (Word). View Object (Word). View Object [Word 2007 Developer Reference]. View Object.
Window.View Property (Word). Window.View Property (Word). View Property [Word 2007 Developer Reference]. View Property.
View.ShowBookmarks Property (Word). View.ShowBookmarks Property (Word). ShowBookmarks Property [Word 2007 Developer Reference]. ShowBookmarks Property. True if square brackets are displayed at the beginning and end of each bookmark. Read/write Boolean.
View.Type Property (Word). View.View Property (Word). Type Property [Word 2007 Developer Reference]. View Property. WdViewType enumeration:

Name Value Description
wdMasterView 5 A master view.
wdNormalView 1 A normal view.
wdOutlineView 2 An outline view.
wdPrintPreview 4 A print preview view.
wdPrintView 3 A print view.
wdReadingView 7 A reading view.
wdWebView 6 A Web view.

wdConflictView=8 (documentation about it is very sparse)

–jeroen

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

 
%d bloggers like this: