Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.word.vba.general
external usenet poster
 
Posts: 120
Default In XL VBA, Word Document SaveAs throws error

I'm working in Vista with Word and Excel 2007. I'm writing a macro in
Excel that opens and reads a file using FSO, creates a new Word doc
and writes the file data into the doc, and then saves the doc so I can
use Word ranges to grab the data and write it into the Excel file.

Every time the code gets to the SaveAs command for the Word document,
the code balks and throws a run-time error. If I click Debug and hit
either F8 or F5, the command immediately executes and the code
continues running just fine. The error is:
-2147417851 (80010105)
Method 'SaveAs' of object '_Docuement' failed

I tried adding Sleep in case there was something going on that wasn't
finished, and even threw in a DoEvents. No joy! If anyone can help
me over this hump, I'd greatly appreciate it!

Thanks!
Ed

strPath = Left(strFile, Len(strFile) - 4)
Sleep (15000)
DoEvents
objDoc.SaveAs strPath & ".docx" 'Both statements throw the error
'objWd.Documents(objWd.Documents.Count).SaveAs Filename:=strPath &
".doc"
Sleep (15000)
  #2   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.word.vba.general
external usenet poster
 
Posts: 23
Default In XL VBA, Word Document SaveAs throws error

What does strFile contain, or more specifically, what is its extension. If
the extension is docx, your code will be trying the save a file with a name
that has two periods before the extension.

Try objDoc.SaveAs strPath & "docx"

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

"Ed from AZ" wrote in message
...
I'm working in Vista with Word and Excel 2007. I'm writing a macro in
Excel that opens and reads a file using FSO, creates a new Word doc
and writes the file data into the doc, and then saves the doc so I can
use Word ranges to grab the data and write it into the Excel file.

Every time the code gets to the SaveAs command for the Word document,
the code balks and throws a run-time error. If I click Debug and hit
either F8 or F5, the command immediately executes and the code
continues running just fine. The error is:
-2147417851 (80010105)
Method 'SaveAs' of object '_Docuement' failed

I tried adding Sleep in case there was something going on that wasn't
finished, and even threw in a DoEvents. No joy! If anyone can help
me over this hump, I'd greatly appreciate it!

Thanks!
Ed

strPath = Left(strFile, Len(strFile) - 4)
Sleep (15000)
DoEvents
objDoc.SaveAs strPath & ".docx" 'Both statements throw the error
'objWd.Documents(objWd.Documents.Count).SaveAs Filename:=strPath &
".doc"
Sleep (15000)


  #3   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.word.vba.general
external usenet poster
 
Posts: 120
Default In XL VBA, Word Document SaveAs throws error

Hi, Doug.

No - the end of strFile is ".xml". So it's a clean string without the
"."

And when it errors and I Debug, then F8 or F5, it saves fine, with no
changes to the file path and name strings.

Ed


On Mar 23, 2:22*pm, "Doug Robbins - Word MVP"
wrote:
What does strFile contain, or more specifically, what is its extension. *If
the extension is docx, your code will be trying the save a file with a name
that has two periods before the extension.

Try objDoc.SaveAs strPath & "docx"

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

"Ed from AZ" wrote in ...



I'm working in Vista with Word and Excel 2007. *I'm writing a macro in
Excel that opens and reads a file using FSO, creates a new Word doc
and writes the file data into the doc, and then saves the doc so I can
use Word ranges to grab the data and write it into the Excel file.


Every time the code gets to the SaveAs command for the Word document,
the code balks and throws a run-time error. *If I click Debug and hit
either F8 or F5, the command immediately executes and the code
continues running just fine. *The error is:
-2147417851 (80010105)
Method 'SaveAs' of object '_Docuement' failed


I tried adding Sleep in case there was something going on that wasn't
finished, and even threw in a DoEvents. *No joy! *If anyone can help
me over this hump, I'd greatly appreciate it!


Thanks!
Ed


strPath = Left(strFile, Len(strFile) - 4)
Sleep (15000)
DoEvents
objDoc.SaveAs strPath & ".docx" *'Both statements throw the error
'objWd.Documents(objWd.Documents.Count).SaveAs Filename:=strPath &
".doc"
Sleep (15000)- Hide quoted text -


- Show quoted text -

  #4   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.word.vba.general
external usenet poster
 
Posts: 23
Default In XL VBA, Word Document SaveAs throws error


You may need to set the SaveFormat to wdFormatDocument as you are converting
from one format to another

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

