jazzyt2u
07-21-2008, 11:13 AM
Hi I'm trying to Save a word document by the same name as an excel document. I don't know the name in advanced to put it into the code.
My code is in excel. I'm getting an object error when trying to save the Word document by the name I get from the excel file and don't know Word enough to know what to do.
Please help: pray2:
Sub ExcelToWord()
Const MyFolder As String = "S:\TashaA\Test2\"
Dim oWord As Object
Dim oDoc As Object
Dim NextFile As String
Dim MyFile As String
NextFile = Dir(MyFolder & "*.xls", vbNormal)
Do While NextFile <> ""
Workbooks.Open MyFolder & NextFile
CurrentFile = ActiveWorkbook.Name
Sheets("PriorityItems").Activate
Range("F14").Select
ActiveCell = CurrentFile
ActiveCell.Offset(1, 0).Select
ActiveCell.Formula = "=LEFT(F14, LEN(F14)-4)" 'Removes the .xls
MyFile = ActiveCell & ".doc" 'Same File name but with .doc
Set oWord = CreateObject("Word.Application")
oWord.Visible = True
Set oDoc = oWord.Documents.Open("S:\TashaA\Test2\TestDocument.doc")
ActiveDocument.SaveAs Filename:=(MyFile)
Windows(CurrentFile).Activate
NextFile = Dir
Loop
End Sub
My code is in excel. I'm getting an object error when trying to save the Word document by the name I get from the excel file and don't know Word enough to know what to do.
Please help: pray2:
Sub ExcelToWord()
Const MyFolder As String = "S:\TashaA\Test2\"
Dim oWord As Object
Dim oDoc As Object
Dim NextFile As String
Dim MyFile As String
NextFile = Dir(MyFolder & "*.xls", vbNormal)
Do While NextFile <> ""
Workbooks.Open MyFolder & NextFile
CurrentFile = ActiveWorkbook.Name
Sheets("PriorityItems").Activate
Range("F14").Select
ActiveCell = CurrentFile
ActiveCell.Offset(1, 0).Select
ActiveCell.Formula = "=LEFT(F14, LEN(F14)-4)" 'Removes the .xls
MyFile = ActiveCell & ".doc" 'Same File name but with .doc
Set oWord = CreateObject("Word.Application")
oWord.Visible = True
Set oDoc = oWord.Documents.Open("S:\TashaA\Test2\TestDocument.doc")
ActiveDocument.SaveAs Filename:=(MyFile)
Windows(CurrentFile).Activate
NextFile = Dir
Loop
End Sub