vba 如何将带有代码的表格样式分配给word文档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11526778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 16:55:00  来源:igfitidea点击:

how to assign table styles with code to a word document

vbams-wordword-style

提问by user1500031

I'm creating a word document with data from a Visual Basic 2010 software I created for my work, which consist in a report ... i was ask to generate a Microsoft word document, so now I'm creating a table and filling that table with data, something likes this

我正在使用我为我的工作创建的 Visual Basic 2010 软件中的数据创建一个 Word 文档,其中包含一个报告......我被要求生成一个 Microsoft Word 文档,所以现在我正在创建一个表格并填充它带有数据的表,像这样

oTable = oDoc.Tables.Add(oDoc.Bookmarks.Item("\endofdoc").Range, 8, 4)
oTable.Range.ParagraphFormat.SpaceAfter = 6
oTable.Range.Font.Size = 10

oTable.Rows.Item(1).Range.Font.Bold = True
oTable.Rows.Item(1).Range.Font.Italic = True
oTable.Cell(1, 1).Range.Text = "Datos de Facturación:"
oTable.Cell(1, 3).Range.Text = "            Enviar a:"
oTable.Cell(2, 1).Range.Text = rs.Text
oTable.Cell(2, 1).Width = 75
oTable.Cell(3, 1).Range.Text = dirfa.Text
oTable.Cell(3, 1).Width = 75 ..... etc..

Microsoft Word has some table designs styles like "DARK LIST - ACCENT 5". "DARK LIST - ACCENT 6" etc, I couldn't figure out how to set this styles to the table, is it possible?

Microsoft Word 有一些表格设计样式,例如“DARK LIST - ACCENT 5”。“DARK LIST - ACCENT 6”等,我不知道如何将这种样式设置到表格中,这可能吗?

回答by Fionnuala

To create a style, you can use a document object:

要创建样式,您可以使用文档对象:

Set doc = wd.Documents.Add(NewTemplate:=True)

With doc.Styles("Certificate")
    With .Font
        .Name = "Arial"
        .Size = 12
        .Italic = True
        .Bold = True
    End With

    With .ParagraphFormat
        ''wdAlignParagraphCenter = 1
        .Alignment = 1
        .SpaceAfter = 0
        .SpaceBefore = 0
    End With
End With

Assign the style:

指定样式:

Set r = doc.Shapes("Course1").TextFrame.TextRange
r.Style = "Certificate"

For this particular case, you might use:

对于这种特殊情况,您可以使用:

    oTable.Range.Style = "ANewStyle"

Or if built-in styles are available to you:

或者,如果您可以使用内置样式:

    oTable.Rows.Item(1).Range.Style = WdBuiltinStyle.wdStyleHeading1