Called by the framework as part of the File Open command.

virtual BOOL OnOpenDocument( 
   LPCTSTR lpszPathName  


  • lpszPathName
    Points to the path of the document to be opened.

Return Value

Nonzero if the document was successfully loaded; otherwise 0.


The default implementation of this function opens the specified file, calls the DeleteContents member function to ensure that the document is empty, calls CObject::Serialize to read the file's contents, and then marks the document as clean. Override this function if you want to use something other than the archive mechanism or the file mechanism. For example, you might write an application where documents represent records in a database rather than separate files.

If the user chooses the File Open command in an SDI application, the framework uses this function to reinitialize the existing CDocument object, rather than creating a new one. If the user chooses File Open in an MDI application, the framework constructs a new CDocument object each time and then calls this function to initialize it. You must place your initialization code in this function instead of in the constructor for the File Open command to be effective in SDI applications.


The following examples illustrate alternative methods of initializing a document object.

// Method 1: In an MDI application, the simplest place to do  
// initialization is in the document constructor.  The framework  
// always creates a new document object for File New or File Open.
   // Do initialization of MDI document here.
// Method 2: In an SDI or MDI application, do all initialization  
// in an override of OnNewDocument, if you are certain that 
// the initialization is effectively saved upon File Save 
// and fully restored upon File Open, via serialization.
BOOL CMyDoc::OnNewDocument()
   if (!CDocument::OnNewDocument())
      return FALSE;

   // Do initialization of new document here. 

   return TRUE;
// Method 3: If the initialization of your document is not 
// effectively saved and restored by serialization (during File Save 
// and File Open), then implement the initialization in single 
// function (named InitMyDocument in this example).  Call the 
// shared initialization function from overrides of both 
// OnNewDocument and OnOpenDocument.
BOOL CExampleDoc::OnNewDocument()
   if (!CDocument::OnNewDocument())
      return FALSE;

   InitMyDocument(); // call your shared initialization function 

   // If your new document object requires additional initialization 
   // not necessary when the document is deserialized via File Open, 
   // then perform that additional initialization here. 

   return TRUE;
// Additional example of OnOpenDocument()
BOOL CExampleDoc::OnOpenDocument(LPCTSTR lpszPathName)
   if (!CDocument::OnOpenDocument(lpszPathName))
      return FALSE;

   InitMyDocument(); // call your shared initialization function 

   return TRUE;


Header: afxwin.h

See Also


CDocument Class

Hierarchy Chart







Other Resources

CDocument Members