Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将多个表格从Excel插入Word VBA_Excel_Vba_Ms Word - Fatal编程技术网

将多个表格从Excel插入Word VBA

将多个表格从Excel插入Word VBA,excel,vba,ms-word,Excel,Vba,Ms Word,我的代码有问题,我试图在Excel中复制3个不同的表/单元格位置,然后将这些值/表粘贴到Word中。我想把它们直接贴在另一个下面 我不断地得到一份工作 运行时错误为5941。 应用程序定义或对象定义错误 我试图查找这可能意味着什么,但我没有运气。我在这个网站上找到了这个参考资料:但是没有运气发现我的问题 我的代码: Sub TalkToWord() Dim wdApp As New Word.Application, wdDoc As Word.Document, wdRng As Word.R

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

我不断地得到一份工作

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

我试图查找这可能意味着什么,但我没有运气。我在这个网站上找到了这个参考资料:但是没有运气发现我的问题

我的代码:

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


这段代码在哪里运行?Word或Excel?我相信这是有效的,但现在我收到一个错误,指出With Block对象未定义,并用F8遍历代码。这似乎是With Block的第二个:Set wdDoc=.Documents.Add With wdDoc.Range是否有更好的方法在没有这么多With Block的情况下生成上述代码?@AlexFields那是另一个问题——你应该问一个新问题。并将此标记为已回答单击答案左侧的复选标记,假设它有助于解决您最初提出的问题。尽管要求一个更好的方法来做某事往往会让一个问题结束,因为它主要是基于观点的。我可以告诉你,我不会用积木把它们垒起来。每个都可以而且应该独立。通过连接插入的字符串,可以进一步优化代码。可以使用&vbCr而不是.InsertParagraphAfter
ThisWorkbook.Worksheets("Sheet2").Range("A1", "B2").Copy