从excel复制和过去循环到出现命令错误的单词

时间:2016-12-14 11:03:22

标签: vba excel-vba word-vba excel

我运行此代码以从我的代码中定义的2个单元格中选择Excel中的单元格,然后将其粘贴到特定书签位置的单词中。但是,当我运行代码时,它适用于“start_of_text”,“end_of_text”和“Start_of_table”,“End_of_table”之间的第一个单元格,但后来我在第二个textemarke.pastespecial(xlPasteall)上遇到错误,说运行时错误'4198 '命令失败。

有人有任何建议吗?

  Dim rownum As Integer
  Dim colnum As Integer
  Dim startrow As Integer
  Dim endrow As Integer
  Dim textmarke

   rownum = 1
   colnum = 2

 ' Filtering for T1 line

  ThisWorkbook.Worksheets("Template-Referenz-Produkte").Columns("A:A").AutoFilter Field:=1, Criteria1:="=<T1 Line>" _
    , Operator:=xlOr


   ' Find eveything between start of text and end of text
    With ThisWorkbook.Worksheets("Template-Referenz-Produkte")
    For rownum = 1 To 10000
       Do
         If .Cells(rownum, 2).Value = "Start_of_text" Then
             startrow = rownum + 1
          End If
          rownum = rownum + 1

       If (rownum > 10000) Then Exit For

       Loop Until .Cells(rownum, 2).Value = "End_of_text"
       endrow = rownum - 1

       'Select everything from start of text to end of text
       ThisWorkbook.Worksheets("Template-Referenz-Produkte").Range("B" & startrow & ":B" & endrow).Copy

       'Paste description from excel sheet
       Set textmarke = doc.Bookmarks("INSERT_T1_LINE_reference_TABLE").Range
       textmarke.PasteSpecial (xlPasteAll)
       textmarke.Style = "Heading 4"
       doc.Bookmarks.Add "INSERT_T1_LINE_reference_TABLE", textmarke 'Set the text marker on this specific range.

    ' Find everything between start of table and end of table
       Do
          If .Cells(rownum, 2).Value = "Start_of_table" Then
             startrow = rownum + 1
          End If
          rownum = rownum + 1

       If (rownum > 10000) Then Exit For

       Loop Until .Cells(rownum, 2).Value = "End_of_table"
       endrow = rownum - 1

      'Select everything from start of table to end of table
       ThisWorkbook.Worksheets("Template-Referenz-Produkte").Range("B" & startrow & ":E" & endrow).Copy

       'Paste description from excel sheet
       Set textmarke = doc.Bookmarks("INSERT_T1_LINE_reference_TABLE").Range
       textmarke.PasteSpecial (xlPasteAll)
       doc.Bookmarks.Add "INSERT_T1_LINE_reference_TABLE", textmarke 'Set the text marker on this specific range.

    Next rownum
    End With

1 个答案:

答案 0 :(得分:0)

您可以尝试代替textMarke.PasteSpecial

textMarke.Select
doc.Parent.CommandBars.ExecuteMso "PasteExcelTableSourceFormatting"