2016-12-27 232 views
0

我想從excel複製和粘貼多個表到word,但它給我下標超出範圍錯誤,當我試圖定義tbl。我在網上找到這些代碼,並試圖修改代碼以適應我的需要。下標超出範圍錯誤 - vba

Sub ExcelTablesToWord_Modified() 

    Dim WordApp As Word.Application 
    Dim myDoc As Word.Document 
    Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary") 
    Dim sheet As Excel.Worksheet 
    Dim tableName As String 

    With dict 
     .Add "TableA1", "TableA1" 
     .Add "TableA2", "TableA2" 
     .Add "TableB1", "TableB1" 
     .Add "TableB2", "TableB2" 
     .Add "TableC", "TableC" 
     .Add "TableD", "TableD" 
     .Add "TableE1", "TableE1" 
     .Add "TableE2", "TableE2" 
     .Add "TableF1", "TableF1" 
     .Add "TableF2", "TableF2" 
     'TODO: add the remaining WorksheetName/TableName combinations 
    End With 

    'Optimize Code 
    Application.ScreenUpdating = False 
    Application.EnableEvents = False 

    'Set Variable Equal To Destination Word Document 
    On Error GoTo WordDocNotFound 
     Set WordApp = GetObject(class:="Word.Application") 
     WordApp.Visible = True 
     Set myDoc = WordApp.Documents("a.docx") 
    On Error GoTo 0 

    'Loop Through Worksheets, and Copy/Paste Multiple Excel Tables 
    For Each sheet In ActiveWorkbook.Worksheets 
     tableName = dict(sheet.Name) 

     'Copy Table Range from Excel 
     sheet.ListObjects(tableName).Range.Copy 

     'Paste Table into MS Word (using inserted Bookmarks -> ctrl+shift+F5) 
     myDoc.Bookmarks(tableName).Range.PasteExcelTable _ 
      LinkedToExcel:=False, _ 
      WordFormatting:=False, _ 
      RTF:=False 

     'Autofit the most-recently-pasted Table so it fits inside Word Document 
     myDoc.Tables(myDoc.Tables.Count).AutoFitBehavior (wdAutoFitWindow) 

    Next sheet 

    'Completion Message 
    MsgBox "Copy/Pasting Complete!", vbInformation 
    GoTo EndRoutine 

    'ERROR HANDLER 
WordDocNotFound: 
    MsgBox "Microsoft Word file 'b' is not currently open, aborting.", 16 

    'Put Stuff Back The Way It Was Found 
EndRoutine: 
    'Optimize Code 
    Application.ScreenUpdating = True 
    Application.EnableEvents = True 

    'Clear The Clipboard 
    Application.CutCopyMode = False 

End Sub 

enter image description here

enter image description here

enter image description here

+2

你的代碼是什麼樣的?我沒有在您的列表中看到Microsoft Word 14.0對象庫。 – Duston

+2

4日在您的選擇是'的Microsoft Office 14.0對象Library',但你可能想'的Microsoft Word 14.0對象Library' – YowE3K

+1

號,4號選擇的Microsoft Office可能會或可能不會包含字。鑑於有明確的Excel和Access庫,可能會有一個明確的Word庫。 – Duston

回答

0

是基於你的原始模型本來我提供的代碼,其中相應的工作表,表,並收藏在每一組有一個不同的名字。

現在您已確保每個集合中的對象名稱相同(這是更好的模型),請嘗試以下過程。唯一的區別是Scripting.Dictionary已被刪除,並且工作表名稱用於提供表名和書籤名稱(因爲所有三個值現在匹配)。

和以前一樣,這一個也已經在Excel/Word中2016進行測試,如預期的那樣發揮作用:

Public Sub ExcelTablesToWord_Modified2() 

    Dim WordApp As Word.Application 
    Dim myDoc As Word.Document 
    Dim sheet As Excel.Worksheet 

    'Optimize Code 
    Application.ScreenUpdating = False 
    Application.EnableEvents = False 

    'Set Variable Equal To Destination Word Document 
    On Error GoTo WordDocNotFound 
     Set WordApp = GetObject(class:="Word.Application") 
     WordApp.Visible = True 
     Set myDoc = WordApp.Documents("a.docx") 
    On Error GoTo 0 

    'Loop Through Worksheets, and Copy/Paste Multiple Excel Tables 
    For Each sheet In ActiveWorkbook.Worksheets 

     'Copy Table Range from Excel 
     sheet.ListObjects(sheet.Name).Range.Copy 

     'Paste Table into MS Word (using inserted Bookmarks -> ctrl+shift+F5) 
     myDoc.Bookmarks(sheet.Name).Range.PasteExcelTable _ 
      LinkedToExcel:=False, _ 
      WordFormatting:=False, _ 
      RTF:=False 

     'Autofit the most-recently-pasted Table so it fits inside Word Document 
     myDoc.Tables(myDoc.Tables.Count).AutoFitBehavior (wdAutoFitWindow) 

    Next sheet 

    'Completion Message 
    MsgBox "Copy/Pasting Complete!", vbInformation 
    GoTo EndRoutine 

    'ERROR HANDLER 
WordDocNotFound: 
    MsgBox "Microsoft Word file 'b' is not currently open, aborting.", 16 

    'Put Stuff Back The Way It Was Found 
