WD97: Timing Problem Getting Object Via OLE Automation

Last reviewed: February 11, 1998
Article ID: Q163492
The information in this article applies to:
  • Microsoft Word 97 for Windows

SUMMARY

When you use OLE automation to process calls to an application, there may be processes occurring within that application that prevent setting the OLE object until the application is free to process the request. This article includes sample Visual Basic for Applications code to demonstrate how you can use an idle loop to wait until the application is free to process the call.

MORE INFORMATION

Microsoft provides examples of Visual Basic for Applications procedures for illustration only, without warranty either expressed or implied, including, but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft support engineers can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.

The following example Visual Basic for Applications macro is run from Microsoft Excel 97. The macro calls a Microsoft Access 97 Visual Basic for Applications macro whose function is to export a query to RTF format and import into Word for Windows.

Because Word will be busy importing the RTF file, the macro then checks to see if Word is busy before it creates a Word OLE object. If it is, the macro then uses a combination of an error trap, Windows Sleep and SendMessage API calls to cycle until Word is free from the import process. Once Word has completed the importing the file, the macro creates a Word object to save the imported RTF data as a Word document.

 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: 97 word8 word97 8.0 8.0 vb vbe vba
Keywords : kbwordvba kbcode kbhowto kbinterop kbole kbprg
Version : 97
Platform : WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 11, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.