簡體   English   中英

將多個表格從 Excel 插入到 Word VBA

[英]Inserting multiple tables from Excel to Word VBA

我的代碼有問題,我試圖在 Excel 中復制 3 個不同的表格/單元格位置,然后將這些值/表格粘貼到 Word 中。 我想將它們直接粘貼到另一個下面。

我不斷得到一個

5941 的運行時錯誤。“應用程序定義或對象定義錯誤”

我嘗試查找這可能意味着什么,但我沒有運氣。 我從這個站點找到了這個參考: 運行時錯誤 StackOverFlow但沒有找到我的問題。

我的代碼:

Sub TalkToWord()
Dim wdApp As New Word.Application, wdDoc As Word.Document, wdRng As Word.Range
Dim myCat As Integer: myCat = InputBox("Enter your Category: ")
Dim myConfig As Long: myConfig = InputBox("Enter your Config #: ")
Dim myGradeName As String: myGradeName = InputBox("Enter your Grade Name: ")
Dim myDept As Integer: myDept = InputBox("Enter your Dept: ")
Dim myClass As Integer: myClass = InputBox("Enter your Class/Subclass: ")
Dim mySeason As String: mySeason = InputBox("Enter your Season Code: ")
myTimeFrame = Application.InputBox("Enter your Time Frame: ", FormatDateTime(Date, vbShortDate))

With wdApp
  .Visible = True
  Set wdDoc = .Documents.Add
  With wdDoc.Range
    .PageSetup.TopMargin = InchesToPoints(0.3)
    .ParagraphFormat.Alignment = wdAlignParagraphCenter
    .Font.Bold = True
    .Font.Underline = True
    .InsertAfter "FY 19 CAT " & myCat
    .InsertParagraphAfter
    Set wdRng = .Characters.Last
    With wdRng
      .ParagraphFormat.Alignment = wdAlignParagraphLeft
      .ParagraphFormat.LeftIndent = InchesToPoints(-0.7)
      .ParagraphFormat.SpaceAfter = 5
      .Font.Underline = False
      .Font.Size = 12
      .InsertParagraphAfter
      .InsertAfter ("Grade Number:")
      .InsertParagraphAfter
      .InsertAfter ("Config #:")
      .InsertParagraphAfter
      .InsertAfter ("Grade Name:" & myGradeName)
      .InsertParagraphAfter
      .InsertAfter (Chr(9) & "-" & "Dept:" & myDept)
      .InsertParagraphAfter
      .InsertAfter (Chr(9) & "-" & "Class/Subclass:" & myClass)
      .InsertParagraphAfter
      .InsertAfter (Chr(9) & "-" & "Season Code:" & mySeason)
      .InsertParagraphAfter
      .InsertAfter (Chr(9) & "-" & "TimeFrame:" & myTimeFrame)
      .InsertParagraphAfter
      .InsertAfter (Chr(9) & "-" & "Grade Type:" & myGradeType)
      .InsertParagraphAfter
      .InsertAfter (Chr(9) & "-" & "Index Breakpoint Bands by Volume Grade:")
      .InsertParagraphAfter
      .InsertParagraphAfter
      .Collapse wdCollapseEnd
      .InsertParagraphAfter
      .InsertBreak Type:=wdLineBreak
      .Collapse wdCollapseEnd
      .PageSetup.TopMargin = InchesToPoints(0.1)
     --- Worksheets("Sheet2").Range("A1", "B2").Copy
      .Paste
      .Tables(1).Rows.Alignment = wdAlignRowCenter
      .InsertBreak Type:=wdLineBreak
      .Collapse wdCollapseEnd
      .PageSetup.TopMargin = InchesToPoints(0.1)
      Worksheets("Sheet2").Range("K1", "Q2").Copy
      .Paste
      .Tables(2).Rows.Alignment = wdAlignRowCenter
      .InsertBreak Type:=wdLineBreak
      .Collapse wdCollapseEnd
      .PageSetup.TopMargin = InchesToPoints(0.1)
      Worksheets("Sheet2").Range("K4", "Q6").Copy
      .Paste
      .Tables(3).Rows.Alignment = wdAlignRowCenter
    End With
  End With
End With
End Sub

錯誤發生在帶有 3 個破折號的線上。

我是 VBA 新手,所以請放輕松!

編輯:它將創建 Word 文檔

如果此代碼是從您從中復制內容的工作簿中運行的,請嘗試將問題行更改為

Worksheets("Sheet2").Range("A1", "B2").Copy

ThisWorkbook.Worksheets("Sheet2").Range("A1", "B2").Copy

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM