Solved

Call Shell command in Outlook VBA to print an email with Bullzip's Printto.exe

Posted on 2013-02-07
10
2,105 Views
Last Modified: 2013-02-08
I have VBA code that parses emails in a folder and for each one creates a text string of how I want to name a PDF file that is created by Bullzip's Printo.exe command.

I call a Sub with the command "  Call PrintEmailwithBullzip(strPutInFolder, Filename)"
That routine is:
Sub PrintEmailwithBullzip(ByVal strPutInFolder, ByVal Filename As String)

    Dim fso As New FileSystemObject
    Dim cff As New Bullzip.PDFPrinterSettings
    Dim cmd As String
    Dim isrunonce As String
    Dim ObjItem As Outlook.MailItem
    Set ObjItem = GetCurrentItem()
   
    cff.SetPrinterName ("Bullzip PDF Printer")

    With cff

        'Output = strPutInFolder & Filename & ".pdf"

        Rem -- Set the values
        .Init
        .SetValue "Output", strPutInFolder & Filename & ".pdf"
        .SetValue "ShowSettings", "always"
        .SetValue "ShowPDF", "no"
        .SetValue "WatermarkText", Now
        .SetValue "ShowProgress", "yes"
        .SetValue "ShowProgressFinished", "yes"
        .SetValue "SuppressErrors", "no"
        .SetValue "ConfirmOverwrite", "no"

        Rem -- Write settings to the runonce.ini
        .WriteSettings True
    End With
   
 'PrintFile = strPutInFolder & Filename & ".pdf"
   
      'cmd = """C:\Media\Bullzip\printto.exe"" Outlook.Application.ActiveExplorer.CurrentFolder.Item(ItemNmbr) ""Bullzip PDF Printer"""
    'cmd = """C:\Media\Bullzip\printto.exe"" & Outlook.Application.ActiveExplorer.CurrentFolder.Item(ItemNmbr)& ""Bullzip PDF Printer"""
    'cmd = "C:\Media\Bullzip\printto.exe " & Outlook.Application.ActiveExplorer.CurrentFolder.Item(ItemNmbr) & " ""Bullzip PDF Printer"""
    'cmd = "C:\Media\Bullzip\printto.exe " & "C:\Media\Bullzip\license.txt" & "Bullzip PDF Printer"
   
     'Call Shell("C:\Windows\system32\cmd.exe  " & cmd, 3)
     'Call Shell("C:\Windows\system32\cmd.exe  " & cmd, 1)
    'Call Shell("C:\Windows\system32\cmd.exe" /c & cmd, 0)

End Sub


I can't get the Call Shell( ) syntax correct.  Above are some of the combinations I've tried.  All seem to compile correctly and don't crash when I run them, but nothing is created.  
So I need someone familiar with Bullzip to check the setvalue statements. And a VBA mavin to give me the proper reference to the "then-current-mail-item" that the loop in the "get name" calling routine is "pointing to" which will print the PDF named as desired.

If you create the variables  strPutInFolder and  Filename (including ".pdf") in one sub, call it and pass them to this Sub, you can test.  I've left in all the tried and failed lines commented out.
0
Comment
Question by:Fritters
  • 7
  • 3
10 Comments
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
Hi, Fritters.

The code below will run a command.

Sub RunExternalProgram(strCommand As String, bolWaitForReturn As Boolean)
    Dim objShell As Object
    Set objShell = CreateObject("WScript.Shell")
    objShell.Run strCommand, , bolWaitForReturn
    Set objShell = Nothing
End Sub

Open in new window


I don't use Bullzip, so I can't tell you what the correct command line is.  Using one of the lines you listed above as a example, here's how you'd call it.

RunExternalProgram "C:\Media\Bullzip\printto.exe " & "C:\Media\Bullzip\license.txt" & "Bullzip PDF Printer"
0
 

Author Comment

by:Fritters
Comment Utility
Compiling

RunExternalProgram "C:\Media\Bullzip\printto.exe " & "C:\Media\Bullzip\license.txt" & "Bullzip PDF Printer"  

returns a "Argument not optional" error.  Do I need to pass a second parameter to match the two in the subroutine?

so I tried to add a ,true as in

     RunExternalProgram ("C:\Media\Bullzip\printto.exe " & "C:\Media\Bullzip\license.txt" & "Bullzip PDF Printer", True)

