Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

add text to line end of tables - MSWORD macro help 1

Status
Not open for further replies.

dcorleto

MIS
May 19, 2002
82
US
I have a document which contains text and tables

The tables are formatted as such:

text(paragraph mark)
text(paragraph mark)
text(paragraph mark)

I would like to write a macro to have the tables now reflect the following:

text(tab)L(tab)-(paragraph mark)
text(tab)L(tab)-(paragraph mark)
text(tab)L(tab)-(paragraph mark)

Only in the tables and not anywhere else in the document. I can do this with a find and replace but it replaces the text in the whole document. Please help me get started...If necessary I can post what I have already.

Thank you
 
As a work-around, if there is text selected, find and replace is restricted to working only within the selection.

Select each table. Do your find and replace macro.
 
The tables are formatted as such:

text(paragraph mark)
text(paragraph mark)
text(paragraph mark)

is above in ONE cell, or is above =
(Cell text), (Cell text), (Cell text)???

u want ALL tables changed?

could b use like:

Code:
Dim aTable As Table
Dim intRow As Integer, intCol As Integer
intRow = 1
intCol = 1 ' could use for colums if needed
For Each aTable In ActiveDocument.Tables
    With aTable
        .Cell(intRow, 1).Range.Text = "text"
        .Cell(intRow, 2).Range.Text = "L"
    End With
Next

increment intRow to stuff other rows

 
Thank you for your responses. The text is all contained in one cell (one column, one row). All I need the macro to do is find the table, then, within that table, add the same text to the end of each line contained in that table, then go to the next table and do the same thing, until the end of the document. I cannot believe I am having such a hard time with this. I can write a macro that finds each table. I can also write one that inserts text at the end of a line. But I cannot seem to combine the two to only insert the text in tables. Please help!
 
post your two code parts; one that finds table, other that does text....mostly one that does the text...still confused what u r trying to do.

"add the same text to the end of each line contained in that table"

do you mean at the end of each ROW? "Line" means line in ONE cell. do you mean for EACH line in EACH cell, add this text?

should quote be: "add the same text to the end of each line, IN EACH CELL, contained in that table"

as for only in tables, if u r using selection, then:

Selection.Information (wdWithinTable) checks to see if selection point in table; OR

make your text doing routine a separate sub and do something like:

Code:
dim var
selection.homekey unit:=wdstory
on error resume next
for var = 1 to activedocument.tables().count
      Selection.GoTo What:=wdGoToTable, Which:=wdGoToNext, Count:=1, Name:=""
   call [i]your separate text doing routine[/i]
next

above goes each table, but POST YOUR CODE please

 
Dear Phaed -

I have posted a Word Document to my ftp server that shows what I would like to do - both the before and after. Also, there, I have the codes that I have created which are newbie at best. If you could take a look at it, I would really appreciate it.

The address to the site is:


Thank you so much in advance
 
Code:
Sub AddText()
Dim var, var2
Dim mPara As Paragraph
Dim i As Integer
Dim r As Range
Selection.HomeKey Unit:=wdStory
On Error Resume Next
For var = 1 To ActiveDocument.Tables().Count
  Selection.GoTo What:=wdGoToTable, _
        Which:=wdGoToNext, Count:=1, Name:=""
' make range of table
' then collapse to start
  Set r = Selection.Tables(1).Range
    r.Collapse direction:=wdCollapseStart
' run through for table paragraph count
' - less end paragraph of table itself
    For var2 = 1 To (Selection.Tables(1).Range.Paragraphs.Count - 1)
        ' expand to cover paragraph, then back up 1
        ' this removes paragraph mark from range
        r.Expand Unit:=wdParagraph
        r.MoveEnd Unit:=wdCharacter, Count:=-1
        '  change the text
        r.Text = r.Text & vbTab & "L" & vbTab & "-"    '  & vbCrLf
        ' collapse to end and move start forward 1
        r.Collapse direction:=wdCollapseEnd
        r.MoveStart Unit:=wdCharacter, Count:=1
    Next  ' paragraph in range
