Automating MS Word: Certain methods / properties do not work with Word not visible.
Posted on 2006-10-23
I'm building a Word document using Word 2003 and Visual Basic 6 (Word 11 OLB). The code just builds tables in the word document based on SQL Server tables and grids on a font end application. This is very easy but there are 2 complications:
1. The tables need to be formatted for users to view (fonts, borders and especially column widths).
2. The documents need to build in the background. Word must not be visible.
The problem is: it seems that certain methods and properties of Word.Application and Word.Document do not work if word is not visible. I am specifically referring to code that adjusts column widths in the word tables.
The code extract is simple:
WordDoc.Tables(tCount).Columns(k).PreferredWidthType = wdPreferredWidthPercent
WordDoc.Tables(tCount).Columns(k).PreferredWidth = WPerc (Dim WPerc as Single)
When I make the word application visible, they all work perfectly fine all the time. When word is not visible, some of them work sometimes... If word is not visible but I am stepping through the code in debug mode, it also seem to work just fine! This is very strange to me - please help.
Hopefully some you have seen this before.