如何将excel复制/粘贴桌复制到Word文档

发布于 2025-01-31 11:36:52 字数 1451 浏览 1 评论 0原文

我想从Excel表将桌子复制到.docx。我有一个单词#tableauxvdd,我想用表代替。

所以我写了这个代码

     With word_fichier.Range
         With .Find
             .ClearFormatting
             .Replacement.ClearFormatting
             .Text = "#tableauxvdd"
             .Replacement.Text = "#tableauxvdd"
             .Forward = True
             .Wrap = wdFindStop
             .Format = False
             .MatchWildcards = True
             .Execute Replace:=wdReplaceNone
         End With
             If .Find.Found = True Then
                For i = 1 To nombre_de_vdd
                    Set tblRangeVdd = ThisWorkbook.Worksheets("Info vdd " & i).Range("A1:C6")
                    tblRangeVdd.Copy
                    .PasteExcelTable _
                        LinkedToExcel:=False, _
                        WordFormatting:=False, _
                        RTF:=False
                Next
             End If
     End With

,但它不起作用。这是我需要复制/粘贴的桌子

这就是结果

这就是我需要的:

I want to copy/paste tables from an Excel sheet toward a .docx. I have a word #tableauxvdd which I want to replace by my tables.

So I wrote this code

     With word_fichier.Range
         With .Find
             .ClearFormatting
             .Replacement.ClearFormatting
             .Text = "#tableauxvdd"
             .Replacement.Text = "#tableauxvdd"
             .Forward = True
             .Wrap = wdFindStop
             .Format = False
             .MatchWildcards = True
             .Execute Replace:=wdReplaceNone
         End With
             If .Find.Found = True Then
                For i = 1 To nombre_de_vdd
                    Set tblRangeVdd = ThisWorkbook.Worksheets("Info vdd " & i).Range("A1:C6")
                    tblRangeVdd.Copy
                    .PasteExcelTable _
                        LinkedToExcel:=False, _
                        WordFormatting:=False, _
                        RTF:=False
                Next
             End If
     End With

But its not working. Here is the tables I need to copy/paste
tables

And this is the result
enter image description here

And this is what i need :
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧梦荧光笔 2025-02-07 11:36:52

每当您编写代码以自动化单词时,您都需要考虑执行UI中的任务时将采取的步骤。

在Word文档中,表始终由段落分开,因此您的代码也需要做同样的操作。

例如:

If .Find.Found = True Then
    For i = 1 To nombre_de_vdd
        Set tblRangeVdd = ThisWorkbook.Worksheets("Info vdd " & i).Range("A1:C6")
        tblRangeVdd.Copy
        .PasteExcelTable _
            LinkedToExcel:=False, _
            WordFormatting:=False, _
            RTF:=False
        .InsertParagraphAfter
    Next
End If

您还需要以相反的顺序添加表,例如:

If .Find.Found = True Then
    Dim tblRangeVdd As word.Range
    Set tblRangeVdd = .Duplicate
    With tblRangeVdd
        .Collapse wdCollapseEnd
        For i = nombre_de_vdd To 1 Step -1
            ThisWorkbook.Worksheets("Info vdd " & i).Range("A1:C6").Copy
            .PasteExcelTable _
                LinkedToExcel:=False, _
                WordFormatting:=False, _
                RTF:=False
            .InsertParagraphAfter
        Next
    End With
End If

Whenever you write code to automate Word you need to think through what steps you would take if you were performing the task in the UI.

In a Word document, tables are always separated by a paragraph, so your code needs to do the same.

For example:

If .Find.Found = True Then
    For i = 1 To nombre_de_vdd
        Set tblRangeVdd = ThisWorkbook.Worksheets("Info vdd " & i).Range("A1:C6")
        tblRangeVdd.Copy
        .PasteExcelTable _
            LinkedToExcel:=False, _
            WordFormatting:=False, _
            RTF:=False
        .InsertParagraphAfter
    Next
End If

You'll also need to add the tables in reverse order, for example:

If .Find.Found = True Then
    Dim tblRangeVdd As word.Range
    Set tblRangeVdd = .Duplicate
    With tblRangeVdd
        .Collapse wdCollapseEnd
        For i = nombre_de_vdd To 1 Step -1
            ThisWorkbook.Worksheets("Info vdd " & i).Range("A1:C6").Copy
            .PasteExcelTable _
                LinkedToExcel:=False, _
                WordFormatting:=False, _
                RTF:=False
            .InsertParagraphAfter
        Next
    End With
End If
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文