Link to home
Start Free TrialLog in
Avatar of Member_2_1348041
Member_2_1348041Flag for Ireland

asked on

Printing to Microsoft Office Document Image Writer launches the viewer

I want to create an MDI file by printing to Microsoft Office Document Image Writer. That part is easy; the VB6 code below does it quite nicely.

But there is one problem with it. The moment it creates the file ... it launches the MDI viewer. I don't want it to launch the viewer. I will be processing the file myself, through code.

How do I stop it from launching the viewer!?
For Each oPrinter In Printers
        MsgBox oPrinter.DeviceName
        If InStr(1, oPrinter.DeviceName, "Microsoft Office Document Image Writer", vbTextCompare) > 0 Then
            Exit For
        End If
Next
If Not oPrinter Is Nothing Then
        Set oWord = New Word.Application
        oWord.ActivePrinter = oPrinter.DeviceName
        Set oWordDoc = oWord.Documents.Open(strNew)
        oWordDoc.SaveAs Environ("TEMP") & "\eek.mdi"
        oWordDoc.PrintOut Range:=wdPrintAllPages, Collate:=True, PrintToFile:=True, OutputFileName:=Environ("TEMP") & "\eek.mdi"
        oWordDoc.Close False
        Set oWordDoc = Nothing
        oWord.Quit False
        Set oWord = Nothing
End If

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Antagony1960
Antagony1960

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Member_2_1348041

ASKER

I actually found a way around the problem. When you go to Word, and you manually print, and you choose the MODI printer, there is actually a tick box at the bottom after you click ok that says, imagine that, "view document image". And when you uncheck that, guess what ... the problem goes away in the code too.

HOWEVER, I've since come up against an even worse problem. The output from the .Printout call is inconsistent. I tried this with a four-page document. And the mdi file contains ... well. Sometimes one page. Sometimes two. Sometimes all four.

Oh the joy.

I suppose you don't know how to send a word document to a specified printer through a shell (command line) commannd, do you? I'm now thinking I'll just invoke it through a synchronous shell; that is, IF I can work out how to do it.
Avatar of Antagony1960
Antagony1960

Yeah, the basic shell command is useless for passing parameters. I use the ShellExecute API. See the vbAccelerator page below for details on using that. It shows how to print a document.

http://www.vbaccelerator.com/codelib/shell/shellex.htm
<shudder>

Dude, this is nuts. I made a small change and guess what, now it works! Check it out. So thankfully I don't need to try and run it as a command line argument.

Now then, all I need next is to convert a PDF to images, and then I'm laughing all the way to the bank.

P

See code:
    For Each oPrinter In Printers
        If InStr(1, oPrinter.DeviceName, "Microsoft Office Document Image Writer", vbTextCompare) > 0 Then
            Exit For
        End If
    Next
    If Not oPrinter Is Nothing Then
        sDriver = oPrinter.DeviceName
    End If
    Set oPrinter = Nothing
    
    Set oWord = New Word.Application
    Set oWordDoc = oWord.Documents.Open(strNew)
    oWord.PrintOut False, , , Environ("TEMP") & "\eek.mdi"
    oWordDoc.Close
    Set oWordDoc = Nothing
    oWord.Quit
    Set oWord = Nothing
    strNew = Environ("TEMP") & "\eek.mdi"

Open in new window

pardon.

again:
    For Each oPrinter In Printers
        If InStr(1, oPrinter.DeviceName, "Microsoft Office Document Image Writer", vbTextCompare) > 0 Then
            Exit For
        End If
    Next
    If Not oPrinter Is Nothing Then
        sDriver = oPrinter.DeviceName
    End If
    Set oPrinter = Nothing
    
    Set oWord = New Word.Application
    oWord.ActivePrinter = sDriver
    Set oWordDoc = oWord.Documents.Open(strNew)
    oWord.PrintOut False, , , Environ("TEMP") & "\eek.mdi"
    oWordDoc.Close
    Set oWordDoc = Nothing
    oWord.Quit
    Set oWord = Nothing

Open in new window

I think the main difference is that the printout is not background, forcing it to show a little dialog saying printing page 1 of ... going through the pages 1, 2, 3, 4. And so it always includes all the pages. Problem solved.

this was bizarre. A whole morning of my life that I'll never get back.
No but you've added to the wealth of your experience. ;)
Hell yeah. The School of Hard Knocks. LOL