gpt4 book ai didi

excel - 使用 Excel VBA 编写和格式化 Word 文档

转载 作者:行者123 更新时间:2023-12-02 21:56:58 26 4
gpt4 key购买 nike

我正在尝试使用 Excel VBA 编写 Word 文档。我可以创建一个Word文档,向其中写入文本,更改样式都不是问题。我想做的是将一些文本居中,但我一生都无法弄清楚。这是我用来编写文档的代码:

   Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = False

Set wrdDoc = wrdApp.Documents.Add

'Set up page settings
With wrdApp.ActiveDocument.PageSetup
.Orientation = wdOrientLandscape
.TopMargin = wrdApp.InchesToPoints(0.98)
.BottomMargin = wrdApp.InchesToPoints(0.98)
.LeftMargin = wrdApp.InchesToPoints(0.98)
.RightMargin = wrdApp.InchesToPoints(0.98)
End With
'End set up page settings

With wrdDoc
.Styles.Add ("SHeading")
.Styles.Add ("StdText")

With .Styles("SHeading").Font
.Name = "Arial"
.Size = 14
.Bold = False
.Underline = True
End With
With .Styles("StdText").Font
.Name = "Arial"
.Size = 8
.Bold = False
.Underline = False
End With
End With
wrdApp.Selection.Collapse Direction:=wdCollapseEnd
wrdApp.Selection.TypeParagraph
wrdApp.Selection.Style = wrdDoc.Styles("SHeading")
wrdApp.Selection.TypeText Text:="Text Line 1"


wrdApp.Selection.TypeParagraph
wrdApp.Selection.Style = wrdDoc.Styles("StdText")
wrdApp.Selection.TypeText Text:="Text Line 2: "
wrdApp.Selection.TypeParagraph

我想做的就是将“文本行 1”文本居中。我用谷歌搜索并尝试了各种解决方案都无济于事。

有什么想法吗?

更新:对其进行排序 - 就像需要在 VBA 中选择 MS Word 对象库引用一样简单,然后居中即可正常工作!

最佳答案

您需要设置Alignment Style 的 ParagraphFormat 对象的属性。

wrdDoc.Styles("SHeading").ParagraphFormat.Alignment = wdAlignParagraphCenter

它必须是 WdParagraphAlignment 之一枚举。

关于excel - 使用 Excel VBA 编写和格式化 Word 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22569182/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com