写作&使用excel VBA格式化word文档

时间:2014-03-21 20:44:04

标签: excel vba ms-word

我正在尝试使用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

我想要做的就是将#34;文本行1和#34;放在中心位置。文本。我用Google搜索并尝试了各种解决方案无济于事。

有任何想法吗?

更新: 对它进行排序 - 就像需要在VBA中选择的MS Word对象库引用一样简单,然后中心工作正常!

1 个答案:

答案 0 :(得分:1)

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

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

它必须是WdParagraphAlignment枚举中的一个。