Run-time error 4248, 4605, or 5941 when you try to use VBA to change properties of a document

Article translations Article translations
Article ID: 290934 - View products that this article applies to.
This article was previously published under Q290934
For a Microsoft Word 2000 version of this article, see 209160.
For a Microsoft Word 98 version of this article, see 182179.
For a Microsoft Word 97 version of this article, see 170892.
Expand all | Collapse all

SYMPTOMS

If you try to use Microsoft Visual Basic for Applications to change the properties of a document in Microsoft Word, you may receive one of the following error messages:
Run-time error '4248':

This command is not available because no document is open.
Run-time error '4605':

This method or property is not available because a document window is not active.
Run-time error '5941':

The requested member of the collection does not exist.

CAUSE

One of these error messages may appear when no documents are currently open, or the document that you are referencing is not open. Word can change only the properties of an open (or visible) document.

Note These error messages may also appear if you opened the document with the Visible property set to False.

WORKAROUND

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
For more information how to use the sample code that is included in this article, click the following article number to view the article in the Microsoft Knowledge Base:
290140 How to run the sample code for the Office XP programs from Knowledge Base articles
The following sample Visual Basic for Applications macros demonstrate how to change the value of the Title field in the Properties dialog box. The following sample also includes code to trap the error, in case there are no documents open, and to display a message:
Sub ChangeDocProperties()

   On Error GoTo ErrHandler
      ActiveDocument.BuiltInDocumentProperties("Title") = "My Title"
   Exit Sub

ErrHandler:
   If Err <> 0 Then

      ' Display an error message.
      MsgBox Err.Description

      'Clear the error.
      Err.Clear
      Resume Next

   End If

End Sub
				
The following sample macro includes code to do the following:
  • Trap the error, in case there are no documents open.
  • In the error trap, create a new document.
  • Resume execution at the line that caused the error.
Sub ChangeDocProperties()

   On Error GoTo ErrHandler
      ActiveDocument.BuiltInDocumentProperties("Title") = "My Title"
   Exit Sub

ErrHandler:
   If Err <> 0 Then

      'Add a document.
      Documents.Add

      'Clear the error.
      Err.Clear

      'Execute the code that caused the error.
      Resume

   End If

End Sub
				

REFERENCES

For more information about how to get help with Visual Basic for Applications, click the following article number to view the article in the Microsoft Knowledge Base:
305326 List of programming resources for Visual Basic for Applications

Properties

Article ID: 290934 - Last Review: January 31, 2007 - Revision: 4.2
APPLIES TO
  • Microsoft Office Word 2003
  • Microsoft Word 2002 Standard Edition
Keywords: 
kbcode kberrmsg kbmacroexample kbpending kbprb kbprogramming KB290934

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com