使用excel VBA编写和格式化word文档

我正在尝试使用Excel VBA编写一个Word文档。 我可以创build一个单词文档,写入文本,改变样式不是一个问题。 我想要做的是围绕一些文字,我不能为我的生活弄清楚。 这里是我用来编写文档的代码:

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了,并尝试各种解决scheme无济于事。

有任何想法吗?

更新:对其进行sorting – 就像需要在VBA中select的MS Word对象库引用一样简单,然后居中工作正常!

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

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

它必须是WdParagraphAlignment枚举之一。