and got a Compile error: Expected: =

BTW, I'm not trying to print "C:\Media\Bullzip\license.txt" or any file.  I'm trying to print an Outlook mailitem which I THINK is referenced as "Outlook.Application.ActiveExplorer.CurrentFolder.Item(ItemNmbr)" and is passed as part of the CMD string.

Not sure if that's the right way to reference the item, though I think it is.
0
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
Yes, I forgot the second parameter.  The value you pass should probably be False (you don't need the macro to wait for the external process to finish).  The format of the command is

RunExternalProgram CommandToRun, True | False

As I said, I don't know anything about Bullzip, so I don't know what command to run.  From what you wrote ("I want to name a PDF file that is created by Bullzip's Printo.exe command.") it sounded like you need to run the Bullzip program and pass it something.  If you just need to print the Outlook message, then

    ObjItem.PrintOut

will do that.  You don't need to run an external program at all.  If instead you do need to run Bullzip, then you'll have to determine the command to run and pass it RunExternalProgram.
0
 

Author Comment

by:Fritters
Comment Utility
ObjItem.PrintOut may print the email item, but the Filename and Path information would be completely ignored.  Not a solution.
0
 
LVL 76

Accepted Solution

by:
David Lee earned 500 total points
Comment Utility
I downloaded and installed a copy of Bullzip so I could see for myself how it works.  I then put together a solution that prints a message from Outlook.  Here's that code.  To use this, call PrintMsgWithBZ and pass it the message to print, the path to the folder you want the resulting PDF to appear in, and the name you want to give the PDF file.

Sub PrintMsgWithBZ(olkMsg As Outlook.MailItem, strFolder As String, strFilename As String)
    Dim objBZSettings As New Bullzip.PDFPrinterSettings
    With objBZSettings
        .SetPrinterName "Bullzip PDF Printer"
        .Init
        .SetValue "Output", strFolder & "\" & strFilename & ".pdf"
        .SetValue "ShowSettings", "never"
        .SetValue "ShowSaveAs", "never"
        .SetValue "ShowPDF", "no"
        .SetValue "WatermarkText", Now
        .SetValue "ShowProgress", "yes"
        .SetValue "ShowProgressFinished", "yes"
        .SetValue "SuppressErrors", "no"
        .SetValue "ConfirmOverwrite", "no"
        .WriteSettings True
    End With
    olkMsg.PrintOut
    Set objBZSettings = Nothing
End Sub

Open in new window

0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:Fritters
Comment Utility
Here's the line in my calling subroutine:
Call PrintMsgWithBZ(Outlook.Application.ActiveExplorer.CurrentFolder.Item(ItemNmbr), strPutInFolder, strFilename)

I'm assuming that the parameter names being passed don't have to be exactly the same as the parameters in your subroutine.

Long and short, not only aren't any PDF's created ( manually set the default printer to the Bullzip PDF Printer), a "runonce.ini" file is not created (where it didn't exist as it's supposed to) and doesn't get edited with the .setValue's as it used to in my non-working code.
0
 

Author Comment

by:Fritters
Comment Utility
olkMsg.PrintOut?  Ahh, the first parameter.  I see.
0
 

Author Comment

by:Fritters
Comment Utility
Found an "error".  My calling Sub already put a "\" on the strFolder.  You added a second one in your routine.  Testing again.  Wait.
0
 

Author Comment

by:Fritters
Comment Utility
Well, I guess I have to give credit for the solution.  I had been attempting to use the printto.exe program to cause the printout and that was the source of the problem for me.  I had a very similar routine to your code already that worked when printing an individual email item but when realized that such code or yours could be a routine called from a loop, that put me on the right track.
However I realized that my code in the calling routine (not posted here) was not setting the default printer to Bullzip which I need to happen for your or my code.  I had been using some code I found online.  Must have messed it up.  But, in any case, when I set the default printer to manually, your solution works.  so, I'll accept it.
0
 

Author Closing Comment

by:Fritters
Comment Utility
See my post for explanation.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
users outlook version on exchange 2010 5 41
MS Outlook Move email to folder 2 38
SBS Server and Office 365 5 33
outlook 15 42
Get an idea of what you should include in an email disclaimer with these Top 5 email disclaimer tips.
If you don't know how to downgrade, my instructions below should be helpful.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now