首页 > 解决方案 > VBA 字对齐语法

问题描述

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

wordLetter.Paragraphs(1).Alignment = wdAlignParagraphRight

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

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

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

非常感谢任何建议。

标签: vbams-wordalignment

解决方案


 WordLetter.Paragraphs(1).Alignment = wdAlignParagraphRight

推荐阅读