PDA

View Full Version : Solved: Opening And Printing A Word Document



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

snb
01-15-2013, 04:08 AM
and now in VBA:

Sub PrintWord_snb(ByVal x As Integer)
with sheets(1)
if Trim(.Cells(1, 5))="Action"
with getobject(Trim(.Cells(x, 3))& Trim(.Cells(1, 7)) & "\" & Trim(.Cells(x, 4)))
.printout true
.close 0
end with
end if
end with
End Sub

dodonohoe
01-16-2013, 02:48 AM
Hi SNB,

Thanks for the response. Unfortunately I am having trouble getting this to work. Here is how I have amended my second sub. Also is your code missing a "Then" at the end of the "If" line? I am not getting any error, it just opens word but not the file.


Sub PrintWord_snb(ByVal x As Integer)


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))


With Sheets(1)
If Trim(.Cells(1, 5)) = "Print" Then
With GetObject(Trim(.Cells(x, 3)) & Trim(.Cells(1, 7)) & "\" & Trim(.Cells(x, 4)))
.PrintOut True
.Close 0
End With
End If
End With

w.Activate

End Sub

snb
01-16-2013, 05:10 AM
this is the only code you need:



Sub PrintWord_snb(x)
With Sheets("Master")
If Trim(.Cells(x, 5)) = "Print" Then
With GetObject(Trim(.Cells(x, 3)) & Trim(.Cells(1, 7)) & "\" & Trim(.Cells(x, 4)))
.PrintOut True
.Close 0
End With
End If
End With
End Sub


You may have to adapt the name of the sheet and you should check the values in de the cells you are referring to.

dodonohoe
01-16-2013, 05:15 AM
SNB I'm sorry about this but I just realised I didn't mention that I am running this in Excel.

snb
01-16-2013, 06:27 AM
That's what this code is for. (Since you are posting in an Excel forum isn't it ?)

dodonohoe
01-16-2013, 09:10 AM
Thanks SNB, I just wanted to make sure I had provided the correct information. That worked great so thank you. The other action I wanted it to do was just open the file. Print worked fine with the following:

With GetObject(FilePath)
.Activate
.PrintOut True
.Close 0

Open does not work when I try something similar

With GetObject(FilePath)
.Activate
.Open True

snb
01-17-2013, 04:14 AM
The file is being opened by 'getobject'.
If you want to make it's content visible:

GetObject("fullname").Application.Visible = True

dodonohoe
01-22-2013, 02:01 AM
Hi SNB,

Thanks very much for your time. That worked perfectly I am marking this thread as solved.

Thanks