仅将包含Excel数据的单元格复制到Word

时间:2017-11-08 14:20:02

标签: excel vba excel-vba ms-word

我需要一些进一步的帮助来开发我的代码。我现在已经掌握了基础知识并提供了一些早期的帮助,但我不确定下一部分。

我正在设计的代码将在电子表格中运行,表单中使用的行数将根据使用的数据量而有所不同。 (由于商业性质和挪威法律,我不能详细介绍。)

我希望将B5:B1000范围作为标准范围,并且只将包含数据的单元格自动填充到模板中,但我不确定如何编写所述代码。有人可以告诉我如何能够解决这个问题吗?

与此相关的所有问题均基于从一张纸复印到另一张。

到目前为止,这是我在excel中使用的代码:

Sub CopyRangeToWord()
Dim objWord
Dim objDoc
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Open("C:\Users\CoffeeFuelsMeNow\Documents\Custom Office Templates\EVERYTHING IS AWESOME.dotx")

Range("B5:B92").Copy


With objDoc.Paragraphs(objDoc.Paragraphs.Count).Range
   'All formatting goes here
   .PasteAndFormat (wdFormatPlainText)
End With
objWord.Visible = True

End Sub

1 个答案:

答案 0 :(得分:0)

这假设列 B compact (数据中没有漏洞):

Sub CopyRangeToWord()
    Dim objWord, N As Long
    Dim objDoc
    Set objWord = CreateObject("Word.Application")
    Set objDoc = objWord.documents.Add

    N = Cells(Rows.Count, "B").End(xlUp).Row
    Range("B5:B" & N).Copy


    With objDoc.Paragraphs(objDoc.Paragraphs.Count).Range
       'All formatting goes here
       .PasteAndFormat (wdFormatPlainText)
    End With
    objWord.Visible = True

End Sub
相关问题