我正在尝试使用 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 对象库引用一样简单,然后居中就可以了!
您需要设置结盟 http://msdn.microsoft.com/en-us/library/office/ff197202%28v=office.15%29.aspxStyle 的 ParagraphFormat 对象的属性。
wrdDoc.Styles("SHeading").ParagraphFormat.Alignment = wdAlignParagraphCenter
它必须是其中之一Wd段落对齐 http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.wdparagraphalignment%28v=office.11%29.aspx枚举。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)