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
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
I'm very new to VBA. I review reports in MS Word from my co-workers on a daily basis and need to make sure that all instances of table borders of 1/2pt be converted to 3/4pt.
There are also instances of thicker border weights up to 1 1/2pt. But I only need to make sure that the minimum weight is 3/4pt.
This is my code so far (that I modified from this post) but it applies the weight to ALL BORDERS and only seems to work for one table in each document.
Sub BorderWeight()
Dim myTable As Table
Dim r As Variant
Set myTable = ThisDocument.Tables(1)
For Each r In myTable.Rows ' <-- loop through all rows in table
r.Borders(wdBorderBottom) = wdLineWidth075pt
Next r
End Sub
Any help is much appreciated.
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
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
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.