OnFileOpen of Visual C++

OnCommand(ID_FILE_OPEN,OnFileOpen) function calls OnFileOpen() to handle events of ID_FILE_OPEN. But I am not sure how to access the filename that is read by OnFileOpen().
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Override CWinApp::OpenDocumentFile. OnFileOpen() calls this with the file name selected by the user:

virtual CDocument* OpenDocumentFile( LPCTSTR lpszFileName);

Good Luck,


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sakeeAuthor Commented:
hello steve,

can you provide a little more information than this? been trying in vain for some days already.


Your goal is to "access the filename that is read by OnFileOpen().". When all else fails, even before then really, I look at the MFC source code. It explains so much!

appdlg.cpp has the code for OnFileOpen:

void CWinApp::OnFileOpen()
  ASSERT(m_pDocManager != NULL);

docmgr.cpp has the above OnFileOpen():

void CDocManager::OnFileOpen()
  // prompt the user (with all document templates)
  CString newName;
  if (!DoPromptFileName(newName, AFX_IDS_OPENFILE,
    return; // open cancelled

  // if returns NULL, the user has already been alerted

You want the file name then override the OpenDocumentFile function on your CWinApp class. It is called from the last line of the above function (AfxGetApp()->OpenDocumentFile(newName):

  virtual CDocument* OpenDocumentFile(LPCTSTR lpszFileName);

That's how you access the filename read by OnFileOpen.

Maybe you really want to do something else? Let me know and I'll try to help.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.