vba将单元格写入文本文档

时间:2016-08-06 19:36:28

标签: excel vba excel-vba ms-word word-vba

我写了一个宏来写一个单词文档中的单元格信息段落和表格,问题就是当我运行宏时,单词文档中出现的第一件事就是一个表格,文本在它下面,然后是另一个表格和文本在那之下等等,问题是每个表格下方的文本需要超越它。我不确定我做错了什么,但到目前为止我的代码

Set objword = CreateObject("Word.Application")
Set objdoc = objword.Documents.Add
Set objselection = objword.Selection
Set objrange = objdoc.Content

objword.Visible = True

Set objrange = objdoc.Content
objrange.Collapse Direction:=wdCollapseend

' write paragraph
If Cells(i, 9) <> "" Then
    objselection.TypeText Text:=Mid(CStr(Cells(i, 1)), 1, 2) & "." & Mid(CStr(Cells(i, 1)), 3, 2) & "." & Mid(CStr(Cells(i, 1)), 5, 2) & "." & Mid(CStr(Cells(i, 1)), 7, 2) & " " & CStr(Cells(i, 9))
    objselection.TypeParagraph
Else
    If Cells(i, 8) <> "" Then
        objselection.TypeText Text:=Mid(CStr(Cells(i, 1)), 1, 2) & "." & Mid(CStr(Cells(i, 1)), 3, 2) & "." & Mid(CStr(Cells(i, 1)), 5, 2) & " " & CStr(Cells(i, 8))
        objselection.TypeParagraph
    Else
        If Cells(i, 7) <> "" Then
            objselection.TypeText Text:=Mid(CStr(Cells(i, 1)), 1, 2) & "." & Mid(CStr(Cells(i, 1)), 3, 2) & " " & CStr(Cells(i, 7))
            objselection.TypeParagraph
        Else
            If Cells(i, 6) <> "" Then
                objselection.TypeText Text:=Mid(CStr(Cells(i, 1)), 1, 2) & " " & CStr(Cells(i, 6))
                objselection.TypeParagraph
            End If
        End If
    End If
End If

With objselection
    .TypeText Text:=Cells(i, 6) & "|" & Cells(i, 7) & "|" & Cells(i, 8) & "|" & Cells(i, 9)
    .TypeParagraph
    .TypeText Text:=CStr(Worksheets("1").Cells(b, 11))
    .TypeParagraph
    .TypeText Text:="test"
    .TypeParagraph
End With

'write table

Set objtable = objdoc.Tables.Add(objrange, cn, 2)
objtable.AutoFormat (16)

'populate table

For cpopulate = 0 To cn - 1
    With objtable
        .Cell(cpopulate + 1, 1).Range.Text = Cells(i + cpopulate, 1)
        .Cell(cpopulate + 1, 2).Range.Text = CStr(Cells(i + cpoppulate, 10))
    End With
Next cpopulate

1 个答案:

答案 0 :(得分:0)

我猜我上面的评论太短了,无法确保你抓住我的漂移。我想说的是,在Excel中创建表格然后在Word中, 更容易(至少对我而言)。

如果您拥有所有Range()ListObject引用(在VBA中)可以快速寻址每个单元格并使用您想要的数据填充它,则尤其如此。之后,您甚至可以考虑将其列为ws.Range("A1:G20").Copy objDoc.Paragraphs(1).Range.PasteExcelTable _ LinkedToExcel:=False, _ WordFormatting:=False, _ RTF:=False 并在该表上应用一个不错的小预设。

表格完成并包含您想要的所有数据并按照您的意愿进行格式化后,您可以使用两行简单的代码轻松复制最终表格:

Option Explicit

Public Sub CopyTableToWord()

Dim ws As Worksheet

'Setup a temporary sheet
Set ws = ThisWorkbook.Worksheets.Add

'here should be your above code to fill the table with all the data that you want / need
'afterwards you may even want to format that table

'... and then you can easily copy over the final table to Word
ws.Range("A1:G20").Copy     '... or whatever the resulting range will be
objDoc.Paragraphs(1).Range.PasteExcelTable _
    LinkedToExcel:=False, _
    WordFormatting:=False, _
    RTF:=False

'... remove the temporary sheet afterwards if you wish or keep it
'    so you don't have to recreate it each time
Application.DisplayAlerts = False
ws.Delete
Application.DisplayAlerts = True

End Sub

为了让事情变得更加轻松,您可以使用&#34;临时&#34;用于此目的的工作表,一旦复制到表格后立即删除:

prop.test