Reputation: 39
This is my first attempt at writing a VBA script for Word (I'm an ex-programmer who's had reasonable, self-taught, success with Excel VBA). I'm using Word 2013 (15.0.4641.1001 64-bit) and VBA 7.1 .142.
I'm trying to perfom a simple task, which is to create a two-column table at the current cursor position (which itself can be inside another table). I want the table to be 100% of the width available to it, then I want to fix the width of the first column. Essentially, my code does what I want (and for a first attempt, I'm reasonably pleased with it), but it's erratic.
The problems relate to the widths. If I position the cursor and "Step-into" the macro, it normally (90%+ of the time) does what I want. If I "Run" it, it never works correctly.
My code is given below. What it's supposed to do is this:-
The problem is with action "2", the others work ok.
One thing I have noticed is that when it executes, the command
.Columns(1).PreferredWidth = 15
seems to take noticably longer than other commands.
As I said, this is my first effort and I don't yet know enough to be aware of whether or not I've overlooked some parameter somewhere.
I'd be really grateful if somebody can explain where I'm going wrong and how it can be fixed.
Cheers
Baz
Here's 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
Upvotes: 3
Views: 943
Reputation: 39
As I'm still learning, I don't yet 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
Upvotes: 1
Reputation: 7019
Option Explicit
is at the top of every module, and code compiles (your experience has prolly already guided you on this). .AutoFitBehavior
is an issue.DoEvents
to help all those little internal buffers to clear. When you step through code, Events are Doing all the time; maybe not during runtime. Upvotes: 1