SaveAs function (and why I think it should be called SaveCopyAs)

Top  Previous  Next

Be careful with this function. It will accept a wchar_t but will not work unless the wchar_t is wrapped inside a CComBSTR type:

 

   hRes = pPartDoc->SaveAs (CComBSTR(L"C:\\CHS_LHS_COLL\\This.ipt"),VARIANT_TRUE) ;

   if (FAILED(hRes)){

       ShowCOMError(hRes,L"CCollettore::CreateCollectorMainTubePart, save failed.");

       return (false) ;

   }

 

That last parameter is:

 

VARIANT_TRUE - any existing file will be overwritten
VARIANT_FALSE - an existing file will not be overwritten and the function will fail.

 

Here is another example with an assembly document:

 

   CComQIPtr<AssemblyDocument> pAssemblyDoc ;

   ...init pAssemblyDoc ...

   CString csFullAssemblyName (L"C:\\YOUR_TEST\\Test_Assembly.iam") ;

   pAssemblyDoc->SaveAs (CComBSTR(csFullAssemblyName),VARIANT_TRUE) ;

 

Gotcha: SaveAs will save to a copy and not change the name of the document. So inside your program you'll still have the original. If you want operate on the copy you'll have to read it in again.

 

If you want to save the current in memory document with a different name you can do something like this:

 

   // In this case we just change the name and save it without closing...

   CComBSTR bstrFullFileName(m_csAssemblyFullFileName); // Convert from CString to BSTR

   pAssemblyDoc->PutFullFileName(bstrFullFileName.m_str); // Set the name (full file name ofthe document)

   pAssemblyDoc->Save(); // Save it with the new name

 

See also Save. After the first save FullFileName is read only.

 

 

 

 

 

 

Text, images and diagrams © 2021 Owen F. Ransen. All rights reserved. (But copy the source code as much as you want!)