EndRoutine: 
    'Optimize Code 
    Application.ScreenUpdating = True 
    Application.EnableEvents = True 

    'Clear The Clipboard 
    Application.CutCopyMode = False 

End Sub 


如果仍然收到同樣的錯誤,那麼也許工作簿已損壞。在這種情況下,嘗試做如下:

  1. 一個工作表
  2. 創建一個新的工作簿重命名工作表,這樣它的名字相匹配的Word文檔中的書籤的一個
  3. 手動的名字添加一個單一的,小的,「測試專用」表到工作表(不要複製/粘貼從原來的工作簿之一)
  4. 確保表的名字是一樣的工作表的名稱
  5. 複製/上述粘貼程序到一個新的模塊中牛逼工作簿
  6. 保存新工作簿
  7. 確保您的Word文檔被打開,並運行程序

如果這樣的作品,那麼你可以考慮在新的工作簿中重新創建整個原始工作簿。這樣做時,如果你的數據集是足夠大,你必須複製 /從原始工作簿粘貼,使用帶有「只有值」,而不是隻是一個正常的粘貼「選擇性粘貼」。然後,手動重新創建任何缺少的格式。這樣,原來的工作簿中的任何腐敗就不太可能轉移到新的工作簿上。

+0

嗨,我修改了代碼,並將每個書籤,工作表名稱和表名更改爲相同的,TableA1,TableA2 ...並且我也完成了。爲我的每個表添加,但是當我運行它時,它在工作表上給我一個錯誤.ListObjects(tableName).Range.Copy這一行,不知道爲什麼。我的工作簿中只有10張,沒有隱藏的標籤。 – sc1324

+0

什麼是錯誤?如果它再次是「下標超出範圍」,那麼很可能你在一個或多個'.Add'行中有一個不正確的表名。在那種情況下,作爲一個測試,你可以嘗試運行除'_Add'行之外的所有行的註釋。否則,我需要知道確切的錯誤信息。 – MJH

+0

我得到了同樣的錯誤,下標越界,並請參見上面,我玩完您提供您的代碼,他們是在同一順序作爲我的工作表。所以我的工作表名稱,表名和我的書籤名稱是相同的。我的單詞doc的名字是a.docx,我的excel的名字是b.xlsm。 – sc1324

0

下面將複製的第一個表中的每一個工作表並粘貼到Word文檔,無論是表名。在Word文檔中的書籤名稱假設是簡單地在1前綴「書籤」的開始。

如果真的需要特定的表名,然後通過在每個工作表中的每個表創建一個名稱的收集和循環,如果表名是在收集然後進行復制。

Option Base 1 'Force arrays to start at 1 instead of 0 

Sub ExcelTablesToWord() 

    Dim oWS As Worksheet 
    Dim tbl As Excel.Range 
    Dim WordApp As Object ' Word.Application 
    Dim myDoc As Object ' Word.Document 
    Dim x As Long ' Integer 


    'Optimize Code 
    Application.ScreenUpdating = False 
    Application.EnableEvents = False 

    'Set Variable Equal To Destination Word Document 
    On Error Resume Next 
    Set WordApp = GetObject(, "Word.Application") 
    If WordApp Is Nothing Then Set WordApp = CreateObject("Word.Application") 
    If WordApp Is Nothing Then GoTo WordDocNotFound 
    WordApp.Visible = True 
    Set myDoc = WordApp.Documents("a.docx") 
    If myDoc Is Nothing Then Set myDoc = WordApp.Documents.Open("a.docx") 
    If myDoc Is Nothing Then GoTo WordDocNotFound 

    'Loop Through and Copy/Paste Multiple Excel Tables 
    x = 1 ' For x = LBound(TableArray) To UBound(TableArray) 
    For Each oWS In ThisWorkbook.Worksheets 

     'Copy Table Range from Excel 
     'Set tbl = ThisWorkbook.Worksheets(x).ListObjects(TableArray(x)).Range 
     Set tbl = oWS.ListObjects(1).Range 
     If Not tbl Is Nothing Then 
      tbl.Copy 

      'Paste Table into MS Word (using inserted Bookmarks -> ctrl+shift+F5) 
      myDoc.Bookmarks("bookmark" & x).Range.PasteExcelTable LinkedToExcel:=False, WordFormatting:=False, RTF:=False 

      'Autofit Table so it fits inside Word Document 
      myDoc.Tables(x).AutoFitBehavior 2 ' (wdAutoFitWindow) 

      x = x + 1 
     End If 
    Next 
    On Error GoTo 0 

    'Completion Message 
    MsgBox "Copy/Pasting Complete!", vbInformation 
    GoTo EndRoutine 

    'ERROR HANDLER 
WordDocNotFound: 
    MsgBox "Microsoft Word file 'b' is not currently open, aborting.", 16 

    'Put Stuff Back The Way It Was Found 
EndRoutine: 
    'Optimize Code 
    Application.ScreenUpdating = True 
    Application.EnableEvents = True 

    'Clear The Clipboard 
    Application.CutCopyMode = False 

End Sub 
+0

嗨,它看起來像代碼經歷並做了複製和粘貼,但我沒有看到它在我的word文檔中的任何地方。 – sc1324

+0

您是否通過了PasteExcelTable部分?當存在從書籤1到書籤4的書籤時,我的測試Excel文件和Word Doc工作正常。休息一下'x = x + 1',看看Word Doc是否還有這些書籤。 – PatricK