1

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

首先,我清除所有现有选项卡以获得干净的文档,然后我想将“此文本应左对齐”与左对齐和“此文本应右对齐”在同一行上右对齐.

.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,但我似乎无法定位它,或者使“此文本应在右侧对齐”与右侧对齐。

最后它应该是这样的:

这两个文本部分应由 VBA 生成并以这种方式对齐。

图片

4

1 回答 1

2

下面的代码对你有用吗?

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
于 2014-07-23T11:29:32.867 回答