Solved

Automating MS Word: Certain methods / properties do not work with Word not visible.

Posted on 2006-10-23
6
167 Views
Last Modified: 2010-04-30
Hi Experts,

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.  
Thanks

0
Comment
Question by:PantoffelSlippers
  • 3
  • 3
6 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 17788883
Hmm,
I have just expanded your code and tested it in VB6, but on Word 2000 which is all I have this week. It works OK for me:

Option Explicit

Private Sub Command1_Click()
    Dim WordDoc As Word.Document
    Dim WordApp As Word.Application
    Dim tbl As Word.Table
    Dim WPerc As Single
    Dim tcount As Integer
    Dim k As Integer
   
    Set WordApp = New Word.Application
    Set WordDoc = WordApp.Documents.Add
    Set tbl = WordDoc.Tables.Add(WordDoc.Range, 5, 3)
    k = 1
    tcount = 1
    WPerc = 10
    WordDoc.Tables(tcount).Columns(k).PreferredWidthType = wdPreferredWidthPercent
    WordDoc.Tables(tcount).Columns(k).PreferredWidth = WPerc
    WordApp.Visible = True
End Sub

I suppose that it is a new bug or that your Word installation has a problem. If it is the latter, you might want to try the steps in this article.

http://www.officearticles.com/word/steps_to_troubleshooting_microsoft_word.htm
0
 

Author Comment

by:PantoffelSlippers
ID: 17793803
Thanks GrahamSkan ,

I do not think that it is a bug or corrupt installation.  My reason is that I have seen this in the past a few times!  Mostly with commands like selection.moveright (when moving between cells in a word table).  Almost on all of these occasions, multiple users have this problem if not all of the users.  

I have been able to solve this in the past by changing syntax, for example selection.moveright would be replaced by syntax that references the cell that selection.moveright would have moved to directly.  Usually this is something like worddoc.tables(tcount).rows(k).cells(i).range.text = "New cell value".  If no alternative syntax can be found I just make the app visible....  

Unfortuantely in this case I am not able to find alternative syntax for adjusting column widths and I am not allowed to make the wordapp visible.

If it helps at all, I found something after my original post:  it seems that all tables on my first page of the document work well all the time but the column widths for all tables after the first page are not adjusted.  I'm not 100% sure about my first page theory but it defenitely seems as if the first part of the document always works and then after the first page or the first number of tables it stops for some reason....

Thanks for help so far - I will update the post if I learn something new and will desperately keep monitoring feedback over the next 72 hours...
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 250 total points
ID: 17794568
I've tried creating several tables and get a problem with all but the first four tables. The overall table width shrinks, but visibility of the Application doesn't affect it.
However I have used this as a guide to create this getround, which comprises moving the table to a temporary document, adjusting the size and pasting it back.

Option Explicit

Private Sub Command1_Click()
    Dim WordDoc As Word.Document
    Dim WordApp As Word.Application
    Dim tempdoc As Word.Document
    Dim tbl As Word.Table
    Dim tbl2 As Word.Table
    Dim WPerc As Single
    Dim tcount As Integer
    Dim k As Integer
    Dim t As Integer
    Dim rng As Word.Range
   
    Set WordApp = New Word.Application
    Set WordDoc = WordApp.Documents.Add
    'WordApp.Visible = True
    For t = 1 To 10
        Set tbl = WordDoc.Tables.Add(WordDoc.Bookmarks("\EndOfDoc").Range, 5, 3, wdWord8TableBehavior)
        WordDoc.Bookmarks("\EndOfDoc").Range.InsertParagraph
        WordDoc.Bookmarks("\EndOfDoc").Range.InsertBreak wdPageBreak
        tbl.Cell(1, 1).Range.Text = t
    Next t
    Set tempdoc = WordApp.Documents.Add
    k = 1
    WPerc = 10
    For tcount = 1 To 10
        Set tbl = WordDoc.Tables(tcount)
        Set rng = tbl.Range
        rng.Collapse wdCollapseEnd
        WordDoc.Bookmarks.Add "tablepos", rng
        tbl.Range.Cut
        tempdoc.Range.Paste
        Set tbl2 = tempdoc.Tables(1)
        tbl2.Columns(k).PreferredWidthType = wdPreferredWidthPercent
        tbl2.Columns(k).PreferredWidth = WPerc
        tbl2.Range.Copy
        WordDoc.Bookmarks("tablepos").Range.Paste
    Next tcount
    WordApp.Visible = True
    Set WordDoc = Nothing
    Set WordApp = Nothing
   
End Sub

0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:PantoffelSlippers
ID: 17794847
Thanks GrahamSkan ,

You say that visibility of the app has no affect.....  

I'm using the following as a temporary solution:

              WPerc = (CWidth / LvwWidth) * 100
              WordDoc.Tables(tCount).Columns(k).PreferredWidthType = wdPreferredWidthPercent
              ****************************************************
              WordApp.Visible = True
              WordApp.WindowState = wdWindowStateMinimize
              ****************************************************
              WordDoc.Tables(tCount).Columns(k).PreferredWidth = WPerc
              WordApp.Visible = False

This is not a complete solution and it is not good programming technique!  I will attempt your solution in the next few hours and post a response.

Thank you.
0
 

Author Comment

by:PantoffelSlippers
ID: 17856058
Thanks GrahamSkan,

A very strange problem to run into, wouldn't you say?

Thanks for the help.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 17857295
Thanks. It puzzles me that the problem has lasted so long.

I have just tested it in Word 2003, and it acts almost identically to the way that it acts for me in Word 2000, i.e. after a certain number of tables setting the a column width in that way causes the other columns to shrink. Again, for me, application visibility makes no difference.

The only difference between 2000 and 2003 is that  I get five 'good' tables in 2003 but only four in 2000.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now