从Excel插入多个表到Word VBA

时间:2018-07-26 13:19:59

标签: excel-vba word-vba

我的代码有问题,我试图在Excel中复制3个不同的表/单元格位置,然后将这些值/表粘贴到Word中。我想将它们直接粘贴在彼此之间。

我不断收到

  

运行时错误5941。   “应用程序定义或对象定义的错误”

我尝试查找这可能意味着什么,但我没有运气。我从以下站点找到了此参考:Run Time Error 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文档

1 个答案:

答案 0 :(得分:1)

如果此代码是从您要复制内容的工作簿中运行的,请尝试将问题行从更改为

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

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