Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming,microsoft.public.word.vba.general
|
|||
|
|||
![]()
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 |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
run time error when inserting Excel sheet into word document | Excel Programming | |||
Error opening Word document from Excel VBA code | Excel Programming | |||
Find Throws Error 91 | Excel Programming | |||
Excel document was saved to word in error how do I get it back | Excel Discussion (Misc queries) | |||
How do I perform a certain function if VBA throws up an error? | Excel Programming |