# 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. ## 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.

2. 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.

3. 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?

4. 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

5. 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.

6. 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.

7. 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

8. 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));
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();

}

9. 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,           //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.

10. 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!

11. That may well be wrong, but hey, that's the first thing that came into my head

12. 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.

13. 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.

14. 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 Last