"Ed from AZ" wrote in message
...
Hi, Doug.

No - the end of strFile is ".xml". So it's a clean string without the
"."

And when it errors and I Debug, then F8 or F5, it saves fine, with no
changes to the file path and name strings.

Ed


On Mar 23, 2:22 pm, "Doug Robbins - Word MVP"
wrote:
What does strFile contain, or more specifically, what is its extension.
If
the extension is docx, your code will be trying the save a file with a
name
that has two periods before the extension.

Try objDoc.SaveAs strPath & "docx"

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

"Ed from AZ" wrote in
...



I'm working in Vista with Word and Excel 2007. I'm writing a macro in
Excel that opens and reads a file using FSO, creates a new Word doc
and writes the file data into the doc, and then saves the doc so I can
use Word ranges to grab the data and write it into the Excel file.


Every time the code gets to the SaveAs command for the Word document,
the code balks and throws a run-time error. If I click Debug and hit
either F8 or F5, the command immediately executes and the code
continues running just fine. The error is:
-2147417851 (80010105)
Method 'SaveAs' of object '_Docuement' failed


I tried adding Sleep in case there was something going on that wasn't
finished, and even threw in a DoEvents. No joy! If anyone can help
me over this hump, I'd greatly appreciate it!


Thanks!
Ed


strPath = Left(strFile, Len(strFile) - 4)
Sleep (15000)
DoEvents
objDoc.SaveAs strPath & ".docx" 'Both statements throw the error
'objWd.Documents(objWd.Documents.Count).SaveAs Filename:=strPath &
".doc"
Sleep (15000)- Hide quoted text -


- Show quoted text -


  #5   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.word.vba.general
external usenet poster
 
Posts: 120
Default In XL VBA, Word Document SaveAs throws error

On Mar 23, 2:47*pm, "Doug Robbins - Word MVP"
wrote:
You may need to set the SaveFormat to wdFormatDocument as you are converting
from one format to another


I'll give that a try, Doug. I'm not sure, though - I am not opening
the XML in Word and resaving as a doc. The XML is opened in FSO and
read, a new do is created using the Word app, contents of the XML are
pasted into the new unsaved Word doc, and then I'm trying to save the
new doc.

Is there some interaction between Word and FSO that I might not be
aware of that's causing this?
Ed



--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

"Ed from AZ" wrote in ...



Hi, Doug.


No - the end of strFile is ".xml". *So it's a clean string without the
"."


And when it errors and I Debug, then F8 or F5, it saves fine, with no
changes to the file path and name strings.


Ed


On Mar 23, 2:22 pm, "Doug Robbins - Word MVP"
wrote:
What does strFile contain, or more specifically, what is its extension..
If
the extension is docx, your code will be trying the save a file with a
name
that has two periods before the extension.


Try objDoc.SaveAs strPath & "docx"


--
Hope this helps.


Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.


Doug Robbins - Word MVP, originally posted via msnews.microsoft.com


"Ed from AZ" wrote in
...


I'm working in Vista with Word and Excel 2007. *I'm writing a macro in
Excel that opens and reads a file using FSO, creates a new Word doc
and writes the file data into the doc, and then saves the doc so I can
use Word ranges to grab the data and write it into the Excel file.


Every time the code gets to the SaveAs command for the Word document,
the code balks and throws a run-time error. *If I click Debug and hit
either F8 or F5, the command immediately executes and the code
continues running just fine. *The error is:
-2147417851 (80010105)
Method 'SaveAs' of object '_Docuement' failed


I tried adding Sleep in case there was something going on that wasn't
finished, and even threw in a DoEvents. *No joy! *If anyone can help
me over this hump, I'd greatly appreciate it!


Thanks!
Ed


strPath = Left(strFile, Len(strFile) - 4)
Sleep (15000)
DoEvents
objDoc.SaveAs strPath & ".docx" *'Both statements throw the error
'objWd.Documents(objWd.Documents.Count).SaveAs Filename:=strPath &
".doc"
Sleep (15000)- Hide quoted text -


- Show quoted text -- Hide quoted text -


- Show quoted text -




  #6   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.word.vba.general
external usenet poster
 
Posts: 23
Default In XL VBA, Word Document SaveAs throws error

It would help if you showed ALL of the code. Otherwise, we are just
guessing.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

