Saving a document using Word COM

This is a discussion on Saving a document using Word COM within the Windows Programming forums, part of the Platform Specific Boards category; Hi I have searched the net and my MSDN disc this time and I don't get it. I have created ...

  1. #1
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128

    Saving a document using Word COM

    Hi I have searched the net and my MSDN disc this time and I don't get it.

    I have created a word document and made it visible and all, now I want to save it to a specific location, but I don't understand how the _Document.SaveAs(); function works.

    Could someone please let me know how I can save.

    Thanks in advance.
    Compiler == Visual C++ 6.0
    "Come Out Fighting."

  2. #2
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503

  3. #3
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128
    Thanks for that. Unfortunately it hasn't helped. The examples it uses are in VB not in C or C++.

    When I try and pass the document name either as a string or a literal string it errors, I have tried passing it just the name and the full path (full path preferable) and it won't accept it.

    If anyone has any experience of using it I would appreciate the help.
    Compiler == Visual C++ 6.0
    "Come Out Fighting."

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    It looks to me that you're suposed to pass a pointer to a string (an array of chars not a string object) did you try that?

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    It looks like it accepts VARIANTS, so you need to wrap all params in VARIANTs before passing.

    Also, they are all optional, so you may have to pass an empty (VT_EMPTY) VARIANT for each param that you are not intersted in

  6. #6
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128
    Yes sorry I have tried every attempt at passing a string or an address of a string or pointer. Thanks for pointing out about VARIANTs, I realise that a VARIANT is a structure, but reading into it I don't understand how the structure members can equate to a path or even filename.
    Compiler == Visual C++ 6.0
    "Come Out Fighting."

  7. #7
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128
    Sorry about this I am being a bit stupid I am on my way to working out how to use it. I get now that the VARIANT can be many thing, and you have to set what type you are using usig the VT flag, I am setting it to VT_BSTR to pass it a reference to a string, now I just need to work out how to reference the string properly.
    Compiler == Visual C++ 6.0
    "Come Out Fighting."

  8. #8
    Chief Code Coloniser!
    Join Date
    Apr 2005
    Posts
    121
    Hi UnclePunker,

    I did this a while ago (so pardon me if i'm a bit rusty ), but you do indeed need to use variants for those parameters. When you set the VT_BSTR flag, you're half way there, but you can't set the content of the variant to a C-style/ANSI string, you need to use BSTR (B strings).

    If you're using C++, you can use the _bstr_t object to help you with the conversions. Something like this:
    Code:
    _bstr_t myBString("C:\\path\\to\\document.doc");
    BSTR paramToPassToSaveAs = (BSTR)myBString;
    That might not be exactly right, but should get you on the way!
    Good luck.

    Edit: Just quickly, the paramToPassToSaveAs variable in the example is what needs to be assigned to the variant, which is then passed into the SaveAs() call
    Last edited by TheColonial; 04-19-2005 at 04:07 AM. Reason: Adding a bit more info

  9. #9
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Code:
    #import "C:\Program Files\Common Files\Microsoft Shared\Office10\MSO.DLL" \
    	rename("RGB","RGB_") rename("DocumentProperties","DocumentProperties_")
    #import "C:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.olb" 
    #import "C:\Program Files\Microsoft Office\Office10\MSWORD.olb" \
    	rename("ExitWindows","ExitWindows_") rename("FindText","FindText_")
    //These previous imports depend on your version of office and where you have installed it.
    //These lines relate to Office XP
    
    #include <windows.h>
    #include <tchar.h>
    #include <comutil.h>
    #include <comdef.h>
    
    void TestHRESULT(HRESULT Res)
    {
    	if(FAILED(Res))
    		throw _com_error(Res);
    }
    
    int main()
    {
    	CoInitialize(0);
    	try
    	{
    		Word::_ApplicationPtr App = 0;
    		Word::_DocumentPtr Doc = 0;
    		Word::SelectionPtr Sel = 0;
    		TestHRESULT(App.CreateInstance(L"Word.Application"));
    		App->PutVisible(_variant_t(TRUE));
    		Doc = App->GetDocuments()->Add();
    		Sel = Doc->GetActiveWindow()->GetSelection();
    		Sel->TypeText(_bstr_t(L"Testing..."));
    		Doc->SaveAs(&_variant_t(L"C:\\test.doc"));
    		Doc->Close();
    		App->Quit();
    	}
    	catch(_com_error& e)
    	{
    		MessageBox(HWND_DESKTOP,e.ErrorMessage(),_T("Com Error"),MB_OK);
    	}
    	CoUninitialize();
    
    }

  10. #10
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128
    Thanks a lot for that TheColonial.

    I have got further on and have actually managed to get it to compile, but when it comes to the SaveAs bit it errors. here is my code, it may not be the tidiest but when I get to grips with it I will tidy up.

    Code:
    		VARIANT saveaway;
    		VARIANT varempty;
    
    		varempty.vt = VT_ERROR;
    
    		varempty.scode = DISP_E_PARAMNOTFOUND;
    
    		saveaway.vt = VT_BSTR;
    
    		_bstr_t myBString(path);
    
    		BSTR parampass = (BSTR)myBString;
    
    		saveaway.pbstrVal = &parampass;
    
    		oDoc.SaveAs(
                     &saveaway,  //FileName 
                     &varempty,               //FileFormat
                     &varempty,           //LockComments                                
                     &varempty,              //Password
                     &varempty,            //Add to recent files                                
                     &varempty,              //WritePassword                
                     &varempty,           //ReadOnlyRecommended                                
                     &varempty,           //EmbedTrueTypeFonts
                     &varempty,           //SaveNativePictureFormat                                
                     &varempty,           //SaveFormsData
                     &varempty);            //SaveAsOCLetter
    the error I get at runtime is:

    This is not a valid file name.
    Try one or more of the following.
    * Check the path to make sure it was typed correctly.
    * Select a file from the list of files and folders.

    The path variable I am putting into myBString is correct ("W:\05\test\MailsortReport.doc") So I am assuming something is getting lost on the

    BSTR parampass = (BSTR)myBString;

    when I run it and check what is in parampass it just shows 87, and not a path.
    Compiler == Visual C++ 6.0
    "Come Out Fighting."

  11. #11
    Chief Code Coloniser!
    Join Date
    Apr 2005
    Posts
    121
    So I am assuming something is getting lost on the

    BSTR parampass = (BSTR)myBString;
    Not quite mate. That bit should work fine. Check out this line:
    Code:
    saveaway.pbstrVal = &parampass;
    parampass is a BSTR, which is already a pointer. so when you pass the address, you're passing a pointer to the string, instead of the string itself. Try this instead:
    Code:
    saveaway.pbstrVal = parampass;
    Good luck!

  12. #12
    Chief Code Coloniser!
    Join Date
    Apr 2005
    Posts
    121
    That may well be wrong, but hey, that's the first thing that came into my head

  13. #13
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128
    Thanks TheColonial. I have tried assigning thae VARIANT value using just parampass, but it has a compiler error saying cannot convert from 'unsigned short *' to 'unsigned short **' which is why I was trying to pass it the address of a pointer (effectively a pointer to a pointer, which it seemed to want). When i pass it the address it compiles, but dies when it runs to the Actual oDoc.SaveAs().

    Thanks for the help though. I am having a break from looking at it now and I will mull it over for a while and see what happens when I come back to it a fresh.
    Compiler == Visual C++ 6.0
    "Come Out Fighting."

  14. #14
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128
    Quote Originally Posted by Fordy
    Code:
    		Doc->SaveAs(&_variant_t(L"C:\\test.doc"));

    My god Fordy, nice one. All your other code looked very impressive, but this is the bit I am most interested in. I don't really get what it is you are doing here, I have added it to my program and modified to it to take my path variable rather than your literal path, and it has worked, lovely.

    Could you please explain what it means, it seems to be some short hand way of doing everything I have been trying to do.
    Compiler == Visual C++ 6.0
    "Come Out Fighting."

  15. #15
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Quote Originally Posted by UnclePunker
    My god Fordy, nice one. All your other code looked very impressive, but this is the bit I am most interested in. I don't really get what it is you are doing here, I have added it to my program and modified to it to take my path variable rather than your literal path, and it has worked, lovely.

    Could you please explain what it means, it seems to be some short hand way of doing everything I have been trying to do.
    The _variant_t(L"C:\\test.doc") creates a temporary object that will last as long as the function call, then I use the '&' operator to get a pointer to it.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. Opening link from Word document
    By BobDole in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 02-20-2008, 08:11 AM
  4. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  5. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 05:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21