Consulting

Results 1 to 20 of 20

Thread: PrintOut Method

  1. #1

    PrintOut Method

    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.

    [VBA]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[/VBA]

  2. #2
    VBAX Regular
    Joined
    Oct 2011
    Posts
    41
    Location
    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

  3. #3
    It is pointing to a funtion. I am able to save the email as a HTML using that [VBA]''objItem.SaveAs SaveLocation & MailDate & " " & strname & " 1" & ".HTML", olHTML
    [/VBA]

    Unfortunatly the users would prefer the xps format.

  4. #4
    Functioned called in SaveAsHTML:

    [VBA]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[/VBA]

  5. #5
    VBAX Regular
    Joined
    Oct 2011
    Posts
    41
    Location
    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.

  6. #6
    VBAX Regular
    Joined
    Oct 2011
    Posts
    41
    Location
    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"

  7. #7
    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.

  8. #8
    VBAX Regular
    Joined
    Oct 2011
    Posts
    41
    Location
    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

  9. #9
    VBAX Regular
    Joined
    Oct 2011
    Posts
    41
    Location
    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.

  10. #10
    VBAX Regular
    Joined
    Oct 2011
    Posts
    41
    Location
    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

  11. #11
    Hi Monarch, your first attempt managed to save the file, but the viewer wasn't able to open it. Trying your other suggestions next.

  12. #12
    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.

  13. #13
    VBAX Expert JP2112's Avatar
    Joined
    Oct 2008
    Location
    Astoria, NY
    Posts
    590
    Location
    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.
    Regards,
    JP

    Read the FAQ
    Getting free help on the web
    My website
    Please use [vba][/vba] tags when posting code

  14. #14
    VBAX Regular
    Joined
    Oct 2011
    Posts
    41
    Location
    Sweet - thanks for the info JP. There is much to learn

  15. #15
    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.

  16. #16
    VBAX Regular
    Joined
    Oct 2011
    Posts
    41
    Location
    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

    [vba]
    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
    [/vba]

  17. #17
    VBAX Regular
    Joined
    Oct 2011
    Posts
    41
    Location
    Whoops, forgot the type. Replace above line with this one:

    [vba]
    MyMail.SaveAs SaveLocation & MailDate & " " & strname & " 1" & ".htm", olHTML
    [/vba]

  18. #18
    Thank you Monarch. I will try that on Monday morning and let you know if it works.

  19. #19
    VBAX Regular
    Joined
    Oct 2011
    Posts
    41
    Location
    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!

  20. #20
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •