Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Method 'SaveAs' of object '_Document' failed

Status
Not open for further replies.

mamartin

Programmer
Aug 10, 2001
75
0
0
US
I am converting a VB 6.0 application that uses MS Word & Outlook that runs on an XP platform to work with Vista. The Office package has also changed (from Office 2003 to 2007). When the Word document is to be saved, the error "Run-time error '-2147417851 (80010105)': Method 'SaveAs' of object '_Document' failed" appears. I've Googled this, but haven't found the solution. Running Debug shows the error happening on the line:

WordDoc.SaveAs (strWordFileName)

Abriged code:

Dim WordApplObj as Word.Application
Dim WordDoc as Word.Document
Dim WordSelect as Word.Selection

Dim strWordFileName as string
'
'
'
strWordFileName = conFolder + conTemplate + ".docx"

Set WordApplObj = CreateObject("Word.Application")
Set WordDoc = WordApplObj.Documents.Open(conFolder + conTemplate)
'
'
'
Section of code to modify the document.
'
'
'
WordDoc.SaveAs (strWordFileName)
WordApplObj.Quit

Set WordApplObj = Nothing
Set WordDoc = Nothing
Set WordSelect = Nothing

I am using Microsoft ActiveX Data Objects 2.5 Library and Microsoft Word 12.0 Object Library.

Any ideas?
 
have you verified the value of "strWordFileName"

strWordFileName = conFolder + conTemplate + ".docx
 
Yes. And, actually, it does not matter what I put into strWordFileName. I receive the same error each time. I have another application that uses Excel and I am receiving a very similar error to this one. In this case, I get "Method 'Add' of object 'Workbooks' failed". It must be the same problem in both apps (should be the same solution, too).
 
Do you still have Office 2003 as well as Office 2007 on your development machine?
 
Negative. Microsoft Office Professional Plus 2007 is installed.
 
Can you add a reference to the Microsoft Office object library rather than binding dynamically.
This will at least give you all the objects you require and will also show are you missing something in the SaveAs option, or potentially executing it against the wrong object



"I'm living so far beyond my income that we may almost be said to be living apart
 
There is a reference to the object library set (and the OP makes it clear that this is specifically the Microsoft Word 12.0 Object Library), since otherwise

Dim WordApplObj as Word.Application

would not work ...

Of course, the later CreateObject might, nevertheless, be trying to bind against the wrong library - but only if another version of Word was installed, hence my earlier question.
 
I think there's a possibility that the code modifying the document might be the culprit. The errortype, seems typical to automation errors, and wrong referencing might be the cause of it. Perhaps post that?

Also, seems you're opening something based on the contents of the variables conFolder + conTemplate (the concatenation operator in VB is ampersand (&)), but concatenate "another" file extension on to it. Not that I think that should cause any problems.

Might be interesting to know, to, I think, whether what you're opening is a document or template, and which version(97-2003 or 2007 doc/dot/docx/docm/dotx/dotm)

Roy-Vidar
 
strongm/hmckillop/RoyVidar,

first, sorry that I did not respond earlier, was unexpectantly out of the office for a few days.

My code post was incorrect, I am using &, not + for concatenation.

The original document being opened is Word 2007 .docx. I have tried using different extensions, hard-coding the document name, but with the same results.

As I mentioned earlier, I receive the same type of error in another app that uses Excel. This one blows up when I try to add a worksheet. There is something common going on here.

Any other ideas/suggestions, let me know.

 
Reduce the code to something like the code you posted, or try the following, which is a copy paste of some test I just ran:

[tt] Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim sfilename As String

sfilename = "c:\blah.docx"

Set oWord = CreateObject("Word.Application")
Set oDoc = oWord.Documents.Open(sfilename)
oDoc.Select

With oWord.Selection
.EndKey Unit:=wdStory
.TypeParagraph ' extra line
.TypeText Text:="blah"
.TypeParagraph
End With

' if document is in old format, one could use .convert
' oDoc.Convert

' with docx, it seems to work regardless of wheher one specifies

' format
'oDoc.SaveAs left(sfilename, Len(sfilename) - 4) & "2" & ".docx", wdFormatXMLDocument
oDoc.SaveAs left(sfilename, Len(sfilename) - 4) & "3" & ".docx"

Set oDoc = Nothing
oWord.Quit
Set oWord = Nothing[/tt]

Now, if the above code works, then the problem is elsewhere.

I'm not to sure about Word, but my impression is that the most challenges occur when trying to write something that's supposed to work both with the 2007 version and previous versions.

For Excel - with regards to saving, there are some pretty good writeups, such as this one
Roy-Vidar
 
I'll give it a try and let you know the results. Thanks much.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top