• VBA – Printing a picture using OIS.exe without the print dialog box

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » VBA – Printing a picture using OIS.exe without the print dialog box

    Author
    Topic
    #502320

    Hi there.

    I’m hoping someone can help me. I currently have a vba macro in Excel that looks in Column “A” and picks out all paths of documents with an ending of “.pdf” and prints the document using the default pdf reader. I would like to expand this macro so that it prints .jpg, .tiff and .tifflist pictures to either the Microsoft Picture Manager or the Windows Photo Viewer, except that I end up getting the print dialog box, which I don’t want. I tried searching the internet, but couldn’t seem to find a way to resolve this. I can’t use Paint because there are multi-page Tiff files that will only print the first page in the Tiff file.

    Has anyone accomplished this? I will include the code that I have so far and cross my fingers that it is something really easy…

    Code:
     Option Explicit
          Private Declare Function ShellExecute Lib “shell32.dll” Alias _
          “ShellExecuteA” (ByVal hwnd As Long, ByVal lpszOp As _
          String, ByVal lpszFile As String, ByVal lpszParams As String, _
          ByVal lpszDir As String, ByVal FsShowCmd As Long) As Long
          Private Declare Function GetDesktopWindow Lib “user32″ () As Long
         
          Const SW_SHOWNORMAL = 1
          Const SE_ERR_FNF = 2&
          Const SE_ERR_PNF = 3&
          Const SE_ERR_ACCESSDENIED = 5&
          Const SE_ERR_OOM = 8&
          Const SE_ERR_DLLNOTFOUND = 32&
          Const SE_ERR_SHARE = 26&
          Const SE_ERR_ASSOCINCOMPLETE = 27&
          Const SE_ERR_DDETIMEOUT = 28&
          Const SE_ERR_DDEFAIL = 29&
          Const SE_ERR_DDEBUSY = 30&
          Const SE_ERR_NOASSOC = 31&
          Const ERROR_BAD_FORMAT = 11&
       Private Declare Function GetRidofDialog Lib ”    ” ()
    Function StartDoc(DocName As Variant) As Long
              Dim Scr_hDC As Long
              Scr_hDC = GetDesktopWindow()
              StartDoc = ShellExecute(Scr_hDC, “Print”, DocName, _
              “”, “C:”, SW_SHOWNORMAL)
              
    End Function
    
      
    Sub PrintPDFfiles()
    
    Dim zProg As String
    Dim SayWhat, Btns, BoxTitle, Answer, cell
    Dim zLastRow As Long, Temp As String
    Dim zFile As Variant, zDone As Long
    Dim zCommand As Long
    ‘FETCH NUMBER OF FILES TO PRINT..
    zLastRow = [a65536].End(xlUp).Row           ‘find last row in column [A]; e.g. 15
    Temp = “a1:a” & zLastRow                    ‘e.g. “a1:a15”
    ‘DISPLAY MESSAGE BEFORE PROCEEDING..
    SayWhat = “Did you set a Zerox printer as your default printer?”   ‘message text
    Btns = vbYesNoCancel + vbDefaultButton2     ‘message box buttons
    BoxTitle = “PRINT PDF FILES..”              ‘message box heading
    Answer = MsgBox(SayWhat, Btns, BoxTitle)    ‘display message box
    ‘CHECK IF USER WANTS TO PROCEED..
    If Answer  vbYes Then                     ‘User cancelled; did not click [Yes] button
    Exit Sub                                    ‘nothing else to do
    End If                                      ‘end of test for User cancelled
    ‘PROCEED WITH PRINTING PDF FILES..
    For Each cell In Range(Temp)                ‘loop through all entries in COLUMN [A] range
    zFile = Trim(cell.Value)                    ‘fetch filename from cell; remove any trailing spaces
    zDone = 0                                   ‘initialise counter
    If zFile Like “*.pdf” Then                  ‘make sure it is a pdf file type
        
    zCommand = StartDoc(zFile)
    ElseIf zFile Like “*.jpg” Then
        zCommand = StartDoc(zFile)
    ElseIf zFile Like “*.tiff” Then
        zCommand = StartDoc(zFile)
    ElseIf zFile Like “*.tifflist” Then
        zCommand = StartDoc(zFile)
    zDone = zDone + 1                           ‘increment counter
    End If                                      ‘end of test for pdf file type
    Application.Wait (Now + TimeValue(“0:00:05”))
    Next                                        ‘process next file in list
    ‘DISPLAY COMPLETION MESSAGE..
    SayWhat = “DONE!”
    SayWhat = SayWhat & vbCr & vbCr
    SayWhat = SayWhat & “Your files are being sent to default printer. This make take a few minutes”
    SayWhat = SayWhat & vbCr & vbCr
    Btns = vbOKOnly + vbInformation
    BoxTitle = “PRINTING ROUTINE”
    Answer = MsgBox(SayWhat, Btns, BoxTitle)
    End Sub
    
    Viewing 1 reply thread
    Author
    Replies
    • #1529265

      Can the command lines in the following thread be adapted to work on your machine?
      http://stackoverflow.com/questions/11321919/command-line-photo-printing-in-windows-7
      http://stackoverflow.com/questions/18024532/can-i-use-createobject-to-print-a-tif-document-with-vba-if-not-what

      If you can work out a command line that works it should be easy enough to code into your macro.

    • #1529349

      Thanks for your response Andrew! Unfortunately, with either one of the command lines, I still get the print dialog box. Is there any way to send the picture to print without getting the box?

      I have tried:
      Shell (“rundll32.exe & C:WindowsSystem32shimgvw.dll, ImageView_PrintTo /pt” & DocName) – but nothing prints
      PrintPicture = Shell(“cmd /c (Program Files (x86)Microsoft OfficeOffice14OIS.exe) /p ” & DocName) – but nothing prints
      DocName = ShellExecute(0&, “Print”, DocName, 0&, 0&, SW_SHOWNORMAL) – but I get the pop-up box below
      Set PrintPicture = CreateObject(“Shell.Application”)
      PrintPicture.ShellExecute “rundll32.EXE c:WindowsSystem32Simgvw.dll”, DocName, “”, “print”, 0 – but I just get an error.

      I may just be struggling with syntax. Can anyone help guide me in the right direction, please?

      42121-Capture

    Viewing 1 reply thread
    Reply To: VBA – Printing a picture using OIS.exe without the print dialog box

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: