Reputation: 11
I need to insert two different text parts automatically into a newly generated file through an Excel Macro. This is an example of my code that I have.
First I clear allthe existing tabs to have a clean document, then I want to put "This text should be aligned on the left" aligned to the left and "This text should be aligned on the right" aligned to the right on the same line.
.Content.Paragraphs.TabStops.ClearAll
.Content.InsertAfter "This text should be aligned on the left"
.Content.Paragraphs.TabStops.Add Position:=NewPos, _
Alignment:=wdAlignTabRight, _
Leader:=wdTabLeaderLines
.Content.InsertAfter "This text should be aligned on the right"
.Content.InsertParagraphAfter
With this command I manage to generate a Tabstop in Word, but I don't seem able to position it, or to make "This text should be aligned on the right" align to the right.
In the end it should look like this:
The two text parts should be generated by VBA and aligned this way.
Upvotes: 1
Views: 2860
Reputation: 556
Is below code working for you?
Sub AlignLeftAndRight()
Selection.ParagraphFormat.TabStops.Add Position:=CentimetersToPoints(16) _
, Alignment:=wdAlignTabRight, Leader:=wdTabLeaderSpaces
Selection.TypeText Text:="This text should be aligned on the left"
Selection.EndKey Unit:=wdLine
Selection.TypeText Text:=vbTab & "This text should be aligned on the right"
End Sub
Upvotes: 2