创建一个word文档(来自excel),有一个标题,一个段落,一个table,一个段落,一个正文,一个段落,一个table,一个段落和一个正文

Create a word document (from excel) with a title, a paragraph, a table, a paragraph, a text, a paragraph, a table, a paragraph and a text

我正在尝试从 excel 创建一个 word 文档。 我想我可以做到,但是用我想要的东西(一个标题,一个空段落,一个table ...我想要的任何地方)填充word文档让我有些头疼。我当然不掌握 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

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

其次,使用 objDoc.Content.Paragraphs.Last.Range 构建文档不会有任何困难。

第三,您应该使用样式来格式化您的文档,以便您可以识别哪个文本是标题等。您需要记住在插入 tables 时始终使用普通样式以启用正确的table 样式的功能,但对于其他文本,您应该使用名称描述其用途的样式,即 Body 文本。

理想情况下,您会为您正在创建的文档类型创建一个模板,其中包含任何标准内容和您想要定义的所有样式。然后,您可以选择老派并使用书签作为文本的占位符或使用内容控件。

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