dodonohoe
01-15-2013, 03:46 AM
Hi Guys,
I am trying to open and then print a word document. The macro works fine opening word for me, but when it tries to open the defined file it is failing with the error "Word was unable to read this document. It may be corrupt". I'm pretty sure the file is not corrupt as I can open it normally. I have marked in the code exactly where it is failing in the second sub called Sub PrintWord(ByVal x As Integer)
Thanks for any help.
Sub PrintForm()
'delete Open/Print Result from prior runs
rowno = 14
Do Until Cells(rowno, 2) & Cells(rowno, 3) = ""
If Cells(rowno, 6) <> "" Then
Cells(rowno, 6) = ""
End If
rowno = rowno + 1
Loop
Dim Month As String
'input box to put a variable (folder name) for the file path
Month = Application.Inputbox("Please enter the file path variable")
If Response = "" Then
' If Cancel was pressed,exit
Show_Box = False
End If
'puts the variable name on the macro sheet where all the files to be printed are listed
Cells(1, 7) = Month
rowno = 14
'this will tell the macro if the file type is Excel, Word or PDF
Do Until Cells(rowno, 2) & Cells(rowno, 3) = ""
FileType = Trim(Cells(rowno, 2))
'call the word piece
If FileType = "Word" Then
'call the word piece
PrintWord (rowno)
End If
If FileType = "Excel" Then
'call the excel piece
Inputs (rowno)
End If
If FileType = "PDF" Then
'call the PDF piece
PrintPDFsInputBox (rowno)
End If
rowno = rowno + 1
Loop
End Sub
Sub PrintWord(ByVal x As Integer)
'Opens a Word Document from Excel
Dim w As Workbook
Set w = ActiveWorkbook
Dim objWord As Object
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
'dim all my variables, these are put in by the user on the main macro workbook
Dim Variablez As String
Dim MyFolder As String
Dim MyFile As String
Dim FileName As String
Dim Action As String
MyFolder = Trim(Cells(x, 3))
FileName = Trim(Cells(x, 4))
Action = Trim(Cells(x, 5))
Variablez = Trim(Cells(1, 7))
MyFile = Dir(MyFolder & Variablez & "\" & FileName)
If MyFile <> "" And FileName <> "" And Action = "Open" Then
'IT FAILS HERE
objWord.Documents.Open MyFolder & Variablez & "\" & FileName
End If
If MyFile <> "" And FileName <> "" And Action = "Print" Then
objWord.Documents.Open MyFolder & Variablez & "\" & FileName
objWord.PrintOut
objWord.Quit wdDoNotSaveChanges
End If
w.Activate
End Sub
I am trying to open and then print a word document. The macro works fine opening word for me, but when it tries to open the defined file it is failing with the error "Word was unable to read this document. It may be corrupt". I'm pretty sure the file is not corrupt as I can open it normally. I have marked in the code exactly where it is failing in the second sub called Sub PrintWord(ByVal x As Integer)
Thanks for any help.
Sub PrintForm()
'delete Open/Print Result from prior runs
rowno = 14
Do Until Cells(rowno, 2) & Cells(rowno, 3) = ""
If Cells(rowno, 6) <> "" Then
Cells(rowno, 6) = ""
End If
rowno = rowno + 1
Loop
Dim Month As String
'input box to put a variable (folder name) for the file path
Month = Application.Inputbox("Please enter the file path variable")
If Response = "" Then
' If Cancel was pressed,exit
Show_Box = False
End If
'puts the variable name on the macro sheet where all the files to be printed are listed
Cells(1, 7) = Month
rowno = 14
'this will tell the macro if the file type is Excel, Word or PDF
Do Until Cells(rowno, 2) & Cells(rowno, 3) = ""
FileType = Trim(Cells(rowno, 2))
'call the word piece
If FileType = "Word" Then
'call the word piece
PrintWord (rowno)
End If
If FileType = "Excel" Then
'call the excel piece
Inputs (rowno)
End If
If FileType = "PDF" Then
'call the PDF piece
PrintPDFsInputBox (rowno)
End If
rowno = rowno + 1
Loop
End Sub
Sub PrintWord(ByVal x As Integer)
'Opens a Word Document from Excel
Dim w As Workbook
Set w = ActiveWorkbook
Dim objWord As Object
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
'dim all my variables, these are put in by the user on the main macro workbook
Dim Variablez As String
Dim MyFolder As String
Dim MyFile As String
Dim FileName As String
Dim Action As String
MyFolder = Trim(Cells(x, 3))
FileName = Trim(Cells(x, 4))
Action = Trim(Cells(x, 5))
Variablez = Trim(Cells(1, 7))
MyFile = Dir(MyFolder & Variablez & "\" & FileName)
If MyFile <> "" And FileName <> "" And Action = "Open" Then
'IT FAILS HERE
objWord.Documents.Open MyFolder & Variablez & "\" & FileName
End If
If MyFile <> "" And FileName <> "" And Action = "Print" Then
objWord.Documents.Open MyFolder & Variablez & "\" & FileName
objWord.PrintOut
objWord.Quit wdDoNotSaveChanges
End If
w.Activate
End Sub