在Excel中为Word文件创buildVBA中的Tabstops

我需要通过Excelmacros将两个不同的文本部分自动插入到新生成的文件中。 这是我的代码的一个例子。

首先,我清除所有现有的选项卡,以获得一个干净的文档,然后我想把“这个文本应该在左边alignment”和左边alignment,“这个文本应该在右边alignment”在同一行右边。

.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 

使用这个命令,我设法在Word中生成一个Tabstop,但是我似乎无法定位它,或者使“这个文本应该在右边alignment”alignment到右边。

最后它应该是这样的:

这两个文本部分应由VBA生成并按此方式alignment。

图片

下面的代码为你工作?

 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