Printing Excel and word from Access

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a switchboard that has hyperlinks to several excel forms and a Word document. I would like to be able to have it just print those documents automatically instead of opening those documents and making the user go through the steps to print it. Does anyone know the code that would do that? Thank you in advance for any help that you could offer.
Steve
 
Hi Steve

This little function should do the trick for you:

Public Function PrintDoc(FileName As String) As Boolean
Dim oApp As Object, oDoc As Object
On Error GoTo ProcErr
Select Case Right(FileName, 3)
Case "doc"
Set oApp = CreateObject("Word.Application")
Set oDoc = oApp.Documents.Open(FileName, ReadOnly:=True)
Case "xls"
Set oApp = CreateObject("Excel.Application")
Set oDoc = oApp.Workbooks.Open(FileName, ReadOnly:=True)
Case Else
Err.Raise vbObjectError, , "Unsupported file type"
End Select
oDoc.PrintOut
PrintDoc = True
ProcEnd:
On Error Resume Next
If Not oDoc Is Nothing Then
oDoc.Close 0
Set oDoc = Nothing
End If
If Not oApp Is Nothing Then
oApp.Quit
Set oApp = Nothing
End If
Exit Function
ProcErr:
MsgBox Err.Description, vbExclamation, "Cannot print " & FileName
Resume ProcEnd
End Function

--
Good Luck!

Graham Mandeno [Access MVP]
Auckland, New Zealand

Steve said:
I have a switchboard that has hyperlinks to several excel forms and a Word
document. I would like to be able to have it just print those documents
automatically instead of opening those documents and making the user go
through the steps to print it. Does anyone know the code that would do
that? Thank you in advance for any help that you could offer.
 
Back
Top