Printing Word Documents

Windows specific forum
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Printing Word Documents

Post by Karbon »

I need to whip up something really quick to print MS-Word documents. Can anyone give me some pointers about doing this in PB? I've never used the Office automation stuff in anything but VB..

Thanks!!
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Post by GedB »

Karbon,

To be honest, If you do have VB then your best off writing a dll to do it in VB and then calling it from PB.

The other alternative is to use the command line or DDE.
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

Ok, this might be a little bit of a hack, but it works :D
(requires the atl.dll to run of course)

Code: Select all

; Constants for the ExecWB Method:
#OLECMDID_OPEN              = 1
#OLECMDID_NEW               = 2
#OLECMDID_SAVE              = 3
#OLECMDID_SAVEAS            = 4
#OLECMDID_SAVECOPYAS        = 5
#OLECMDID_PRINT             = 6
#OLECMDID_PRINTPREVIEW      = 7
#OLECMDID_PAGESETUP         = 8
#OLECMDID_SPELL             = 9
#OLECMDID_PROPERTIES        = 10
#OLECMDID_CUT               = 11
#OLECMDID_COPY              = 12
#OLECMDID_PASTE             = 13
#OLECMDID_PASTESPECIAL      = 14
#OLECMDID_UNDO              = 15
#OLECMDID_REDO              = 16
#OLECMDID_SELECTALL         = 17
#OLECMDID_CLEARSELECTION    = 18
#OLECMDID_ZOOM              = 19
#OLECMDID_GETZOOMRANGE      = 20
#OLECMDID_UPDATECOMMANDS    = 21
#OLECMDID_REFRESH           = 22
#OLECMDID_STOP              = 23
#OLECMDID_HIDETOOLBARS      = 24
#OLECMDID_SETPROGRESSMAX    = 25
#OLECMDID_SETPROGRESSPOS    = 26
#OLECMDID_SETPROGRESSTEXT   = 27
#OLECMDID_SETTITLE          = 28
#OLECMDID_SETDOWNLOADSTATE  = 29
#OLECMDID_STOPDOWNLOAD      = 30

#OLECMDEXECOPT_DODEFAULT        = 0
#OLECMDEXECOPT_PROMPTUSER       = 1
#OLECMDEXECOPT_DONTPROMPTUSER   = 2
#OLECMDEXECOPT_SHOWHELP         = 3 


#Button = 1
#Web = 2

If OpenWindow(0, 0, 0, 300, 200, #PB_Window_SystemMenu|#PB_Window_Screencentered, "Print word documents...")
  If CreateGadgetList(WindowID())
  
    ButtonGadget(#Button, 5, 5, 120, 20, "Print a document")
    
    WebGadget(#Web, 0, 0, 0, 0, "")  ; nobody needs to see this ;)
    HideGadget(#Web, 1)

    ; get pointer to the WebGadget object
    WebObject.IWebBrowser2 = GetWindowLong_(GadgetID(#Web), #GWL_USERDATA)

    Repeat
      Event = WaitWindowEvent()
     
      If Event = #PB_EventGadget And EventGadgetID() = #Button
     
        File$ = OpenFileRequester("Print file...", "C:\", "MS Word Documents (*.doc)|*.doc|All Files (*.*)|*.*", 0)
        If File$ <> ""                
       
          ; load the file into the webgadget...
          SetGadgetText(#Web, File$)
          
          SetGadgetText(#Button, "Loading...")  ; put a wait message on the button :)
          While WindowEvent(): Wend
          
          ; wait until file is loaded!
          Repeat
            WebObject\get_Busy(@IsBusy.l)
            Delay(1)
          Until IsBusy = 0
          
          
          ; print the file...
          WebObject\ExecWB(#OLECMDID_PRINT, #OLECMDEXECOPT_PROMPTUSER, 0, 0)
          
          ; use this to print without asking the user:
          ; WebObject\ExecWB(#OLECMDID_PRINT, #OLECMDEXECOPT_DONTPROMPTUSER, 0, 0)
          
          ; reset wait message          
          SetGadgetText(#Button, "Print a document")
                      
               
        EndIf
     
      EndIf
     
    Until Event = #PB_EventCloseWindow
     
  EndIf
EndIf

End
Note: on the first run, the file might take a little time before printing,
because the webgadget needs to load the MS-word file display thingy first.
subsequent printings should be faster then.

Timo
quidquid Latine dictum sit altum videtur
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post by Karbon »

Very impressive!

THANK YOU!

For all the code you've given me I really owe you a beer or something.. If I'm ever near you, count on a pint(++) :-)
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
AlGonzalez
User
User
Posts: 53
Joined: Sun Feb 15, 2004 6:04 am
Location: Easley, SC, USA

Post by AlGonzalez »

You may want to look at the OpenSource AutoIt as an option:

Code: Select all

Dim $WordApp, $DocPath, $DocTitle

$WordApp = "E:\apps\Microsoft Office\Office\WINWORD.EXE"
$DocPath = "z:\Working\FVS2\Docs\"
$DocTitle = "Setup Sybase Server Service.doc"

Run($WordApp)
AutoItSetOption("WinTitleMatchMode", 2) ; Match any substring in title
WinWaitActive("- Microsoft Word")
Send("^o")                              ; Open File
Send($DocPath & $DocTitle & "{ENTER}")  ; Load file
Sleep(2000)                             ; Wait 2 seconds to load
Send("^p{Enter}")                       ; Print File
Sleep(5000)                             ; Wait 5 seconds to print
AutoItSetOption("WinTitleMatchMode", 1) ; Match partial in title from start
WinActivate($DocTitle)
WinWaitActive($DocTitle)
Send("!fx")                             ; Exit
You can run the AutoIt exe from PB or use the older version 2 that includes a dll.

The new version 3 will have a dll soon (I hope).

HTH
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post by Karbon »

Freak: Any reason this wouldn't work with the Acrobat extension and a PDF in the browser?

I get nothing...
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

I guess it is because the PDF plugin adds it's own 'print' button, and maybe
doesn't use the IE printing interface at all.
Sorry, can't help you there.

Timo
quidquid Latine dictum sit altum videtur
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post by Karbon »

Ok, thanks!
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
Post Reply