[英]Writing & formatting word document using excel VBA
我正在尝试使用excel VBA编写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
我想要做的就是将“文本行1”文本居中。 我用Google搜索并尝试各种解决方案无济于事。
有什么想法吗?
更新:排序 - 它就像需要在VBA中选择的MS Word对象库引用一样简单,然后中心工作正常!
您需要设置Style的ParagraphFormat对象的Alignment属性。
wrdDoc.Styles("SHeading").ParagraphFormat.Alignment = wdAlignParagraphCenter
它必须是WdParagraphAlignment枚举之一。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.