编辑1:我正在尝试使用字符串(今天的日期)格式化段落,在VBA中右对齐,使用Excel VBA测试函数,如下所示。下面的这一行我认为是可接受的语法,但我在这里或函数中的其他地方出了问题,因为当我打开保存的文档时,日期仍显示左对齐:
wordLetter.Paragraphs(1).Alignment = wdAlignParagraphRight
此函数使用Normal模板创建新的Word文档,添加日期,相对于当前Excel工作簿位置保存日期。
否则,我理解.Paragraphs(1)
已经存在,当使用.Add
或.Open
集合的Documents
方法创建Word文档时,我才刚刚意识到这一点。
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
非常感谢任何建议。
WordLetter.Paragraphs(1).Alignment = wdAlignParagraphRight