Click here to Skip to main content
Click here to Skip to main content

CDocument::DoSave revealed

, 18 Sep 2002 CPOL
Rate this:
Please Sign up or sign in to vote.
Explains how you can suppress the File-Save-As dialog in a Doc/View app, how to save files to multiple formats, and how DoSave is implemented.


For one of my recent projects I had a rather strange requirement. It was an image conversion program basically, and it allowed you to open bitmap files. When the user clicked on the save icon in the toolbar or if he took the save-item from the File-menu, I was to save the image in a custom format using the same name as the original bitmap, but replacing the bmp extension with a custom extension. This meant that I should suppress the File-Save-As dialog. Initially I thought I simply had to override OnSaveDocument and refrain from calling the base class, but I quickly discovered that OnSaveDocument was too late to suppress the File-Save-As dialog.

DoSave revealed

I took a look at doccore.cpp and soon figured out the order in which methods got called inside the CDocument class. Essentially when the user tries to save a file, MFC command routing routes the message to CDocument::OnFileSave or CDocument::OnFileSaveAs depending on whether you clicked on Save or on Save-As. CDocument::OnFileSave calls CDocument::DoFileSave(). CDocument::DoFileSave() checks to see if the file exists and if it does, it proceeds to call CDocument::DoSave passing the full path of the file, else it calls CDocument::DoSave passing NULL for the file path. CDocument::OnFileSaveAs simply calls CDocument::DoSave passing NULL for the file path. Thus eventually we end up in CDocument::DoSave. So I decided that this was the method to override. CDocument::DoSave is declared thus :-

BOOL CDocument::DoSave(LPCTSTR lpszPathName, BOOL bReplace);
  • lpszPathName :- This is the full path of the file to save. If this is NULL the default implementation will prompt the user for a filename and path using the File-Save-As common dialog.
  • bReplace :- If TRUE it will replace an existing file, if FALSE it won't.

In my particular case I was least bothered with the working of the DoSave method. My intention was to get rid of this method totally. So this is what I did - I overrode this member function and did not call the base class implementation.

BOOL CBmpToXyzDoc::DoSave(LPCTSTR lpszPathName, BOOL bReplace)
    //SrcPath is the full path of the current file
    CString DestPath = SrcPath;

    //I replace the extension with the custom one

    //Now I simply call OnSaveDocument

    //File saved successfully
    return TRUE;

That was just what I had wanted to accomplish. The user never gets prompted and the file is saved using the same name as the original except for the change in extension.

Other plausible applications

While I did not specifically require it for my project, I figured that DoSave can be used for some other purposes too. At least one nifty usage came to my mind. Assume that I wanted to do different things based on some flag. For example assume that I want to show a Save-As dialog with JPG filter if the current file is a GIF and might want to show a Save-As dialog with GIF filter if the current file is a JPG. If so, I could show my own CFileDialog after setting the corresponding OPENFILENAME members.

BOOL CBmpToXyzDoc::DoSave(LPCTSTR lpszPathName, BOOL bReplace)
    CFileDialog fd(false);

        fd.m_ofn.lpstrFilter="JPG Files(*.jpg)\0*.jpg\0\0";
        fd.m_ofn.lpstrTitle ="Save as JPG";
        fd.m_ofn.lpstrFilter="GIF Files(*.gif)\0*.gif\0\0";
        fd.m_ofn.lpstrTitle ="Save as GIF";


    return TRUE;    

Tech notes

The CDocument::DoSave implementation is very interesting. If lpszPathName is NULL, it calls CWinApp::DoPromptFileName :-

