Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Cannot use SaveAs method on ActiveDocument object in Word 2007

452 views
Skip to first unread message

Charles

unread,
Jul 22, 2008, 1:33:05 PM7/22/08
to
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?

Charles

unread,
Jul 22, 2008, 5:37:04 PM7/22/08
to
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!

Cindy M.

unread,
Jul 24, 2008, 12:01:14 PM7/24/08
to
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 :-)

Charles

unread,
Jul 24, 2008, 4:07:02 PM7/24/08
to
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 M.

unread,
Jul 25, 2008, 5:20:38 AM7/25/08
to
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

jprice

unread,
Jul 25, 2008, 12:04:13 PM7/25/08
to
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

Charles

unread,
Jul 29, 2008, 3:16:01 PM7/29/08
to
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.

Charles

unread,
Jul 29, 2008, 3:24:37 PM7/29/08
to
I deleted the key and word regenerated the key but I still get the same error.

Cindy M.

unread,
Jul 30, 2008, 1:24:55 PM7/30/08
to
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).

Thierry Brun

unread,
Oct 6, 2010, 6:45:36 PM10/6/10
to
I was very surprised to see others with this problem, but i guess i shouldn't have. I have 2 clients with this problem, and now experience the problem locally.

Anyone figure out why Word 2007 word object SAVE AS is having problems saving files? Same issue, where it used to work and now not. Permissions on the folder have been ruled out. What next?

Appreciate any feedback on this one.

Thierry


> On Tuesday, July 22, 2008 1:33 PM Charle wrote:

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


>>> On Thursday, July 24, 2008 12:01 PM Cindy M. wrote:

>>> Hi =?Utf-8?B?Q2hhcmxlcw==?=,
>>>

>>> 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 :-)


>>>> On Thursday, July 24, 2008 4:07 PM Charle wrote:

>>>> 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 M." wrote:


>>>>> On Friday, July 25, 2008 5:20 AM Cindy M. wrote:

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

>>>>> 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 :-)


>>>>>> On Saturday, July 26, 2008 7:50 AM jprice wrote:

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


>>>>>>> On Tuesday, July 29, 2008 3:16 PM Charle wrote:

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

>>>>>>> "Cindy M." wrote:


>>>>>>>> On Tuesday, July 29, 2008 3:24 PM Charle wrote:

>>>>>>>> I deleted the key and word regenerated the key but I still get the same error.
>>>>>>>>
>>>>>>>> "jprice" wrote:


>>>>>>>>> On Wednesday, July 30, 2008 1:24 PM Cindy M. wrote:

>>>>>>>>> Hi =?Utf-8?B?Q2hhcmxlcw==?=,
>>>>>>>>>

>>>>>>>>> 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 :-)


>>>>>>>>> Submitted via EggHeadCafe - Software Developer Portal of Choice
>>>>>>>>> Why GUIDs are not a good idea for SQL Server Primary Keys
>>>>>>>>> http://www.eggheadcafe.com/tutorials/aspnet/d662b371-ed27-481c-aee0-ebe7cf2d9fad/why-guids-are-not-a-good-idea-for-sql-server-primary-keys.aspx

0 new messages