Userform populates wrong table in word document - vba

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

Related

Changing Visible Word borders via VBA from 1/2pt to 3/4pt

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.

How to use a word table as mailmerge datasource

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

Excel, cycle through columns and rows

I have an excel table that calculates how many materials we need to order per job we are doing.
What I want to accomplish is to create a button that will copy the existing table on to page 2 of my excel workbook. The table is two columns wide. Therefore I would like to click the button, it copies the table thats on A and B of page 1 onto page 2 A and B. After having done so, I want excel to remember A and B are taken and next time I click the button, it goes in column C and D, then E and F and so on (always on page 2).
to explain why I would like this, I need to order 1000 sq ft of materials, but in sections. only 250 sq ft of materials at a time. So, I want to fill out how much im ordering, click the button and it saves the information of what was ordered on page 2. That way its easy to go back and see what was done, what date, how much was ordered, what is left to order, etc.
I know with numbers it would be easy by just incrementing i by two at every button click, but I dont know how to go about doing this for the excel columns (letters).
Is there some easy command I just cannot find online to do this sort of thing ?
Thanks for your help !
What you should learn in VBA and modify in this code below is how to determine the actual range of the table on Sheet1 -- instead of hard-coding it to "A1:B10"
UPDATE
Not sure why the previous version didn't work for you, but I've updated the code below to store the next column location in a helper cell to see if that works for you.
Option Explicit
Sub Button1_Click()
Dim srcTable As Range
Dim dstTable As Range
Dim nextLoc As Range
Set srcTable = Sheets("Sheet1").Range("A1:B10")
Set nextLoc = Sheets("Sheet2").Range("A20")
'--- if this is your first copy, then cell A1 should be empty
If IsEmpty(Sheets("Sheet2").Range("A1").Value) Then
Set dstTable = Sheets("Sheet2").Range("A1:B10")
nextLoc.Value = 3 'next location is column 3
Else
'--- we're adding the next table...
Set dstTable = Sheets("Sheet2").Cells(1, nextLoc.Value)
nextLoc.Value = nextLoc.Value + 2
End If
srcTable.Copy dstTable
End Sub

Align certain cells in Word table to the right with VBA/Macro

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

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.