Quantcast
Channel: Planet Object Pascal
Viewing all articles
Browse latest Browse all 1725

The Wiert Corner - irregular stream of stuff: jpluimers

$
0
0

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:
NameValueDescription
wdMasterView5A master view.
wdNormalView1A normal view.
wdOutlineView2An outline view.
wdPrintPreview4A print preview view.
wdPrintView3A print view.
wdReadingView7A reading view.
wdWebView6A Web view.

wdConflictView=8 (documentation about it is very sparse)

–jeroen


Filed under: Delphi, Delphi XE2, Delphi XE3, Delphi XE4, Development, Office, Office 2000, Office 2003, Office 2007, Office 2010, Office 2013, Office Automation, Power User, Software Development, Word

Viewing all articles
Browse latest Browse all 1725

Trending Articles