简体   繁体   English

在不使用书签的情况下将命名范围从 excel 复制到 word 文档中的特定位置 [更新]

[英]Copying named ranged from excel to a specific location in a word document without using bookmarks [Updated]

I wanted some help with the following problem我需要一些帮助来解决以下问题

I have a series of ranges in excel as the example here.作为示例,我在 excel 中有一系列范围。

table key表键 table range表范围
table6表6 A4:G14 A4:G14
table7表7 A15:G20 A15:G20
table8表8 A21:E30 A21:E30

and so on.等等。

I wanted to know if there is a code that can search a word document I have for the names in column 1 and paste the ranges in the second column.我想知道是否有代码可以在我拥有的 word 文档中搜索第 1 列中的名称并将范围粘贴到第二列中。 This is the segment of the code I have这是我拥有的代码段

    For Each cell In rng "rng is the range of the table key column"
    If cell.Value = "" Then Exit For
    With wdDoc.Content.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .MatchWildcards = False
        .Wrap = wdFindContinue
        .Text = cell.Value
        .Replacement.Text = Range(cell.Offset(0, 1))
        .Execute Replace:=wdReplaceAll
    End With
Next

I get an error in the replacement.text line but I don't know how to replace it with a range.我在 replacement.text 行中收到错误,但我不知道如何用范围替换它。

EDIT I also tried using the code below to copy and paste the ranges in the table but I get an object defined error at the pasteexceltable line.编辑我还尝试使用下面的代码复制和粘贴表中的范围,但我在 pasteexceltable 行中收到 object 定义的错误。

Set appWd = CreateObject("Word.Application")
set wdFind = appWd.Selection.Find
For Each cell In rng
    If cell.Value = "" Then Exit For
    ClipT = " "
    Range(cell.Offset(0, 1)).Copy
    wdFind.Text = cell.Value
    wdFind.Replacement.Text = ""
    wdFind.Forward = True
    wdFind.Wrap = wdFindContinue
    wdFind.Execute
    Call CheckClipBrd 'This function checks if the clipboard is empty'
    Range(cell.Offset(0, 1)).Copy
    ActiveWindow.Selection.PasteExcelTable _
        LinkedToExcel:=False, _
        WordFormatting:=False, _
        RTF:=True
    CutCopyMode = False

If, for example, your Word template contains the relevant tables and each is bookmarked with the same name as the table names in Excel, then, provided each Word table:例如,如果您的 Word 模板包含相关表格,并且每个表格都使用与 Excel 中的表格名称相同的名称作为书签,则提供每个 Word 表格:

• has both a header row and an empty data row; • 同时具有 header 行和空数据行; and

• the same number of columns as your Excel data, • 与您的 Excel 数据相同的列数,

you could use code like:你可以使用如下代码:

Sub ExportToWord()
' Note: A reference to the Microsoft Word # Object Library is required,
' set via Tools|References in the Excel VBE.
Dim WdApp As New Word.Application, WdDoc As Word.Document
Dim XlSht As Excel.Worksheet, XlTbl As Excel.ListObject
Set XlSht = ThisWorkbook.Worksheets("Sheet1")
With WdApp
  Set WdDoc = .Documents.Add(Template:="full name & path to template")
  For Each XlTbl In XlSht.ListObjects
    XlTbl.DataBodyRange.Copy
    With WdDoc
      If .Bookmarks.Exists(XlTbl.Name) Then
        With .Bookmarks.Exists(XlTbl.Name)
          .Range.PasteAppendTable
          .Rows(2).Delete
        End With
      End If
    End With
  Next
End With
End Sub

The reason for the Word table initially having an empty data row is so that the paste doesn't take on the formatting of the header row. Word 表最初具有空数据行的原因是粘贴不会采用 header 行的格式。

If you really, really don't want to use bookmarks, you would need another way of correlating the Word tables and Excel data (eg via Find for an identifying string [such as the Excel table's name] in Word, or by the Word table index # [which might be retrieved from the Excel table's name]).如果您真的,真的不想使用书签,您将需要另一种方法来关联 Word 表和 Excel 数据(例如,通过查找识别字符串 [例如 Excel 表的名称] 在 Word 中,或通过 Word 表index # [可能从 Excel 表的名称中检索到])。

For example, if the Excel table's name is in the Word table's second row:例如,如果 Excel 表的名称在 Word 表的第二行:

Sub ExportToWord()
' Note: A reference to the Microsoft Word # Object Library is required,
' set via Tools|References in the Excel VBE.
Dim WdApp As New Word.Application, WdDoc As Word.Document
Dim XlSht As Excel.Worksheet, XlTbl As Excel.ListObject
Set XlSht = ThisWorkbook.Worksheets("Sheet1")
With WdApp
  Set WdDoc = .Documents.Add(Template:="full name & path to template")
  For Each XlTbl In XlSht.ListObjects
    XlTbl.DataBodyRange.Copy
    With WdDoc
      With .Range.
        With .Find
          .Text = XlTbl.Name
          .MatchWildcards = True
          .Execute
        End With
        If .Find.Found = True Then
          With .Tables(1)
            .Range.PasteAppendTable
            .Rows(2).Delete
          End With
        End If
      End With
    End With
  Next
End With
End Sub

or, if the the Word table's index # corresponds with the Excel table's name:或者,如果 Word 表的索引 # 对应于 Excel 表的名称:

Sub ExportToWord()
' Note: A reference to the Microsoft Word # Object Library is required,
' set via Tools|References in the Excel VBE.
Dim WdApp As New Word.Application, WdDoc As Word.Document
Dim XlSht As Excel.Worksheet, XlTbl As Excel.ListObject
Set XlSht = ThisWorkbook.Worksheets("Sheet1")
With WdApp
  Set WdDoc = .Documents.Add(Template:="full name & path to template")
  For Each XlTbl In XlSht.ListObjects
    XlTbl.DataBodyRange.Copy
    With WdDoc
        With .Tables(Replace(XlTbl.Name, "Table", ""))
          .Range.PasteAppendTable
          .Rows(2).Delete
        End With
      End If
    End With
  Next
End With
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM