Word VBA script behaves differently when started with "Step-in" or "Run"
This is my first attempt at writing a VBA script for Word (I'm a retired programmer who had reasonable, self-destructive success with Excel VBA). I am using Word 2013 (15.0.4641.1001 64-bit) and VBA 7.1.142.
I am trying to accomplish a simple task, which is to create a table with two columns at the current cursor position (which itself may be inside another table). I want the table to be 100% of its available width, then I want to fix the width of the first column. Basically, my code does what I want (and for the first try, I'm happy with it), but it's unstable.
The problems are related to width. If I place the cursor and "Step" into the macro, it usually (90% + of the time) does what I want. If I "run" it, it will never work correctly.
My code is below. It should do the following: -
- Create a 5 rows x 2 columns table, i.e. full width wherever it is.
- Make the first column a fixed width (accept a short fixed format string)
- Sequential number of each line and right justification of the text.
- Split column two into two lines by the original line.
The problem is action "2", the others work fine.
- If the table is full document width, action "2" in "Step in" seems to always work correctly, however
- When it is Run, action 2 always fails, making the first column 50% wide.
- If I manually create a 1 row by 2 column table and then "Stand" in a macro to create a subtable in each of its two cells:
- Action "2" for the first subheading works correctly, but
- Action "2" for the second subtable usually sets the first column to 50% instead of a fixed width.
- Sometimes action "2" sets the entire subtable to 50% of the width of its parent cell and sets both of its columns to 50% of its own width.
One thing I noticed is that when it is executed, the command
.Columns(1).PreferredWidth = 15
seems to take significantly longer than other commands.
As I said, this is my first effort, and I don't know enough yet to know if I've really forgotten a parameter somewhere.
I would be very grateful if someone can explain where I am going wrong and how this can be fixed.
Greetings
Baz
Here is my code.
Sub CreateTable()
AddRows = 5
Set NewTable = ActiveDocument.Tables.Add(Selection.Range, AddRows, 2)
With NewTable
'Display cell borders
If .Style <> "Table Grid" Then
.Style = "Table Grid"
End If
'Make table 100% wide
.PreferredWidthType = wdPreferredWidthPercent
.PreferredWidth = 100
'Make column 1 narrow
.PreferredWidthType = wdPreferredWidthPoints
.Columns(1).PreferredWidth = 15
.AutoFitBehavior (wdAutoFitFixed)
'Number each row
For a = 1 To AddRows
.Cell(a, 1).Range.InsertAfter a & ")"
.Cell(a, 1).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
Next a
'Split column 2
For a = 1 To (AddRows * 2) - 1 Step 2
.Cell(a, 2).Split NumRows:=2, NumColumns:=1
Next a
End With
End Sub
source to share
As I'm still learning, I still don't know enough about Word VBA to fully understand why one method works and the other doesn't, but here's a simplified version of the code that worked.
Sub MakeTable()
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=5, NumColumns:=2, _
DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitWindow
Selection.Tables(1).Columns(1).PreferredWidthType = wdPreferredWidthPoints
Selection.Tables(1).Columns(1).PreferredWidth = CentimetersToPoints(1)
'Number each row
For a = 1 To AddRows
Selection.Tables(1).Cell(a, 1).Range.InsertAfter a & ")"
Selection.Tables(1).Cell(a, 1).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
Next a
'Split column 2
For a = 1 To (AddRows * 2) - 1 Step 2
Selection.Tables(1).Cell(a, 2).Split NumRows:=2, NumColumns:=1
Next a
End Sub
source to share
- Make sure to be
Option Explicit
at the top of each module and the code will compile (your experience has already led you to this). - Start temporary commenting on actions in order to further fix the problem. I am wondering if the problem is
.AutoFitBehavior
. - Paste
DoEvents
to clear all those little internal buffers. As you step through the code, events are executed all the time; possibly not at runtime.
source to share