
Printing docs thru Word 97 using VB/DDE - an example
Ya-harrrrrrrr, maties! Yer ol' Pirate Pal has finally broken through
the VB/DDE - Word 97 barrier!! I now have a bit of code I'd like to
share with you -- it contains references to a couple DLL's I wrote, so
this code will not work for you, but hopefully it'll help you better
understand the DDE communication process:
======================================================
Sub PrintRecords()
' this subroutine prints documents through Word 97.
' Each document is based on a template, with a
' couple of replaceable parameter which are filled
' in based on data from an Access database (the
' recordset of which is defined in public variable
' rs.
Dim x As String, outFile As String, inFile As String
Dim bm As String, myhWnd As Long
Dim docsPrinted As Integer
On Error Resume Next
' if recordset isn't opened, bow out gracefully
bm = rs.Bookmark
If Err > 0 Then
On Error GoTo 0
Exit Sub
End If
docsPrinted = 0
On Error GoTo 0
inFile = AddPath("", "vocprint.rtf")
outFile = AddPath("", "vocword.rtf")
Status(1) = "Printing selected records"
rs.MoveFirst
Do While Not rs.EOF
RecNo = rs("RecNo")
docsPrinted = docsPrinted + 1
' read the entire file into a string
Open inFile For Binary As #1
x = Input(LOF(1), 1)
' morph replaceable parms
ReplaceString "%name", rs("Name"), x
ReplaceString "%desc", rs("Comment"), x
ReplaceString "%sources", rs("Sources"), x
ReplaceString "%num", rs("RecNo"), x
Close #1
' start with a fresh output file
If Exists(outFile) Then
Kill outFile
End If
' create the output file
Open outFile For Binary As #2
Put #2, , x
Close #2
Dim LaunchErr As Long
Dim Launching As Boolean, LaunchErrDesc As String
Launching = False
LaunchErr = 0
' prepare to initiate DDE conversation
txtDDE.LinkMode = 0
txtDDE.LinkTopic = "WinWord|System"
' open a copy of Word for 97
Shell1.ShellExecuteEx EXECUTE_BY_COMMAND_LINE, _
"""C:\Program Files\Microsoft Office\Office\Winword.exe"" /x", _
vbMinimized
' this code is to allow me to determine when Word 97 has
' finished printing my document
myhWnd = Shell1.hwnd
txtDDE.LinkMode = 2
' tell Word97 to do the following:
' a) Confirm file conversion (if any are necessary)
' b) Open our file
' c) Print the file
' d) Shut down Word 97
txtDDE.LinkExecute "[t=FileConfirmConversions()]" _
& "[FileOpen(""" & outFile & """)]" _
& "[FilePrint 0][FileConfirmConversions t][FileExit 2]"
' terminate the DDE conversation
txtDDE.LinkMode = 0
Select Case LaunchErr
' Ack, ptoo! Word is not responding!
Case 235, 282, 293
If Not Launching Then
MsgBox "Unable to launch Microsoft Word 97"
Launching = False
End If
Case 0
Launching = True
Case Else
MsgBox LaunchErrDesc, vbCritical, _
"Unable to establish DDE communications with Word97"
Launching = False
Exit Do
End Select
If Not Launching Then Exit Do
' wait until the Word window closes
Do Until IsWindow(myhWnd) = 0
DoEvents
Loop
' allow Windows Explorer ample time to
' clear the file lock imposed by
' Word 97...the time period depends
' on the number of documents printed
' also allow time for spooler to catch up
If docsPrinted Mod 5 = 0 Then
Sleep 3000 ' wait 3 extra seconds
ElseIf docsPrinted Mod 14 = 0 Then
Sleep 5000 ' wait 5 extra seconds
End If
' wait 2 seconds by default for each doc
Sleep 2000
' grab next record
rs.MoveNext
Loop
rs.Bookmark = bm
End Sub
========================================================
Hope this helps--I know what a bear it was getting all
of this stuff working right!
---------------------------------------------------------------
:) aka Jack Voltz :) Web: www.ovnet.com/~voltz
:) M A R A N A T H A ! :) Prolife: ./prolife.htm
:) S i e m p r e P o r V i d a ! :) Music: ./music.htm
;) Pour la Vie, toujours ! :) Y2K: ./y2klinks.htm
;) Sempre per la Vita! :)
;) Semper Pro Vita! :)
;) A L W A Y S P R O L I F E ! :)