How to use OLE Automation to save a Word 2007 document to the file system

I am writing a dxl script to run in batch mode. The purpose of this script is to traverse a Project, find a particular module, export it into MS Word 2007 and then automate the "SaveAs" function within Word 2007 to save the module as a Word file to the file system. I have completed everything successfully, with the exception of the automated "SaveAs" function. I need a function that completes this task. Below is what I have written.

bool saveFileAs(string fileName)
{
if(fileOpen)
{
clear objArgBlock;
put(objArgBlock, "FileName", fileName);
string result = oleMethod(objDocs, "SaveAs", objArgBlock);
if(!null result)
return false;
else return true;
}
print "File save has failed. \n";
return false;
}

This runs with no errors, however does not save the Word 2007 document.
Is this possible to do with Word 2007?
Thanks in advance.
Mike
szczy7 - Tue Jan 04 14:17:04 EST 2011

Re: How to use OLE Automation to save a Word 2007 document to the file system
szczy7 - Tue Jan 04 14:20:41 EST 2011

I forgot to add, when I print the variable result, its value states: Problem with OLE Argument names.

Re: How to use OLE Automation to save a Word 2007 document to the file system
Zetareticuli - Wed Jan 05 13:15:08 EST 2011

I'm doing this in my modified Word exporter as well. It's definitely doable. Here's how I'm doing it:

clear objArgBlock
put(objArgBlock, directoryLocation)
oleMethod(objWord, "ChangeFileOpenDirectory", objArgBlock)
clear objArgBlock
put(objArgBlock, fileName)
put(objArgBlock, 0) //0 = wdFormatDocument (which is presumably .doc or Word 2003 format)
oleMethod(objDoc, "SaveAs", objArgBlock)

I've written a wrapper function for my ole commands (not shown here) which just prints out any error message if an OLE command fails, you may want to do something similar instead of the specific check on "result" that you are making. I think DOORS has a checkResult() function which does something similar, but I'm not a fan of theirs. Mine looks like this:

TryOLE(string s, string info)
{
if(s == null) return
print info "\n"
print s
}

That allows you to call an ole command like this:

TryOLE(oleMethod(objDoc, "SaveAs", objArgBlock), "Attempting to save the document.")

That way the ole command is self-documenting and if it fails, you have a very descriptive error message for exactly which ole command failed on you.

Re: How to use OLE Automation to save a Word 2007 document to the file system
szczy7 - Wed Jan 05 15:01:26 EST 2011

Zetareticuli - Wed Jan 05 13:15:08 EST 2011
I'm doing this in my modified Word exporter as well. It's definitely doable. Here's how I'm doing it:

clear objArgBlock
put(objArgBlock, directoryLocation)
oleMethod(objWord, "ChangeFileOpenDirectory", objArgBlock)
clear objArgBlock
put(objArgBlock, fileName)
put(objArgBlock, 0) //0 = wdFormatDocument (which is presumably .doc or Word 2003 format)
oleMethod(objDoc, "SaveAs", objArgBlock)

I've written a wrapper function for my ole commands (not shown here) which just prints out any error message if an OLE command fails, you may want to do something similar instead of the specific check on "result" that you are making. I think DOORS has a checkResult() function which does something similar, but I'm not a fan of theirs. Mine looks like this:

TryOLE(string s, string info)
{
if(s == null) return
print info "\n"
print s
}

That allows you to call an ole command like this:

TryOLE(oleMethod(objDoc, "SaveAs", objArgBlock), "Attempting to save the document.")

That way the ole command is self-documenting and if it fails, you have a very descriptive error message for exactly which ole command failed on you.

Zetareticuli,
Have you gotten yours to work. I tried your suggested solution with the same results as my initial post. Any insite and/or advice would be greatly appreciated.
Thank you.

Re: How to use OLE Automation to save a Word 2007 document to the file system
Mathias Mamsch - Thu Jan 06 17:22:43 EST 2011

The following works for me:
 

OleAutoObj wDocs = null, wDoc = null 
OleAutoObj wApp = oleGetAutoObject "Word.Application"
 
olePut (wApp, "visible", "true")
oleGet(wApp, "Documents", wDocs)
OleAutoArgs args = create() 
oleMethod (wDocs, "Add", args, wDoc) 
 
put (args, "C:\\Temp\\Test.docx")  // choose a meaningful file path 
print "SaveAs:" oleMethod (wDoc, "SaveAs", args)

 


I remember that DOORS OLE functions have problems with named function arguments. So you should just leave the argument names away. I think when I tried it, that I needed to supply ALL arguements (even the optional ones), otherwise it did not work. Maybe this helps,

Regards, Mathias

 

 

 


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

 

 

Re: How to use OLE Automation to save a Word 2007 document to the file system
szczy7 - Fri Jan 07 14:06:01 EST 2011

Mathias Mamsch - Thu Jan 06 17:22:43 EST 2011

The following works for me:
 

OleAutoObj wDocs = null, wDoc = null 
OleAutoObj wApp = oleGetAutoObject "Word.Application"
 
olePut (wApp, "visible", "true")
oleGet(wApp, "Documents", wDocs)
OleAutoArgs args = create() 
oleMethod (wDocs, "Add", args, wDoc) 
 
put (args, "C:\\Temp\\Test.docx")  // choose a meaningful file path 
print "SaveAs:" oleMethod (wDoc, "SaveAs", args)

 


I remember that DOORS OLE functions have problems with named function arguments. So you should just leave the argument names away. I think when I tried it, that I needed to supply ALL arguements (even the optional ones), otherwise it did not work. Maybe this helps,

Regards, Mathias

 

 

 


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

 

 

Mathias,
I thank you greatly for your last post. I had to do a minor tweek to get it to work in my senario. Thank you again.

Re: How to use OLE Automation to save a Word 2007 document to the file system
bmic - Tue Jan 07 16:27:24 EST 2014

Mathias Mamsch - Thu Jan 06 17:22:43 EST 2011

The following works for me:
 

OleAutoObj wDocs = null, wDoc = null 
OleAutoObj wApp = oleGetAutoObject "Word.Application"
 
olePut (wApp, "visible", "true")
oleGet(wApp, "Documents", wDocs)
OleAutoArgs args = create() 
oleMethod (wDocs, "Add", args, wDoc) 
 
put (args, "C:\\Temp\\Test.docx")  // choose a meaningful file path 
print "SaveAs:" oleMethod (wDoc, "SaveAs", args)

 


I remember that DOORS OLE functions have problems with named function arguments. So you should just leave the argument names away. I think when I tried it, that I needed to supply ALL arguements (even the optional ones), otherwise it did not work. Maybe this helps,

Regards, Mathias

 

 

 


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

 

 

Hi Mathias,

 

I'm hoping if I write here that you'll get an update or something. I tried following what you instructed above, but Doors just saves a blank word document and the exported document is left unsaved.

I posted a question here and included the code that I am using:

https://www.ibm.com/developerworks/community/forums/html/topic?id=c979d21a-985c-401d-8748-b44988963914&ps=25

 

If you have any insight or advice, I would greatly appreciate it.