Article ID: 163492
Article Last Modified on 1/19/2007
Private Declare Sub SleepAPI Lib "Kernel32" Alias _
"Sleep" (ByVal dwMS As Long)
Private Declare Function SendMessage Lib "User32" Alias _
"SendMessageA" (ByVal hWnd As Integer, ByVal msg As Integer, _
ByVal wp As Integer, lp As Any) As Long
Sub ExportDataToWord()
' Set Access variables.
Dim objAccess As Object
Set objAccess = CreateObject("Access.Application")
' Run Access macro.
With objAccess
.OpenCurrentDatabase ThisWorkbook.Path & "MyData.mdb"
.Visible = True
.Run "ExportQueryToRTF"
.Quit
End With
' Set Word variables.
Dim objWord As Object
Dim cTries As Integer
' Create Word Object.
' If Word is busy, then jump to the error
' trap to cycle until Word is free.
On Error GoTo WAITFORWORD
Set objWord = GetObject(, "Word.Application")
On Error GoTo 0
' Save the document and free the Word Object.
With objWord
.Visible = True
.ActiveDocument.SaveAs FileName:=ThisWorkbook.Path & "\MyData.doc"
.Quit ' "Microsoft Word"
End With
' Clean up
Set objWord = Nothing
Exit Sub
WAITFORWORD: ' <--- This line must be left aligned.
' Force Word to register Application and Basic object.
SendMessage -1, 61, 0, 0
' Loop 25 times until Word is free
' waiting 2 seconds between tries.
If cTries < 25 Then
cTries = cTries + 1
Sleep 2 ' wait 2 seconds
Resume
Else
MsgBox "Word is taking too long. Process ended."
End If
End Sub
Sub Sleep(nSec As Integer)
' Call the Sleep API to wait the number
' of seconds specified by 'nSec'.
SleepAPI nSec * 1000
End Sub
Additional query words: wordcon 97 word8 word97 8.0 vb vbe vba
Keywords: kbprogramming KB163492