VBA字对齐语法

时间:2019-03-06 14:34:12

标签: vba ms-word alignment

编辑1: 我正在尝试使用如下所示的Excel VBA测试功能在VBA中使用带有右对齐的字符串(今天的日期)格式化段落。我认为下面的这一行是可接受的语法,但是在此或函数中的其他地方出现了问题,因为打开保存的文档时日期仍显示为左对齐:

wordLetter.Paragraphs(1).Alignment = wdAlignParagraphRight

此功能使用Normal模板创建新的Word文档,添加日期,并相对于当前Excel工作簿位置保存日期。

否则,我知道使用.Paragraphs(1)集合的.Add.Open方法创建Word文档时,Documents已经存在,我只是意识到这一点。

Private Function Test()

    Dim objWord As Object: Set objWord = CreateObject("Word.Application")

    objWord.Application.DisplayAlerts = False

    objWord.Application.ScreenUpdating = False

    Dim wordLetter As Object: Set wordLetter = objWord.Documents.Add

    wordLetter.Range.Font.textColor.RGB = RGB(0, 0, 0)

    Dim strDate As String: strDate = Format(Now(), "dddd, mmm d, yyyy")

    wordLetter.Paragraphs(1).Alignment = wdAlignParagraphRight
    wordLetter.Paragraphs(1).Range.text = strDate

    objWord.Application.ScreenUpdating = True

    savePath = ThisWorkbook.path & "\testDoc.docx"

    With wordLetter
        .SaveAs2 Filename:=savePath, FileFormat:=wdFormatDocumentDefault
    End With

    Application.DisplayAlerts = True

End Function

非常感谢任何建议。

1 个答案:

答案 0 :(得分:1)

 WordLetter.Paragraphs(1).Alignment = wdAlignParagraphRight