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.
Related
I have been using google for a while and finally decided to ask here for help.
Basically I have a word documents with a couple of tables that is being used by different people. Each table has a VBA button "add new row". Click on a button and a userform is filled out. Userform populates the table. Only problem left is there are multiple tables and userforms and they populate the wrong table.
How can I populate the right tables with userforms?
Googling helped me that bookmarks are needed. I set a bookmark called
Table and Bookmark
Userform
Notation/Names
Code for clicking add in userforms:
Private Sub CommandButtonAdd_Click()
Dim table2 As table
Dim newRow As Row
Set table2 = ActiveDocument.Tables(1)
Set newRow = table2.Rows.Add
newRow.Cells(1).Range.Text = TextBoxFunction.Text
newRow.Cells(2).Range.Text = TextBoxName.Text
End Sub
First time using VBA, credits go to this person: https://www.youtube.com/watch?v=9cZ_XWzQZX0
Many many thanks for your help.
You choose the table by index. If you have any mask you can recognize the correct table, you can try to check all the tables and recognize the right one with loop
Dim tempT as Variant
Dim count as long
count=0
For Each tempT in ActiveDocument.Tables
count=count+1
if ActiveDocument.Tables(count) 'here some condition to recognize the corrct table ' Then
Set newRow = table2.Rows.Add
'etc your code
End If
Next tempT
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
I have a word document containing a table I need to use as mailmerge datasouce.
I've reached my goal by copying data of the word table to excel and using the sheet as datasource.
Now my question is: is there a way to use the word table as datasource (I mean create a datasource using the data in the word table) without calling excel?
I'm asking this because I need to use a word-vba macro and I'm trying to avoid opening an excel instance.
edit
I'll try to clarify what I'm looking for with the following pseudocode (I added a "What for this?" in wrong code lines):
Dim mDoc As Document
Dim mTbl As Table
Set mDoc = ActiveDocument
Set mTbl = mDoc.Tables(1)
Dim mDS As MailMerge.DataSource 'What for this?
For C = 1 To mTbl.Columns.Count
'Add Fields
Dim FieldName As String
FieldName = mTbl.Cell(1, C).Range.Text
mDS.Fields.Add (FieldName) 'What for this?
'Add Values
For R = 2 To mTbl.Rows.Count
mDS.Fields(FieldName)(R) = mTbl.Cell(R, C).Range.Text 'What for this?
Next R
Next C
In Word, start with a blank document, then add one table. Enter the category in the top row (i.e. First Name, Last Name, etc). Then add the data in the rows below. Then you should be able to use a statement like this to connect:
ActiveDocument.MailMerge.OpenDataSource Name:="Y:\Test\Word\MailmergeSource.docx", LinkToSource:=True, SubType:=wdMergeSubTypeOther
very new to VBA but our clients want the all the data in 1,850 pages of Word Tables aligned right. I'm thinking this is pretty easy in VBA. I am trying to figure it out and I'm sure I could nail it on my own, but a deadline is forcing me to seek help. So I apologize in advance if I missed a published solution.
As an example they want this:
To be this:
So i've got:
Dim oTable As Table
Dim oRow As Row
For Each oTable In ActiveDocument.Tables
For Each oRow In oTable.Rows
But I don't know how to loop through just the body of the table. Also the top 4 rows (table title) is merged to one cell and the first column is still left aligned. Help, and the next rounds on me :)
Normally I'm not a huge fan of "please write code for me" but I've not done enough with VBA in Word and want to learn some myself.
This is going to get you most of the way there.
You do not currently provide enough information to allow me to guarantee the if statement is workable for the entire document but you should be able to go from here.
Sub alignTableElementsRight()
Dim oTable As Table
Dim oRow As Row
Dim i As Integer
Dim dataTable As Boolean
For Each oTable In ActiveDocument.Tables
'this will be set once you are in the "table" part and
'not headings
dataTable = False
For Each oRow In oTable.Rows
'you will need custom logic here to determine what your if statement
'is to properly execute on the right row, this is going to depend based on your table
'format, etc. This checks if a leftmost column heading is "65 to 66"
If (InStr(oRow.Cells(1).Range.Text, "65 to 66") > 0) Then
dataTable = True
End If
'if you are in the datatable, move all values to align right in each row following
If (dataTable = True) Then
For i = 2 To oRow.Cells.Count
oRow.Cells(i).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
Next i
End If
Next oRow
Next oTable
End Sub
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