Jean-Guy
Fri Jun 02 10:58:54 CDT 2006
damezumari was telling us:
damezumari nous racontait que :
> I use vb6, win2000, and word2003.
>
> I am creating a word document in vb.
>
> After I write the data to the document I put it into a table:
>
> oRange.start = WordDoc.Range.start
> oRange.ConvertToTable numcolumns:=IIf(choice, 4, 1), AutoFit:=True
>
> I want to have the second column right-aligned. This code does not
> work:
>
> tbl.Columns(2).Select
> Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
>
> It works as a macro in Word on the final document, but not from vb.
>
> This code works, but is slow:
>
> Dim cl As Cell
> For Each cl In tbl.Columns(2).Cells
> cl.Range.ParagraphFormat.Alignment = wdAlignParagraphRight
> Next cl
>
> Regards,
>
> Jan Nordgreen
This works for me:
'_______________________________________
Option Explicit
'_______________________________________
Sub Test()
Dim rgeDoc As Range
Dim docWord As Document
Dim tbl As Table
Dim boolChoice As Boolean
Dim rgeStart As Range
Set rgeStart = Selection.Range
Set docWord = ActiveDocument
Set rgeDoc = docWord.Range
With rgeDoc
If .Paragraphs(1).Range.Words.Count = 8 Then
boolChoice = True
Else
boolChoice = False
End If
.Start = docWord.Range.Start
.ConvertToTable numcolumns:=IIf(boolChoice, 4, 1), AutoFit:=True
End With
Set tbl = docWord.Tables(1)
With tbl
.Columns(2).Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
End With
rgeStart.Select
End Sub
'_______________________________________
You wrote that you were executing the code from vb... Could it be that your
doc is hidden/invisible while executing the code? The Selection object works
in funny ways with invisible documents...
In fact, it is better to avoid it altogether.
It is something better to go for slower code and use a range object (as in
your "Dim cl As Cell" alternative). Of course, if the table is massively
long and makes the code run so slow that it is unbearable, then you have to
use the Selection object.
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site:
http://www.word.mvps.org