Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB/VBA: Bug in Opening an Office 97 document and Fix

Status
Not open for further replies.

paoconnell

Programmer
Jun 11, 2002
41
US
This tip has also been posted in the VBA Forum.

I have a VB6 application that opens an arbitrary Word document using Microsoft Word 97, and scans the document for a keyword. Some of the documents are large and complex, and would hang at the next document manipulation statement past the "WordObj.Documents.Open" statement.

The bug is in Word 97 SR2, and may or may not be in Word 2000 or XP (they weren't tested).

A cure for this problem was found a couple of years ago by another programmer in my office when writing a similar application: force repagination before searching the document. Sample code follows.
Code:
    Dim WordObj As Object 'late binding--Office 2K
    ' compatible, but declaring the object as a Word 97 
    '"Word.Application" did not cure the bug.
    ...
    
    'Open what is presumably a Word document in sFilename
    
    '1. Start a new instance of Microsoft Word
    '--error trap in case Word isn't present
    
    On Error GoTo WordOpenError 'error handler out of range
    'of this snippet
 
    Set WordObj = CreateObject("Word.Application")
    
    'In my app we don't want the Word application to 
    'appear, so .Visible set to false.
    
    WordObj.Visible = False
    
    '2. Open sFilename with Error trap in case sFilename
    '  has problems opening.
    
    On Error GoTo SearchDocForTagOpenErr
    WordObj.Documents.Open sFilename, ReadOnly:=True,_
    Revert:=False
    
    '*** Here's the fix:
    'Let the file open completely by forcing repagination. 
    
    WordObj.ActiveDocument.Repaginate
    DoEvents
    '***
    'Go on with document processing
Hope this helps someone.

Pat O'Connell
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top