Link to home
Start Free TrialLog in
Avatar of reichj
reichj

asked on

Re-open the open Document

I would like to open, and therefore re-initialize, the MFC document which is currently open. This is a SDI application.
The default behavior is to NOT reload a file (ie. de-serialize a document) if it is the current document. Selecting the file from either the MRU list or the Open Dialog only works when the selected file is not the current open document, weither dirty or not.
Is there some way to tell MFC to allow document re-loading? or extend the framework in some fashion (probably CWinApp or CDocManager)?
ASKER CERTIFIED SOLUTION
Avatar of wpinto
wpinto

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wpinto
wpinto

Now use CMySingleDocTemplate wherever you would use CSingleDocTemplate
Avatar of reichj

ASKER

Wilfred,

Thanks for the response.
Unfortunatly the code did'nt work perfectly at first. By the speed of your response I believe the code did work in the past, maybe on an older version of MFC (I'm using VC++ ver 5.0).
It seems CDocManager::OpenDocumentFile() (in docmgr.cpp) needs a little re-writing (I'll leave that up to microsoft). In any event 'match' is not tested, instead 'rpDocMatch' is tested against NULL to see if MatchDocType() has an already open doc. On top of that, setting 'match' to CDocTemplate::noAttempt forces a "Failed to open Doc" messagebox to appear.

According to the documentation :

If the file is not open but the extension in lpszPathName matches the extension specified by CDocTemplate::filterExt, this function returns CDocTemplate::yesAttemptNative and sets rpDocMatch to NULL

Thus, the body of CMySingleDocTemplate::MatchDocType() should be

Confidence MatchDocType(LPCTSTR lpszPathName, CDocument*& rpDocMatch)
{
      Confidence match;
      match = CSingleDocTemplate::MatchDocType(lpszPathName, rpDocMatch);

      // Fool calling function into believing that the
      // Doc is not open.
      if (match == CDocTemplate::yesAlreadyOpen){
            match = CDocTemplate::yesAttemptNative;
            rpDocMatch = NULL;
            POSITION p = GetFirstDocPosition();
            GetNextDoc(p)->SetModifiedFlag(FALSE);
      }
      return (match);
}

I also clear the Modified flag since it doesnt make much sense to get a SaveFile dialog prior to opening the same Doc. Of course just using the first doc position only works for an SDI app.

Thanks,
Jeff Reich