Cannot use SaveAs method on ActiveDocument object in Word 2007

C

Charles

I have a vbscript program which creates and saves a word document using the
automation interface. This was working with Word 2003, but since the upgrade
to Word 2007 it is consistently failing on the call to the SaveAs method. The
error is "The server threw and exception" with code 80010105, and source:
(null). I'm using XP SP 3. This is a simplified code example which is failing
in this way.

set word = createObject("word.application")
word.visible = true
word.documents.add
word.activeDocument.saveAs "Test.docx"

I've tried using the document format parameter to force it to be the XML
document format, or forcing the format as Word 97 (and using a ".doc" name),
also setting all of the option parameters for the SaveAs method. I've tried
having the script sleep before calling SaveAs to see if a timing issue
existed. Specified the full path. Nothing seems to make any difference. Calls
to saveAs will work if I write them in VBA inside the word VBA editor, but
not from VBScript running either as cscript.exe or wscript.exe. The document
it's trying to create (current directory for the WINWORD.EXE process is My
Documents folder) does not exist.

Anyone have any ideas?
 
C

Charles

Charles adds:

Additionally I get a similar result from running the same commands in
PowerShell 1.0. Here is the script.

$word = new-object -comObject "word.application"
$word.visible = $true
$word.documents.add()
$word.activeDocument.saveAs("Test.doc")

As with vbscript, Word opens, and this is the output to PowerShell.
format-default : Error HRESULT E_FAIL has been returned from a call to a COM
component.

Also this is Word 2007 SP 1.

Thanks!
 
C

Cindy M.

Hi =?Utf-8?B?Q2hhcmxlcw==?=,
I have a vbscript program which creates and saves a word document using the
automation interface. This was working with Word 2003, but since the upgrade
to Word 2007 it is consistently failing on the call to the SaveAs method. The
error is "The server threw and exception" with code 80010105, and source:
(null). I'm using XP SP 3. This is a simplified code example which is failing
in this way.

set word = createObject("word.application")
word.visible = true
word.documents.add
word.activeDocument.saveAs "Test.docx"
Well, I'd try creating a document object and assigning that directly to the new
document. Possibly, "ActiveDocument" is not returning what you expect:

set doc = word.documents.add
doc.saveAs "Test.docx"

If that doesn't help, are you able to use the object "doc" at all? For example:
doc.Content.Text = "test"

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
C

Charles

Thanks for the suggestions. The same fault occurs whether using
word.activeDocument or the document object reference from the call to
word.documents.add (which test as being equal to each other actually.)

The document object seems to work fine. The initial version of this program
generated 100+ page documents, with lots of formatting, tables, etc. After
upgrading to Word 2007, the generated document looked fine -- it just fails
to save through the automation interface. It can be saved manually since the
word.visible property is set and the instantiated word application is just
sitting there. (It also fails with the same error if .visible is not set.) I
get the same behavior for small documents, empty documents, large documents.
 
C

Cindy M.

Hi Charles,

Do you have access to VB6 or a version of Visual Studio? Or, worst case, you
could use Excel...

The next thing I'd test is whether you also see the problem when automating
from something more "local". If that works, then you can be fairly sure the
problem is with how vbscript is communicating. If, say, Excel VBA demonstrates
the same problem, it's more likely something with Word. And you might also get
a more informative error message.

The other thing I'd try is saving to a more "generic" folder. Perhaps there are
some kind of permissions restrictions on writing the a profile-related folder
("My Documents").
Thanks for the suggestions. The same fault occurs whether using
word.activeDocument or the document object reference from the call to
word.documents.add (which test as being equal to each other actually.)

The document object seems to work fine. The initial version of this program
generated 100+ page documents, with lots of formatting, tables, etc. After
upgrading to Word 2007, the generated document looked fine -- it just fails
to save through the automation interface. It can be saved manually since the
word.visible property is set and the instantiated word application is just
sitting there. (It also fails with the same error if .visible is not set.) I
get the same behavior for small documents, empty documents, large documents.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
J

jprice

You may want to try deleting the following registry key. Sometimes it
gets corrupted. When you delete it Word will recreate the key and it
has fixed some strange problems I have had.

HKEY_CURRENT_USER\SOFTWARE\MICROSOFT\OFFICE\12.0\WORD
 
C

Charles

I used Excel VBA, though I have VS .Net 2003. I get the same "server threw an
exception" message when I run this code in Excel VBA.

Dim word As Object
Set word = CreateObject("Word.Application")
word.Visible = True

Dim doc As Object
Set doc = word.Documents.Add

doc.Content.Text = "Test"
doc.SaveAs "c:\workdir\Test.docx"

c:\workdir exists and is writeable.
 
C

Cindy M.

Hi =?Utf-8?B?Q2hhcmxlcw==?=,
I used Excel VBA, though I have VS .Net 2003. I get the same "server threw an
exception" message when I run this code in Excel VBA.

Dim word As Object
Set word = CreateObject("Word.Application")
word.Visible = True

Dim doc As Object
Set doc = word.Documents.Add

doc.Content.Text = "Test"
doc.SaveAs "c:\workdir\Test.docx"

c:\workdir exists and is writeable.
So we can assume it's not a permissions issue?

What starting Windows in Safe Mode, does that make any difference? If it does,
I'd look for some outside software (anti-virus, file management) that might be
interfering with saving documents when control is coming from outside the
application.

You might also try using Shell with the /a parameter to start Word in Safe Mode,
then connect using GetObject.

And you might try Set word = New Word.Application in Excel VBA (as an
alternative to CreateObject, since they use a different approach).

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top