创建一个带有标题,段落,表格,段落,文本,段落,表格,段落和文本的Word文档(来自excel)

时间:2020-10-25 17:46:00

标签: excel vba ms-word

我正在尝试从excel创建Word文档。 我想我可以做到,但是用我想要的东西(标题,空白段落,表格……无论我想要的地方)填充单词文档会让我有些头疼。我当然不掌握vba一词,但我发现excel-vba更容易。 下面是我的可怕代码,但是如果您可以按照标题中的描述给我一段代码,那对我来说可能是一个很好的起点。

    Dim objWord As Object
    Dim objDoc As Object
    Dim objRange As Object
    Dim objTable As Object
    Dim txtword As String
    '-----------------------
    Set objWord = CreateObject("Word.Application")
    objWord.Visible = True
    Set objDoc = objWord.Documents.Add
    Set objRange = objDoc.Content 'Range
    '-----------------------
    txtword = "Something"
    With objRange
        .Text = txtword & vbCr & vbCr 'add the paragraph break at the end of the text string
        .Collapse Direction:=0
    End With
    Set objTable = objDoc.Tables.Add(objRange, 2, 8)
    objTable.Borders.Enable = True
    
    
    objRange.Text = txtword & vbCr & vbCr
    objRange.Collapse Direction:=0
    
    
    ' From direct Macro:
    'objRange.MoveDown Unit:=wdLine, Count:=3
    'objRange.TypeParagraph
    'objRange.TypeText Text:="Text"
    'objRange.TypeParagraph

    
    ' HOW TO GET OUT OF THIS TABLE????
    
    
    'objRange.Text = "" & vbCr
    'Set objRange = objDoc.Content
    'objRange.Collapse Direction:=0
    objDoc.Tables.Add Range:=objRange, NumRows:=2, NumColumns:=2
    
    '---------------------
    Set objTable = Nothing
    Set objRange = Nothing
    Set objDoc = Nothing
    Set objWord = Nothing

1 个答案:

答案 0 :(得分:2)

首先在跨Office应用程序工作时停止使用后期绑定。这种方法没有优点,但有很多缺点。而是使用您需要/打算支持的最旧版本的Office编写代码。

第二,使用objDoc.Content.Paragraphs.Last.Range,您可以轻松构建文档。

第三,您应该使用样式来设置文档格式,以便可以标识哪个文本是标题等。您需要记住,在插入表格时始终使用“普通”样式以启用表格样式的正确功能,但对于其他文本,则应使用样式来描述其用途,即主体文本。

理想情况下,您将为要创建的文档类型创建一个模板,该模板包含任何标准内容,并根据需要定义所有样式。然后,您可以去上学,使用书签充当文本的占位符,也可以使用内容控件。

Sub CreateWordDoc()
   Dim wdApp As Word.Application
   Dim wdDoc As Word.Document
   Dim wdTable As Word.Table
   Dim quitWord As Boolean
   Dim txtword As String
   '-----------------------
   On Error Resume Next
   Set wdApp = GetObject(, "Word.Application")
   If Err Then
      'Word wasn't already running so create a new instance of MS Word
      Err.Clear
      Set wdApp = New Word.Application
      If Err Then
         MsgBox "Microsoft Word not installed"
         Exit Sub
      Else
         'as Word wasn't already open set flag to show Word needs to be shut down
         'and make application visible
         quitWord = True
         wdApp.Visible = True
      End If
   End If
   'clear error stack and reset error handling
   On Error GoTo 0
   Set wdDoc = wdApp.Documents.Add
   '-----------------------
   txtword = "Something"
   With wdDoc.Content
      .Paragraphs.Last.Range.Text = txtword & vbCr & vbCr 'add the paragraph break at the end of the text string
      .Paragraphs.First.Style = wdStyleHeading1
      Set wdTable = wdDoc.Tables.Add(Range:=.Paragraphs.Last.Range, NumRows:=2, NumColumns:=8)
      wdTable.Borders.Enable = True
    
      With .Paragraphs.Last.Range
         .Style = wdStyleBodyText
         .Text = vbCr & txtword & vbCr
      End With
      .Paragraphs.Last.Range.Style = wdStyleNormal
      Set wdTable = wdDoc.Tables.Add(Range:=.Paragraphs.Last.Range, NumRows:=2, NumColumns:=2)
      wdTable.Borders.Enable = True
      With .Paragraphs.Last.Range
         .Style = wdStyleBodyText
         .Text = vbCr & txtword
      End With
   End With
   wdDoc.SaveAs2 "Full path and filename"
   '---------------------
   If quitWord Then wdApp.Quit
   Set wdTable = Nothing
   Set wdDoc = Nothing
   Set wdApp = Nothing
End Sub