Macro to run on a Table after Merge

R

Rashid

I am using MS Word 2003/Windows XP

I have a third-party application that launches a word template for a
mail merge. Various fields from the database are inserted into
merged
document in 4 columns viz Description, Qty, Unit Price, Amount.

At present I have recorded the following macro (with two rows of data)
which I run after selecting the data under Column Heading Unit Price
and Amount.

Sub Macro1()
Selection.MoveRight Unit:=wdCharacter, Count:=3, Extend:=wdExtend
Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
End Sub


Can anyone help me with a macro which would run after the merge is
completed and make the data (only) with Column Headings Unit Price and
Amount to be right aligned please?

Pls note that the rows may vary from 1 to various rows.

Thanks in advance.


Rashid Khan
 
G

Graham Mayor

Why not format the table before running the merge? However the following
should so what you asked i.e formats columns 3 and 4 as right aligned,
ignoring the header row.

With ActiveDocument.Tables(1)
For i = 2 To .Rows.Count
For j = 3 To 4
.Cell(i, j).Range.ParagraphFormat.Alignment _
= wdAlignParagraphRight
Next j
Next i
End With

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
R

Rashid

Hi Graham,
It does not do anything..
I pasted your code after pressing Alt-F11 as follows:

Sub Test()
With ActiveDocument.Tables(1)
For i = 2 To .Rows.Count
For j = 3 To 4
.Cell(i, j).Range.ParagraphFormat.Alignment _
= wdAlignParagraphRight
Next j
Next i
End With
End Sub

What am I doing wrong?
TIA
Rashid Khan
 
G

Graham Mayor

Do you have more than one table in your document? The macro as posted works
on the first one. i.e. Tables(1)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>


Hi Graham,
It does not do anything..
I pasted your code after pressing Alt-F11 as follows:

Sub Test()
With ActiveDocument.Tables(1)
For i = 2 To .Rows.Count
For j = 3 To 4
.Cell(i, j).Range.ParagraphFormat.Alignment _
= wdAlignParagraphRight
Next j
Next i
End With
End Sub

What am I doing wrong?
TIA
Rashid Khan
 
R

Rashid

Hi Graham
Thanks for the prompt reply.
I changed the table number accordingly.
But how can I automate it. Now I have to press Alt-F11 and then run
the Macro.

Is there a possibility that it will run automatically after merge?

Thanks for your time and help
Rashid Khan
 
G

Graham Mayor

Add it to a toolbar button or include it in your merge macro code -
http://www.gmayor.com/installing_macro.htm

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>



Hi Graham
Thanks for the prompt reply.
I changed the table number accordingly.
But how can I automate it. Now I have to press Alt-F11 and then run
the Macro.

Is there a possibility that it will run automatically after merge?

Thanks for your time and help
Rashid Khan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top