if (!AfxGetApp()->DoPromptFileName(newName,
    return FALSE;

CWinApp::DoPromptFileName itself calls CDocManager::DoPromptFileName.

BOOL CWinApp::DoPromptFileName(CString& fileName, 
                               UINT nIDSTitle, 
                               DWORD lFlags,
                               BOOL bOpenFileDialog, 
                               CDocTemplate* pTemplate)
    ASSERT(m_pDocManager != NULL);
    return m_pDocManager->DoPromptFileName(fileName, 
        nIDSTitle, lFlags,  bOpenFileDialog, pTemplate);

CDocManager::DoPromptFileName simply uses CFileDialog to prompt for a filename.

BOOL CDocManager::DoPromptFileName(CString& fileName, 
                                   UINT nIDSTitle, 
                                   DWORD lFlags, 
                                   BOOL bOpenFileDialog, 
                                   CDocTemplate* pTemplate)
    CFileDialog dlgFile(bOpenFileDialog, NULL, NULL, 

    CString title;

    dlgFile.m_ofn.Flags |= lFlags;


    INT_PTR nResult = dlgFile.DoModal();


Of course it does a lot of stuff in addition to just showing the file dialog. For example it will append a *.* filter to your File dialogs, which is why in addition to your document filter, you'll also see a *.* filter in the file type drop-down combo-box. Knowing how the flow proceeds is handy in the sense that if you want to customize it without hooking the window, you might simply override CWinApp::DoPromptFileName and call your own CFileDialog there (remember that this will affect both Open and Save dialogs).


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Nish Nishant

United States United States
Nish is a real nice guy who has been writing code since 1990 when he first got his hands on an 8088 with 640 KB RAM. Originally from sunny Trivandrum in India, he has been living in various places over the past few years and often thinks it’s time he settled down somewhere.
Nish has been a Microsoft Visual C++ MVP since October, 2002 - awfully nice of Microsoft, he thinks. He maintains an MVP tips and tricks web site - where you can find a consolidated list of his articles, writings and ideas on VC++, MFC, .NET and C++/CLI. Oh, and you might want to check out his blog on C++/CLI, MFC, .NET and a lot of other stuff -
Nish loves reading Science Fiction, P G Wodehouse and Agatha Christie, and also fancies himself to be a decent writer of sorts. He has authored a romantic comedy Summer Love and Some more Cricket as well as a programming book – Extending MFC applications with the .NET Framework.
Nish's latest book C++/CLI in Action published by Manning Publications is now available for purchase. You can read more about the book on his blog.
Despite his wife's attempts to get him into cooking, his best effort so far has been a badly done omelette. Some day, he hopes to be a good cook, and to cook a tasty dinner for his wife.

Comments and Discussions

GeneralUseful! Thanks! Pinmemberwutuma5-Dec-12 23:01 
GeneralMy vote of 5 PinmemberNithin Sundar15-Feb-11 18:59 
GeneralNot Virtual Pinmembermvinca14-Jan-05 6:56 
GeneralRe: Not Virtual Pinmemberboz11-Feb-05 2:04 
GeneralSave As with Encoding option PinsussAnonymous20-May-04 7:26 
Generalproblem in saveas Pinmemberpartha arathy8-May-03 1:01 
Generalproblem in saveas Pinmemberparha sarathy8-May-03 1:01 
GeneralNice article PinmemberKannan Kalyanaraman20-Sep-02 6:21 
GeneralRe: Nice article PineditorNishant S20-Sep-02 15:25 
GeneralRe: Nice article PinmemberRosenkilde6-May-03 5:54 
GeneralPretty Good PinmemberTom Welch19-Sep-02 11:10 
GeneralRe: Pretty Good PineditorNishant S19-Sep-02 14:41 
GeneralHmm... PinmemberShog919-Sep-02 7:22 
GeneralRe: Hmm... PineditorNishant S19-Sep-02 14:43 
GeneralRe: Hmm... PinmemberShog919-Sep-02 14:44 
GeneralA tiny little problem PinmemberYawar Maajed19-Sep-02 6:45 
GeneralRe: A tiny little problem PinmemberYawar Maajed19-Sep-02 6:50 
GeneralRe: A tiny little problem PineditorNishant S19-Sep-02 14:56 
GeneralRe: A tiny little problem PineditorNishant S19-Sep-02 15:00 
GeneralRe: A tiny little problem PinmemberYawar Maajed19-Sep-02 16:30 
GeneralRe: A tiny little problem PineditorNishant S19-Sep-02 16:55 
GeneralInteresting... PinmemberRoger Allen19-Sep-02 1:10 
GeneralRe: Interesting... PineditorNishant S19-Sep-02 1:12 
GeneralRe: Interesting... PinmemberJoao Vaz19-Sep-02 1:27 
GeneralRe: Interesting... PineditorNishant S19-Sep-02 1:40 
GeneralRe: Interesting... PinmemberJoao Vaz19-Sep-02 1:48 
GeneralRe: Interesting... PineditorNishant S19-Sep-02 1:54 
GeneralRe: Interesting... PinmemberJoao Vaz19-Sep-02 2:07 
GeneralRe: Interesting... PinmemberRoger Allen19-Sep-02 7:35 
GeneralRe: Interesting... PineditorNishant S19-Sep-02 14:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150326.1 | Last Updated 19 Sep 2002
Article Copyright 2002 by Nish Nishant
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid