Change style and format of tables - vba

I have code that is supposed to update the style of the tables in a Word document and then change the width to 17cm.
Sub ConvertTables()
Dim tbl As Table
For Each tbl In ActiveDocument.Tables
tbl.Style = "K2 Table"
Next
Selection.Tables(1).PreferredWidthType = wdPreferredWidthPoints
Selection.Tables(1).PreferredWidth = CentimetersToPoints(17)
End Sub
When I run the macro, it stops at the second part. Sometimes it will run in the VB viewer but never from running the macro through the Developer.
What is wrong with this piece of VBA?

Try this
Dim tbl As Table
For Each tbl In ActiveDocument.Tables
tbl.Style = "K2 Table"
tbl.PreferredWidthType = wdPreferredWidthPoints
tbl.PreferredWidth = CentimetersToPoints(17)
Next
I'm guessing the second part doesn't work when you don't have a table selected

Related

How to make regular tables in VBA Word

so I am having this problem. I want to create a table in Word using VBA , but every time I do it creates a table with this weird grey border; I want it to look like a normal table. Here is my code I am using
Sub MakeATable()
Dim actdoc As Document
Dim newtbl As Table
Set actdoc = ActiveDocument
Set myrange = actdoc.Range(0, 0)
Set newtbl = actdoc.Tables.Add(myrange, 10, 2, wdWord9Behavior)
End Sub
I tired adding in the following to get the table to be a normal style but adding it did not work either
newtbl.Style = Normal
What is so weird is that I get no problem at all with the following code
Sub MakeATable()
Dim actdoc As Document
Dim newtbl As Table
Set actdoc = ActiveDocument
Set myrange = actdoc.Range(0, 0)
actdoc.Tables.Add Range:=myrange, NumRows:=10, NumColumns:=2, DefaultTableBehavior:=wdWord9TableBehavior
Can anyone please help me or at least explain what is going on?
Thank you
In your first listing, replace
wdWord9Behavior
with
wdWord9TableBehavior

Need Script to Ignore MSword tables - probably a simple issue with syntax

I have issues with my code dealing with tables, specifically I want the code to ignore them. I do not want this code to apply to tables so I thouht that I'd use "Selection.Information(wdWithInTable) = False" to clear things up. Unfortunately I don't know how to select the paragraph that the script is currently working.
I tried putting Selection.Paragraphs(i).Range.Select in at the **** but that didn't eliminate working with the first row of a table and I don't know why. I'm new to VBA and syntax in general so I'm assuming that's the issue.
Dim prePara As Paragraph
Dim curPara As Paragraph
Dim nextPara As Paragraph
For i = 2 To ActiveDocument.Paragraphs.Count
Set prePara = ActiveDocument.Paragraphs(i - 1)
Set curPara = ActiveDocument.Paragraphs(i)
If curPara.LeftIndent <= prePara.LeftIndent And curPara.Style = "Normal" Or curPara.Style = "List Paragraph" Then
***** 'here is where I tried Selection.Paragraphs(n).Range.Select but it didn't work
If Selection.Information(wdWithInTable) = False Then
If curPara.LeftIndent < prePara.LeftIndent Then
curPara.LeftIndent = prePara.LeftIndent
End If
End If
End If
Next
With this statement:
Selection.Paragraphs(i).Range.Select
you are trying to Select a Selection object.
Try:
ActiveDocument.Paragraphs(i).Range.Select

Changing color on all but one table in Word with VBA

I have a macro that I'm using to remove all of the color in all of the tables in certain Word Documents. The colors being removed are there initially to indicate where someone should type.
Trust me, I'd rather use form fields or ActiveX text boxes, but this is not a situation where they will work as Word is being opened through a 3rd party application that invalidates these with a mail merge. Anyway, I want to skip over the first table. I have the code below set up to do it, then change the first cell of the first table back to a particular color.
Sub decolordocument()
'
' decolordocument Macro
'
'
Dim tbl As Table
For Each tbl In ActiveDocument.Tables
tbl.Shading.BackgroundPatternColor = wdColorWhite
Next
ActiveDocument.Tables(1).Cell(1, 1).Shading.BackgroundPatternColor = wdColorLightTurquoise
End Sub
This works fine for removing the color, but the color of that first cell of the first table isn't the same in all of them. I just want to skip the first table during the for each loop. I've tried an if statement (If tbl = ActiveDocument.Tables(1) Then...) but evidently this is not an allowed comparison as it doesn't recognize the Then statement. I've also tried doing this with a range, but couldn't quite get it right. Any thoughts would be appreciated.
Sub decolordocument()
'
' decolordocument Macro
'
'
Dim first as Boolean
Dim tbl As Table
first = true
For Each tbl In ActiveDocument.Tables
If first Then
first = false
Else
tbl.Shading.BackgroundPatternColor = wdColorWhite
End If
Next
'ActiveDocument.Tables(1).Cell(1, 1).Shading.BackgroundPatternColor = wdColorLightTurquoise
End Sub
if activedocument.Tables.Count >1 then
for x = 2 to activedocument.Tables.Count
activedocument.Tables(x).Shading.BackgroundPatternColor = wdColorWhite
next x
end if

