Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
J J is offline
external usenet poster
 
Posts: 1
Default Trying to print specific worksheet from outlook attachment

Hi everyone-

I'm trying to use VBA to print a specific "(1)ORDER FORM" worksheet
from workbooks that I'm receiving in Outlook. I've constructed the
following, but it keeps erring out when reaches the
Worksheet("(1)ORDER FORM").Printout line. I receive errors ranging
from error 9 to error 462 to error 1004. What am I doing wrong? Thanks
for your help in advance!


Public Sub PrintOrders()
On Error GoTo ErrorHandler

Dim objOutlook As Outlook.Application
Dim objMsg As Object
Dim objAttachments As Outlook.Attachments
Dim objSelectedItems As Outlook.Selection
Dim i As Long
Dim lngCounter As Long
Dim strFile As String
Dim strLocalFileLink As String
Dim strLocalPathLink As String
Dim strUserName As String
Dim strFolder As String
Dim strFolderpath As String



' Set the Attachment folder.

strFolderpath = "C:\Temp"


Set objOutlook = CreateObject("Outlook.Application")
Set objSelectedItems = objOutlook.ActiveExplorer.Selection



For Each objMsg In objSelectedItems
' if the message is a mail message
If objMsg.Class = olMail Then

Set objAttachments = objMsg.Attachments
lngCounter = objAttachments.Count

If lngCounter 0 Then
Dim strLogger As String
strLogger =
"------------------------------------------------------------"


' create and display a link to the destination folder

strLogger = strLogger & vbCrLf & "This order was
printed on " & Date & vbCrLf

'iterate the attachment object collection
For i = lngCounter To 1 Step -1

strFile = objAttachments.Item(i).FileName

If UCase(Right(strFile, 3)) = "XLS" Then
strSavedFile = strFolderpath & "\" & strFile

' save the attachment file
objAttachments.Item(i).SaveAsFile strSavedFile

PrintAtt (strSavedFile)

Kill strSavedFile
End If
Next i

strLogger = strLogger &
"------------------------------------------------------------" &
vbCrLf

' display log/links in the message body
objMsg.Body = objMsg.Body & vbCrLf & strLogger
objMsg.Save
objMsg.UnRead = False
End If
End If
Next


ExitSub:
Set objAttachments = Nothing
Set objMsg = Nothing
Set objSelectedItems = Nothing
Set objOutlook = Nothing
Exit Sub

ErrorHandler:
MsgBox "Error Code: " & Err.Number & vbCrLf & _
"Description: " & Err.Description

Err.Clear
GoTo ExitSub
End Sub


'################################################# ##############################
'### print routine
Sub PrintAtt(fFullPath As String)
On Error GoTo ErrorHandler
Dim xlApp As Excel.Application
Dim wb As Excel.Workbook
'in the background, create an instance of xl then open, print,
quit
Set xlApp = New Excel.Application
Set wb = xlApp.Workbooks.Open(fFullPath)

Worksheets("(1)ORDER FORM").PrintOut

ActiveWorkbook.Close savechanges:=False
Set wb = Nothing
xlApp.Quit
Set xlApp = Nothing


ExitSub:
ActiveWorkbook.Close savechanges:=False
Set wb = Nothing
xlApp.Quit
Set xlApp = Nothing

Exit Sub

ErrorHandler:
MsgBox "Error Code: " & Err.Number & vbCrLf & _
"Description: " & Err.Description

Err.Clear
GoTo ExitSub
End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,549
Default Trying to print specific worksheet from outlook attachment

You set a reference to the workbook and then didn't use it.
Power point doesn't know what "WorkSheets" is and the
Excel application is not visible when it starts, so there is no ActiveWorkbook.
Also, you have duplicated your exit routine.
Try...

'print routine
Sub PrintAtt(fFullPath As String)
On Error GoTo ErrorHandler
Dim xlApp As Excel.Application
Dim wb As Excel.Workbook

' in the background, create an instance of xl then open, print, quit
Set xlApp = New Excel.Application
Set wb = xlApp.Workbooks.Open(fFullPath)

wb.Worksheets("(1)ORDER FORM").PrintOut

ExitSubHe
wb.Close savechanges:=False
Set wb = Nothing
xlApp.Quit
Set xlApp = Nothing
Exit Sub

ErrorHandler:
MsgBox "Error Code: " & Err.Number & vbCrLf & _
"Description: " & Err.Description
Err.Clear
GoTo ExitSubHere
End Sub
--
Jim Cone
Portland, Oregon USA
http://www.contextures.com/excel-sort-addin.html
(30+ ways to sort in excel)

..
..
..

"J"
wrote in message
...
Hi everyone-

I'm trying to use VBA to print a specific "(1)ORDER FORM" worksheet
from workbooks that I'm receiving in Outlook. I've constructed the
following, but it keeps erring out when reaches the
Worksheet("(1)ORDER FORM").Printout line. I receive errors ranging
from error 9 to error 462 to error 1004. What am I doing wrong? Thanks
for your help in advance!

-snip-

'print routine
Sub PrintAtt(fFullPath As String)
On Error GoTo ErrorHandler
Dim xlApp As Excel.Application
Dim wb As Excel.Workbook
'in the background, create an instance of xl then open, print, quit
Set xlApp = New Excel.Application
Set wb = xlApp.Workbooks.Open(fFullPath)

Worksheets("(1)ORDER FORM").PrintOut

ActiveWorkbook.Close savechanges:=False
Set wb = Nothing
xlApp.Quit
Set xlApp = Nothing


ExitSub:
ActiveWorkbook.Close savechanges:=False
Set wb = Nothing
xlApp.Quit
Set xlApp = Nothing

Exit Sub

ErrorHandler:
MsgBox "Error Code: " & Err.Number & vbCrLf & _
"Description: " & Err.Description

Err.Clear
GoTo ExitSub
End Sub
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,549
Default Trying to print specific worksheet from outlook attachment

Correction...
"Power point" should read "Outlook"
--
Jim Cone

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Add link to specific Excel worksheet in HTML Outlook message [email protected] Links and Linking in Excel 4 May 17th 23 11:47 AM
HOW TO SEND WORKSHEET AS ATTACHMENT(IN PRINT FORMATT) rn2pacu Excel Discussion (Misc queries) 4 May 30th 07 10:33 PM
print a specific area within a worksheet by clicking on print? honclub Excel Worksheet Functions 1 October 29th 04 03:22 AM
Outlook attachment keepITcool Excel Programming 0 June 28th 04 05:01 PM
User cannot print form object after worksheet sent not as attachment Kevin Mitchell Excel Programming 1 April 20th 04 05:01 AM


All times are GMT +1. The time now is 07:00 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"