Next  ' next table in document
set r = nothing
End Sub

try this. make sure test on document with right text...not the sample with the results already in it.
 
oh...tried above on your document...works fine.
 
only work one cell tables like ur doc; need more complex for more complex tables
 
crap..not need "dim mpara, dim i" - there from earlier version. take 'em out - using range better.
 
That's amazing - it works!!! Thank you so much....I cannot figure out the code, but I will do my best now to go through it and try to understand how it all works and learn a little more from this.

There is another wish I have in relation to this same issue, that is - I wish that I could just select some of the tables and have the macro run on them, without affecting the other tables, only the ones that I select - would that be possible too?
 
yes is possible. easy actually. depends on how you want tables identified.
 
I would think that just by multiple selecting the ones that I would want to run the macro on - using the mouse on the left side to select each table and using the Control key to hightlight more than one table at a time....

Hope this explanation is clear.

DAN
 
no. the code does NOT, repeat NOT, use selections. code runs from doc start and runs through all tables. it is automatic. does not use selection. do not USE selection. if you select something and run code it will ignore your selection, and process all tables regardless of selection. that is what you asked for. code is code and runs behind scene. this because it uses Range object.

if u wanted a macro that ONY functions on a the table you have selection point in (can can be done) then u should have said so.

it is easy to change to that, but then it would only function on the table the selction point was in. actually selecteding would be irrelevant.

macro could also function on selective tables if:

tables had identifying bookmark in them; OR
tables to be process had some sort of order (every second one for example)

you got what you asked for. and it has absolutely nothing to do with selecting anything.
 
I realize that the code does not work for selections, I was just merely asking if there was a way to select particular tables and write something that would have the same effect, but only on the selected tables. I know that would mean probably taking a completely different approach to how the script would be composed. I have been working hard to analyze each line of what Phaed wrote in order to learn more. Some I understand, some I still do not.

What was provided though is exactly what I was looking for, so once again, I thank you immensely. One day, I dream of being able to write something like that and make it work. I'll continue my studies...

 
You may try this adaptation of the code Phaed gave you:
Sub AddTextSelection()
Dim var2
Dim r As Range
On Error Resume Next
' make range of table
' then collapse to start
Set r = Selection.Tables(1).Range
r.Collapse direction:=wdCollapseStart
' run through for table paragraph count
' - less end paragraph of table itself
For var2 = 1 To (Selection.Tables(1).Range.Paragraphs.Count - 1)
' expand to cover paragraph, then back up 1
' this removes paragraph mark from range
r.Expand Unit:=wdParagraph
r.MoveEnd Unit:=wdCharacter, Count:=-1
' change the text
r.Text = r.Text & vbTab & "L" & vbTab & "-" ' & vbCrLf
' collapse to end and move start forward 1
r.Collapse direction:=wdCollapseEnd
r.MoveStart Unit:=wdCharacter, Count:=1
Next ' paragraph in range
Set r = Nothing
End Sub

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
PHV

Thank you so much for that quick response - it also works like a charm! I select a table, and it does it's thing!

Can I select two tables at the same time with this code and will it have the same effect? I tried and it seems to do just the last table. I have no idea if that was the intent, but either way, I now have two ways to do it - either the whole thing, or one table at a time....

I would love to be able to select just 4 or 5 tables non-contiguously, and have the code do its thing....

Thank you so much for your input!!!
 
problem: selection multiple non-contiguous tables returns count = 1.

select 3 contiguous tables...table count = 3
select 3 non-contiguous tables...table count = 1

select 3 contiguous, skip 1, add another...table count = 1

odd, but .... is Word.

process contiguous selected tables - no problem
For each Table in Selection.Tables
...do code...
Next

process current table - no problem, PHV good.

process non-contiguous tables..problem, as count = 1

process input by table number (order) eg, # 2, # 4, # 5, #6 - can be done. make input box and accept string "2,4,5,6"; parse string into array; process array.

possible solution make macro button on toolbar, click 4 each table want process
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top