The more people use Word 2013, the more changes and problems are bubbling to the surface. Again, this one was brought to the world’s attention in the Word for Developers forum.

“Reading mode” was introduced a few years ago to provide a more efficient way to read documents on-screen. By default, documents opened from an e-mail attachment will display in this view. As the view is optimized for reading, most editing commands are not available while in that mode. This lock-out also applies to the object model. When code tries to make modifications to the document that aren’t allowed while in reading mode, you see run-time error 4605: “This method or property is not available because this command is not available for reading”.

Of course, we don’t expect to see this error when a document is opened in a different view, such as the default Print Layout. But that’s exactly what’s happening in Word 2013 when two documents are opened, the header/footer of the second is addressed and information copied from it into the header/footer of the first document, then a method is used that affects the entire first document, rather than a specific range. A somewhat obscure combination of circumstances, but nonetheless legitimate.

In effect, it appears that the second of the two document windows being opened is being tagged as open in Reading mode after the header/footer is addressed. This is a bug.

The workaround is to add a line of code that explicitly forces the second document window into Print Layout view.

Here are the repro steps for VBA. The workaround is the line commented out. Remove the apostrophe at the beginning of the line and the code should then work as expected, with no errors.
  1. Create a new document; save as source.docs
  2. Create a new document; save it as a template as template.dotx
  3. Run the following VBA code

Sub ReadModeError()
  Dim WDApp As Word.Application
  Set WDApp = Application

  Dim Doc As Word.Document
  Set Doc = WDApp.Documents.Add("c:\test\template.dotx")
  Dim HF As Word.Document
  Set HF = WDApp.Documents.Open("c:\test\source.docx")

  'HF.ActiveWindow.View = wdPrintView 'Workaround to avoid problem

  Doc.Sections.First.Headers(wdHeaderFooterPrimary).Range.FormattedText _
    = HF.Sections.First.Headers(wdHeaderFooterPrimary).Range.FormattedText
  Doc.Sections.First.Footers(wdHeaderFooterPrimary).Range.FormattedText _
    = HF.Sections.First.Footers(wdHeaderFooterPrimary).Range.FormattedText

  HF.Saved = True
  HF.Close

  Dim rng As Word.Range
  Set rng = Doc.Content
  rng.InsertBefore vbCrLf 'Throws the exception
  rng.Paragraphs.TabStops.ClearAll 'Also throws the exception
End Sub
Leave a Reply

*