【发布时间】:2014-04-29 10:27:16
【问题描述】:
我正在尝试使用 excel VBA 编写一个 word 文档。我可以创建一个 word 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
我想要做的就是将“Text Line 1”文本居中。 google了一下,各种办法都试过了,都没用。
有什么想法吗?
更新: 排序 - 就像需要在 VBA 中选择 MS Word 对象库引用一样简单,然后居中工作正常!
【问题讨论】:
-
尝试在 Word 中居中现有文本的同时录制宏,看看 Word 使用什么。