Word 2010 template table generation

I'm trying to use Word 2010 to create a template for a programming project test plan. I've created a mockup template showing what I want to do.
What I'd like to be able to do is be able to click on something on the Word ribbon, and have the template generate the next test table and sequence the caption. Once the table is generated, I would fill in the table fields for the test.
Could someone tell me what to look up in the Word help or elsewhere so I can create this template?
I personally would create a macro for this or you can embed it in your template with code to add menu items and add something like the following. (It's very rough but you can use it to generate a table with your layout and numeric ascending numbers), it is not as dynamic as knowing where the previous test left off but should be a start point.)
Dim iCount As Integer
iCount = CInt(InputBox("How many tables?", "Table Count", 1))
For icurtable = 1 To iCount
Dim oTableRange As Paragraph
Dim oTable As Table
Dim oCaption As Paragraph
Set oCaption = ActiveDocument.Paragraphs.Add
Call oCaption.Range.InsertBefore(CStr(icurtable))
Set oTableRange = ActiveDocument.Paragraphs.Add
Set oTable = oTableRange.Range.Tables.Add(oTableRange.Range, 4, 1, True, True)
oTable.Rows.First.Cells(1).Range.InsertBefore ("Setup:")
oTable.Rows(2).Cells(1).Range.InsertBefore ("Test:")
oTable.Rows(3).Cells(1).Range.InsertBefore ("Expected Response:")
oTable.Rows(4).Cells(1).Range.InsertBefore ("Restore")
Call oTableRange.Range.InsertAfter(vbCrLf)
Next
In case someone else comes across this question, I'll provide my solution. I decided to create a table inside of a table so the test case number will be on the left, where people expect to see it.
Using Sacha's answer as a model, and making liberal use of the macro recorder, I came up with this VBA macro that does most of what I want.
Sub InsertTestTable()
'
' InsertTestTable Macro
' This macro inserts a test table into the document.
'
Dim oTable As Table
Dim iTable As Table
Set oTable = ActiveDocument.Tables.Add(Selection.Range, 1, 2, _
wdWord9TableBehavior, wdAutoFitContent)
Selection.TypeText ("1.")
Selection.MoveRight
Set iTable = ActiveDocument.Tables.Add(Selection.Range, 4, 2, _
wdWord9TableBehavior, wdAutoFitContent)
iTable.Rows(1).Cells(1).Range.InsertBefore ("Setup:")
iTable.Rows(2).Cells(1).Range.InsertBefore ("Test:")
iTable.Rows(3).Cells(1).Range.InsertBefore ("Expected Response:")
iTable.Rows(4).Cells(1).Range.InsertBefore ("Restore:")
iTable.Rows(1).Cells(2).Range.Select
End Sub
Now, all I need to do is format the tables the way I want, and figure out how to have the number ascend through the set of tables in the document.

Positioning a table at a specific location

I have written a macro to allow a user to select an office branch from a combo box, and now I want to insert the relevant address into the word document at a specific location. I have it using a table to hold the address, however when the table is created, it is created at wherever position the cursor just happens to be sitting at on the page.
I can't seem to find a way to tell the table to position exactly (x,y) where I need it to appear. Since there is nothing else in the document but text, there is nothing to reference to.
I am also trying to stay away from using Active X controls if at all possible.
This code will add a three column, one row table between the second and third paragraphs.
Sub InsertTable()
Dim tbl As Table
Dim pg As Paragraph
With ThisDocument
'Add a new paragraph that the table will replace
Set pg = .Paragraphs.Add(.Paragraphs(3).Range)
'Add a table in place of the new paragraph
Set tbl = .Tables.Add(pg.Range, 1, 3)
End With
tbl.Columns(1).Cells(1).Range.Text = "123 Main St"
tbl.Columns(2).Cells(1).Range.Text = "City"
tbl.Columns(3).Cells(1).Range.Text = "State"
tbl.Rows.LeftIndent = 41
End Sub
You may use this to position table both horizontally and vertically
tbl.Rows.HorizontalPosition = 150 'In points
tbl.Rows.VerticalPosition = 200
Hope that helped.
I had trouble grasping the code with the with-statement from Dick Kusleika's answer, so I'd like to share my version without the with-statement, for people like me much easier to understand:
Sub InsertTable()
Dim tbl As Table
Dim pg As Paragraph
'Add a new paragraph that the table will replace
Set pg = ThisDocument.Paragraphs.Add(ThisDocument.Paragraphs(3).Range)
'Add a table in place of the new paragraph
Set tbl = ThisDocument.Tables.Add(pg.Range, 1, 3)
tbl.Columns(1).Cells(1).Range.Text = "123 Main St"
tbl.Columns(2).Cells(1).Range.Text = "City"
tbl.Columns(3).Cells(1).Range.Text = "State"
tbl.Rows.LeftIndent = 41
End Sub