"Ed from AZ" wrote in message
...
On Mar 23, 2:47 pm, "Doug Robbins - Word MVP"
wrote:
You may need to set the SaveFormat to wdFormatDocument as you are
converting
from one format to another


I'll give that a try, Doug. I'm not sure, though - I am not opening
the XML in Word and resaving as a doc. The XML is opened in FSO and
read, a new do is created using the Word app, contents of the XML are
pasted into the new unsaved Word doc, and then I'm trying to save the
new doc.

Is there some interaction between Word and FSO that I might not be
aware of that's causing this?
Ed



--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

"Ed from AZ" wrote in
...



Hi, Doug.


No - the end of strFile is ".xml". So it's a clean string without the
"."


And when it errors and I Debug, then F8 or F5, it saves fine, with no
changes to the file path and name strings.


Ed


On Mar 23, 2:22 pm, "Doug Robbins - Word MVP"
wrote:
What does strFile contain, or more specifically, what is its
extension.
If
the extension is docx, your code will be trying the save a file with a
name
that has two periods before the extension.


Try objDoc.SaveAs strPath & "docx"


--
Hope this helps.


Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.


Doug Robbins - Word MVP, originally posted via msnews.microsoft.com


"Ed from AZ" wrote in
...


I'm working in Vista with Word and Excel 2007. I'm writing a macro
in
Excel that opens and reads a file using FSO, creates a new Word doc
and writes the file data into the doc, and then saves the doc so I
can
use Word ranges to grab the data and write it into the Excel file.


Every time the code gets to the SaveAs command for the Word
document,
the code balks and throws a run-time error. If I click Debug and
hit
either F8 or F5, the command immediately executes and the code
continues running just fine. The error is:
-2147417851 (80010105)
Method 'SaveAs' of object '_Docuement' failed


I tried adding Sleep in case there was something going on that
wasn't
finished, and even threw in a DoEvents. No joy! If anyone can help
me over this hump, I'd greatly appreciate it!


Thanks!
Ed


strPath = Left(strFile, Len(strFile) - 4)
Sleep (15000)
DoEvents
objDoc.SaveAs strPath & ".docx" 'Both statements throw the error
'objWd.Documents(objWd.Documents.Count).SaveAs Filename:=strPath &
".doc"
Sleep (15000)- Hide quoted text -


- Show quoted text -- Hide quoted text -


- Show quoted text -


  #7   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.word.vba.general
external usenet poster
 
Posts: 120
Default In XL VBA, Word Document SaveAs throws error

On Mar 23, 7:38*pm, "Doug Robbins - Word MVP"
wrote:
It would help if you showed ALL of the code. *Otherwise, we are just
guessing.


Thanks for sticking with this, Doug.
Here's everything except a couple of message boxes from the beginning
to saving the Word file:

strMeName = Environ("username")

'Set application objects
Set objWB = ActiveWorkbook

Set objFSO = CreateObject("Scripting.FileSystemObject")

On Error Resume Next
Err.Clear
Set objWd = GetObject(, "Word.application")
If Err Then
Set objWd = New Word.Application
End If
Err.Clear
On Error GoTo 0

'Select a file to work on
strFile = Application.GetOpenFilename("XML Files (*.xml),*.xml")
If strFile = "False" Then
MsgBox "No file selected"
Exit Sub
End If

'Get contents
Set objFile = objFSO.OpenTextFile(strFile, ForReading)
strAll = objFile.ReadAll

'Write to doc
Set objDoc = objWd.Documents.Add
objWd.Visible = True
Set rngDoc = objDoc.Content
rngDoc.Text = strAll
objFile.Close
Set objFile = Nothing

strPath = Left(strFile, Len(strFile) - 4)
objWB.SaveAs "C:\Users\" & strMeName & "\Desktop\MaintWorkbook.xls"

Sleep (15000)
DoEvents
objDoc.SaveAs strPath & ".docx"
'objWd.Documents(objWd.Documents.Count).SaveAs Filename:=strPath &
".doc"
Sleep (15000)
  #8   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.word.vba.general
external usenet poster
 
Posts: 120
Default In XL VBA, Word Document SaveAs throws error

Okay - got a solution. Don't know if it's "proper", but it seems to
work.

I replaced:

Sleep (15000)
DoEvents
objDoc.SaveAs strPath & ".docx"
'objWd.Documents(objWd.Documents.Count).SaveAs Filename:=strPath &
".doc"
Sleep (15000)

with:

