Solved

Position text at an absolute position in a Word table using VBA

Posted on 2011-03-06
10
1,102 Views
Last Modified: 2012-05-11
I have a Word table with 2 columns.The row sizes vary but the column widths are fixed.  Each row has several pieces of text in the first column.  Each piece is separated by a blank line. Each text piece can be of arbitrary length. There can be any number of text pieces in the first column.

I need to insert text in the next column that lines up vertically with a specific text piece in the first column. Note that the column widths are not equal. The first column width is significantly wider than the second. Also note that not every text piece in the first column will have an associated text piece in the second column.

Below is an image that shows an example

 Example of Problem
Any help on how to align each piece of text in the second column with the associated text in the first column would be appreciated. Thanks in advance.
0
Comment
Question by:borgtamer
  • 6
  • 3
10 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 35046500
Divide the table into more rows. You can hide the new borders.
0
 
LVL 3

Accepted Solution

by:
JAMcDo earned 500 total points
ID: 35046551
It is a slight of hand trick but create the table and put each piece/item in its own cell.  That way all left and right pieces stay aligned.

Afterwards, in Table Tools, make the borders between selected rows invisible.  I have attached an example.

If you are using Word 2010 (and may works with 2070), there are table styles that can be used that make the rows nearly invisible.

J.
Table-trick.docx
0
 

Author Comment

by:borgtamer
ID: 35046559
The problem is that I do not know beforehand how many text pieces there will be in the first column. They are added dynamically at run time.

In addition, there are other columns after the second with only 1 piece of information in them.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 35046560
Here is an example HiddenRowBorders.doc
0
 

Author Comment

by:borgtamer
ID: 35046567
Can I use absolute positioning in any way to specify exactly where the text in the second column should go?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 76

Expert Comment

by:GrahamSkan
ID: 35046585
So to insert more data, you will need some code to insert a new row at the end of the first apparent row, with a hidden top border, but visible bottom border.

I'll give it a go.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 35046601
It is very difficult to do absolute positioning. You could put the text in text boxes and float them over the table, or you could add some blank paragraphs or line feeds, or possible use 'Space before' in the paragraph formatting.

I'll do the hidden row code, and see how that suits you.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 35046639
You can increase the size of the array to suit a table with more columns
Sub AddNewDataRow()
    Dim tbl As Table
    Dim rw As Row
    Dim cl As Cell
    Dim rw1 As Row
    Dim i As Integer
    Dim NewData(1) As String
    
    NewData(0) = "Some more text for the first column Some more text for the first column"
    NewData(1) = "Some more text for the second column"
    
    Set tbl = ActiveDocument.Tables(1)
    For Each rw In tbl.Rows
        If rw.Borders(wdBorderTop).LineStyle = wdLineStyleSingle And rw.Borders(wdBorderBottom).LineStyle = wdLineStyleSingle Then
            Set rw1 = tbl.Rows.Add(rw)
            rw1.Borders(wdBorderTop).LineStyle = wdLineStyleNone
            rw1.Borders(wdBorderBottom).LineStyle = wdLineStyleSingle
            
            For i = 0 To UBound(NewData)
                rw1.Cells(i + 1).Range.Text = NewData(i)
            Next i
        End If
    Next rw
End Sub

Open in new window

0
 

Author Comment

by:borgtamer
ID: 35046640
The table also has a third column with a single piece of text in it. This text can be of arbitrary length and must be kept together. It can easily span the entire height of the column. Below is another image showing the table with this row.

Example of the problem showing the 3rd column
So how do I solve the problem in light of this? Thanks
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 35047049
You can merge the cells in the third column.


HiddenRowBorders2.doc
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Recursion / Iteration in Word 2013 Mail Merge 16 61
Vbscript 8 78
Script is being strange 8 63
Add and format columns in vb6 7 46
Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now