View Full Version : PrintOut Method
counterrojo
12-08-2011, 03:12 PM
Hi,
I have been trying to use the printout method in Outlook 2010 to save emails as xps files. I lifted this part of the code from an excel macro and was hoping that it would work the same. I get a compile error. Wrong number of arguments or invalid property assignment. As far as I can see on the web all of the arguments are optional. The paticular line is near the bottom of the code. If anyone could point me in the right direction I'd appreciate it.:)
Sub SaveAsHTML()
Dim myItem As Outlook.Inspector
Dim objItem As Object
Dim EmailDate As Date
Dim MyMail As MailItem
Dim MailDate As String
On Error Resume Next
Set myOlApp = CreateObject("Outlook.Application")
Set myItem = myOlApp.ActiveInspector
Set MyMail = myItem.CurrentItem
On Error GoTo 0
If Not TypeName(myItem) = "Nothing" Then
Set objItem = myItem.CurrentItem
MailDate = MyMail.ReceivedTime
MailDate = Replace(MailDate, "/", "-")
MailDate = Replace(MailDate, ":", ".")
strname = Name ''objItem.Subject
If strname = "" Then Exit Sub '' Means User hit canceled in dialog box
'Prompt the user for confirmation
Dim strPrompt As String
Dim SaveLocation As String
SaveLocation = BrowseForFolder & "\"
If SaveLocation = "\" Then Exit Sub
''objItem.SaveAs SaveLocation & MailDate & " " & strname & " 1" & ".HTML", olHTML
MyMail.PrintOut PrintToFile:=True, PrToFileName:=SaveLocation & MailDate & " " & strname & " 1" & ".xps"
Call SaveAttachment(strname, SaveLocation, MailDate)
Else
MsgBox "You Have no Email Open"
End If
End Sub
monarchd
12-08-2011, 03:29 PM
Is this line pointing to a function or another sub in your Outlook module?
SaveLocation = BrowseForFolder & "\"
You may need to copy that over from your Excel
counterrojo
12-08-2011, 03:33 PM
It is pointing to a funtion. I am able to save the email as a HTML using that ''objItem.SaveAs SaveLocation & MailDate & " " & strname & " 1" & ".HTML", olHTML
Unfortunatly the users would prefer the xps format.
counterrojo
12-08-2011, 03:34 PM
Functioned called in SaveAsHTML:
Public Function Name() As String
Name = InputBox("Please Enter File Name")
End Function
Function BrowseForFolder() As String
Dim ShellApp As Object
Set ShellApp = CreateObject("Shell.Application").BrowseForFolder(0, "Please choose a folder", 0, "C:\Users\mdiamond\Desktop")
On Error Resume Next
BrowseForFolder = ShellApp.self.path
On Error GoTo 0
If BrowseForFolder = "" Then Exit Function
Select Case Mid(BrowseForFolder, 2, 1)
Case Is = ":"
If Left(BrowseForFolder, 1) = ":" Then
BrowseForFolder = ""
End If
Case Is = "\"
If Not Left(BrowseForFolder, 1) = "\" Then
BrowseForFolder = ""
End If
Case Else
BrowseForFolder = ""
End Select
ExitFunction:
Set ShellApp = Nothing
End Function
monarchd
12-08-2011, 03:41 PM
How about compare your References from what is in Excel to what is set in Outlook. On the menu, it's Tools | References. Maybe there is an association in Excel that's making it work there and not in Outlook.
monarchd
12-08-2011, 03:44 PM
And this line is using something called PrToFileName. Is there another function with that name over in Excel?
MyMail.PrintOut PrintToFile:=True, PrToFileName:=SaveLocation & MailDate & " " & strname & " 1" & ".xps"
counterrojo
12-08-2011, 03:52 PM
I checked my references and I have the same ones checked in both applications. I have never used this method in Excel, I found this piece of code in an excel blog... The PrToFileName is suposed to be one of the parameters.
monarchd
12-09-2011, 07:19 AM
Try this:
Sub SaveAsHTML()
Dim myItem As Outlook.Inspector
Dim objItem As Object
Dim EmailDate As Date
Dim MyMail As MailItem
Dim MailDate As String
Dim myOlApp
Dim strname As String
On Error Resume Next
Set myOlApp = CreateObject("Outlook.Application")
Set myItem = myOlApp.ActiveInspector
Set MyMail = myItem.CurrentItem
On Error GoTo 0
If Not TypeName(myItem) = "Nothing" Then
Set objItem = myItem.CurrentItem
MailDate = MyMail.ReceivedTime
MailDate = Replace(MailDate, "/", "-")
MailDate = Replace(MailDate, ":", ".")
strname = Name ''objItem.Subject
If strname = "" Then Exit Sub '' Means User hit canceled in dialog box
'Prompt the user for confirmation
Dim strPrompt As String
Dim SaveLocation As String
SaveLocation = BrowseForFolder & "\"
If SaveLocation = "\" Then Exit Sub
Call SaveAttachment(strname, SaveLocation, MailDate)
MyMail.SaveAs SaveLocation & MailDate & " " & strname & " 1" & ".xps"
Else
MsgBox "You Have no Email Open"
End If
End Sub
monarchd
12-09-2011, 07:58 AM
I'm at a loss trying to use the .PrintOut too. I can't get any parameters to play nice with .PrintOut. Hope someone else can chime in.
monarchd
12-09-2011, 09:09 AM
I was trying these Shells too but I don't have XPS Viewer installed to see if I can open them intact. I've got them commented out to flip between IE and XPS Viewer.
Sub SaveAsHTML()
Dim myItem As Outlook.Inspector
Dim objItem As Object
Dim EmailDate As Date
Dim MyMail As MailItem
Dim MailDate As String
Dim myOlApp
Dim strname As String
On Error Resume Next
Set myOlApp = CreateObject("Outlook.Application")
Set myItem = myOlApp.ActiveInspector
Set MyMail = myItem.CurrentItem
On Error GoTo 0
If Not TypeName(myItem) = "Nothing" Then
Set objItem = myItem.CurrentItem
MailDate = MyMail.ReceivedTime
MailDate = Replace(MailDate, "/", "-")
MailDate = Replace(MailDate, ":", ".")
strname = Name ''objItem.Subject
If strname = "" Then Exit Sub '' Means User hit canceled in dialog box
'Prompt the user for confirmation
Dim strPrompt As String
Dim SaveLocation As String
SaveLocation = BrowseForFolder & "\"
If SaveLocation = "\" Then Exit Sub
With MyMail
'Call SaveAttachment(strname, SaveLocation, MailDate)
MyMail.SaveAs SaveLocation & MailDate & " " & strname & " 1" & ".xps"
'Shell """C:\Windows\System32\XPSViewer\XPSViewer.exe"" /p """ + SaveLocation & MailDate & " " & strname & " 1" & ".xps" + """", vbHide
'Shell """C:\Program Files\Internet Explorer\iexplore.exe"" /p """ + SaveLocation & MailDate & " " & strname & " 1" & ".xps" + """", vbHide
End With
Else
MsgBox "You Have no Email Open"
End If
End Sub
counterrojo
12-09-2011, 10:46 AM
Hi Monarch, your first attempt managed to save the file, but the viewer wasn't able to open it. Trying your other suggestions next.
counterrojo
12-09-2011, 11:07 AM
Monarch,
I tried the first commented out code; I did not get a runtime error but XPS viewer program gave me a file not found error. The second I get no errors but nothing is save.
JP2112
12-09-2011, 11:22 AM
The MailItem.PrintOut Method does not take any arguments. You will need to use MailItem.SaveAs to save the item.
See http://msdn.microsoft.com/en-us/library/ff869250.aspx for a list of valid file types. I'm afraid XPS isn't one of them.
Also note the caveat here: http://msdn.microsoft.com/en-us/library/ff868727.aspx
Also note that even though olDoc is a valid OlSaveAsType constant, messages in HTML format cannot be saved in Document format, and the olDoc constant works only if Microsoft Word is set up as the default email editor.
monarchd
12-09-2011, 11:57 AM
Sweet - thanks for the info JP. There is much to learn :)
counterrojo
12-09-2011, 12:50 PM
Well that's nice to know it doesn't take any arguments. I think Monarcd was on the right path with a shell app. Using shell applications is new to me, but something I'll want to read up on.
monarchd
12-09-2011, 01:42 PM
Maybe something like SaveAs olHTML then shell to Word to open it and do the printOut to your default distiller printer from there. You may have to adjust your path to the Winword.exe
With MyMail
'Call SaveAttachment(strname, SaveLocation, MailDate)
MyMail.SaveAs SaveLocation & MailDate & " " & strname & " 1" & ".htm"
Shell """C:\Program Files\Microsoft Office\Office12\Winword.exe"" /q /n /mFilePrintDefault """ + SaveLocation & MailDate & " " & strname & " 1" & ".htm" + """", vbHide
End With
monarchd
12-09-2011, 01:58 PM
Whoops, forgot the type. Replace above line with this one:
MyMail.SaveAs SaveLocation & MailDate & " " & strname & " 1" & ".htm", olHTML
counterrojo
12-09-2011, 01:59 PM
Thank you Monarch. I will try that on Monday morning and let you know if it works.
monarchd
12-09-2011, 02:01 PM
Cool - I tested it here and it does save the file intact as an htm file and Word pops open just fine and *bingo* comes off the printer without any weirdness - email contents are all there. Getting closer!
counterrojo
12-09-2011, 02:15 PM
Well, I couldn't wait and remoted into my work computer. It saved but as a bunch of wierd characters. I did find a solution that works for what I need. I used the saveas method and picked the olMHTML type. The reason the users didn't want the html is that they didn't want the folders that is created with it. I still want to figure this out for my one curiosity. I'm going to have to learn more about using shell applications. I mostly work with excel and haven't had the need for them, but it looks really cool. Thanks for all of your help.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.