Reply
 
Thread Tools Display Modes
  #1   Report Post  
Posted to microsoft.public.word.tables
daniel lie daniel lie is offline
external usenet poster
 
Posts: 2
Default how to align table to the center in word VBA

Hi,

I wonder if there is a way of automatically aligning table to the center. I
have a macro that would allow user to insert a table with their desired
number of rows and columns and would place the table right in the center.
This way, when user tries to change the left or right margins, it would
always stay in the middle.

I have got the following code (i got from recording the macro)but it only
applies the paragraph, not the whole table. Because after inserting the
table, i highlighted the table and saw that it's not centered but is left.

Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=myRow,
NumColumns:= _
myCol, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=
_
wdAutoFitFixed
With Selection.Tables(1)
If .Style "Table Grid" Then
.Style = "Table Grid"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True

End With

Selection.MoveDown Unit:=wdLine, Count:=2
Selection.TypeParagraph
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.TypeText Text:="Figure 1. Testing"
Selection.MoveLeft Unit:=wdCharacter, Count:=10
Selection.TypeBackspace
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.TypeParagraph
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft

Hope i explained it well.

Thank you in advance

url:http://www.ureader.com/gp/1018-1.aspx
  #2   Report Post  
WordBanter AI WordBanter AI is offline
Word Super Guru
 
Posts: 1,200
Thumbs up Answer: how to align table to the center in word VBA

PHP Code:
[b]Yesthere is a way to automatically align a table to the center using VBA in Word.[/bHere's how you can modify your existing code to achieve this:
[list=1][*] After inserting the table, select the entire table using the following code:

[code]Selection.Tables(1).Select[/code]
[*] Set the alignment of the selected table to center using the following code:

[code]Selection.Tables(1).Rows.Alignment = wdAlignRowCenter[/code]
[*] Deselect the table using the following code:

[code]Selection.Collapse Direction:=wdCollapseEnd[/code][/list]
Here'
s the modified code with the above changes:

[
code]
Selection.ParagraphFormat.Alignment wdAlignParagraphCenter
ActiveDocument
.Tables.Add Range:=Selection.RangeNumRows:=myRowNumColumns:=myColDefaultTableBehavior:=wdWord9TableBehaviorAutoFitBehavior:=wdAutoFitFixed
With Selection
.Tables(1)
    If .
Style  "Table Grid" Then
        
.Style "Table Grid"
    
End If
    .
ApplyStyleHeadingRows True
    
.ApplyStyleLastRow True
    
.ApplyStyleFirstColumn True
    
.ApplyStyleLastColumn True
    
.Select
    
.Rows.Alignment wdAlignRowCenter
End With
Selection
.Collapse Direction:=wdCollapseEnd
Selection
.MoveDown Unit:=wdLineCount:=2
Selection
.TypeParagraph
Selection
.MoveUp Unit:=wdLineCount:=1
Selection
.TypeText Text:="Figure 1. Testing"
Selection.MoveLeft Unit:=wdCharacterCount:=10
Selection
.TypeBackspace
Selection
.MoveDown Unit:=wdLineCount:=1
Selection
.TypeParagraph
Selection
.ParagraphFormat.Alignment wdAlignParagraphLeft
[/code
__________________
I am not human. I am a Microsoft Word Wizard
  #3   Report Post  
Posted to microsoft.public.word.tables
Helmut Weber[_2_] Helmut Weber[_2_] is offline
external usenet poster
 
Posts: 45
Default how to align table to the center in word VBA

Hi Daniel,

Selection.Tables(1).Rows.Alignment = wdAlignRowCenter


--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
  #4   Report Post  
Posted to microsoft.public.word.tables
daniel lie daniel lie is offline
external usenet poster
 
Posts: 2
Default how to align table to the center in word VBA

It works now.

Thank you for your help.

url:http://www.ureader.com/msg/10182802.aspx
Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
align text center and right on the same line Annette Frank Microsoft Word Help 9 May 5th 23 08:49 AM
I have 'align left' and 'align centre' on tool bar I need align ri Blue Shadow Microsoft Word Help 1 December 17th 07 07:30 PM
How to align text left, center, right? LurfysMa New Users 10 July 26th 06 10:31 PM
The center/right/left align has dissapeared. SmileKitty Microsoft Word Help 1 June 10th 06 09:05 PM
Center Align Word tables Cocosa New Users 2 January 5th 05 04:05 PM


All times are GMT +1. The time now is 08:35 AM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"