写作与写作使用Excel VBA格式化Word文档 [英] Writing & formatting word document using excel VBA
问题描述
我正在尝试使用excel VBA编写word文档.我可以创建一个单词doc,向其中写入文本,更改样式都没有问题.我想做的是将一些文本居中,我一辈子都无法弄清楚.这是我用来编写文档的代码:
I'm trying to write a word document using excel VBA. I can create a word doc, write text to it, change styles not a problem. What I want to do is center some text, I cant for the life of me figure it out. Here is the code I'm using to write the doc:
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"文本居中.我用谷歌搜索并尝试了各种解决方案,但无济于事.
All I want to do is center the "Text Line 1" text. I have googled and tried all sorts of solutions to no avail.
那里有什么主意吗?
更新: 对其进行排序-就像需要在VBA中选择MS Word对象库引用一样简单,然后居中就可以了!
UPDATE: Sort it - it was as simple as needing the MS Word Object Library reference selected in VBA, then the centering works fine!
推荐答案
您需要设置
它必须是 WdParagraphAlignment 枚举. 这篇关于写作与写作使用Excel VBA格式化Word文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!