Do
On Error Resume Next
objDoc.SaveAs strPath & ".doc"
On Error GoTo 0

strDoc = objDoc.Name
Loop While strDoc = "Document1"

Like I said, it works. I do wish I understood why the "right" way
wouldn't, though. Maybe some day ...

Thanks for keeping with me, Doug.
Ed
Ed
  #9   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.word.vba.general
external usenet poster
 
Posts: 23
Default In XL VBA, Word Document SaveAs throws error

Hi Ed,

Trimming your code down to the bare minimum

Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim strpath As String
Dim strAll As String
strpath = "C:\MergeFiles\test"
strAll = "This is the text."
On Error Resume Next
Err.Clear
Set objWd = GetObject(, "Word.application")
If Err Then
Set objWd = New Word.Application
End If
Err.Clear
On Error GoTo 0

'Write to doc
Set objDoc = objWd.Documents.Add
objWd.Visible = True
Set rngDoc = objDoc.Content
rngDoc.Text = strAll

objDoc.SaveAs strpath & ".docx"

and running that code, a document containing the "This is the text." is
created and saved with the path\name of c:\mergefiles\test\docx with no
problem.

Try re-arranging your code as follows:

strMeName = Environ("username")

'Set application objects
Set objWB = ActiveWorkbook

Set objFSO = CreateObject("Scripting.FileSystemObject")

On Error Resume Next
Err.Clear
Set objWd = GetObject(, "Word.application")
If Err Then
Set objWd = New Word.Application
End If
Err.Clear
On Error GoTo 0

'Select a file to work on
strFile = Application.GetOpenFilename("XML Files (*.xml),*.xml")
If strFile = "False" Then
MsgBox "No file selected"
Exit Sub
End If
strPath = Left(strFile, Len(strFile) - 4)

'Get contents
Set objFile = objFSO.OpenTextFile(strFile, ForReading)
strAll = objFile.ReadAll

'Write to doc
Set objDoc = objWd.Documents.Add
objWd.Visible = True
Set rngDoc = objDoc.Content
rngDoc.Text = strAll
objDoc.SaveAs strPath & ".docx"
objFile.Close
Set objFile = Nothing

objWB.SaveAs "C:\Users\" & strMeName & "\Desktop\MaintWorkbook.xls"



--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

"Ed from AZ" wrote in message
...
On Mar 23, 7:38 pm, "Doug Robbins - Word MVP"
wrote:
It would help if you showed ALL of the code. Otherwise, we are just
guessing.


Thanks for sticking with this, Doug.
Here's everything except a couple of message boxes from the beginning
to saving the Word file:

strMeName = Environ("username")

'Set application objects
Set objWB = ActiveWorkbook

Set objFSO = CreateObject("Scripting.FileSystemObject")

On Error Resume Next
Err.Clear
Set objWd = GetObject(, "Word.application")
If Err Then
Set objWd = New Word.Application
End If
Err.Clear
On Error GoTo 0

'Select a file to work on
strFile = Application.GetOpenFilename("XML Files (*.xml),*.xml")
If strFile = "False" Then
MsgBox "No file selected"
Exit Sub
End If

'Get contents
Set objFile = objFSO.OpenTextFile(strFile, ForReading)
strAll = objFile.ReadAll

'Write to doc
Set objDoc = objWd.Documents.Add
objWd.Visible = True
Set rngDoc = objDoc.Content
rngDoc.Text = strAll
objFile.Close
Set objFile = Nothing

strPath = Left(strFile, Len(strFile) - 4)
objWB.SaveAs "C:\Users\" & strMeName & "\Desktop\MaintWorkbook.xls"

Sleep (15000)
DoEvents
objDoc.SaveAs strPath & ".docx"
'objWd.Documents(objWd.Documents.Count).SaveAs Filename:=strPath &
".doc"
Sleep (15000)


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
run time error when inserting Excel sheet into word document Haim[_3_] Excel Programming 0 November 7th 08 03:16 PM
Error opening Word document from Excel VBA code engineer_rich Excel Programming 3 April 9th 08 09:26 PM
Find Throws Error 91 Dave Birley Excel Programming 13 May 17th 07 07:01 PM
Excel document was saved to word in error how do I get it back Janice Excel Discussion (Misc queries) 1 May 29th 06 03:45 PM
How do I perform a certain function if VBA throws up an error? Matt[_37_] Excel Programming 2 February 24th 06 03:06 PM


All times are GMT +1. The time now is 02:13 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"