Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.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文档_Excel_Vba_Ms Word - Fatal编程技术网

将数据从excel推送到word文档

将数据从excel推送到word文档,excel,vba,ms-word,Excel,Vba,Ms Word,我正在尝试将excel数据推送到word文档中。word文档是预先存在的文档,需要用值和批次ID填充。我设置了word文档,以便所有需要从excel中填写数据的点都是丰富的内容控件。我将Rich Content控件插入到文档中需要填写的每个位置 [![在此处输入图像描述][1][1] Excel数据 仅为样本数据粘贴了10行 [![在此处输入图像描述][2][2] Word文档。“权重”列和“批次”列中的丰富内容控件 我想完成的是将excel电子表格中的“权重”推到word文档的“权重”列中,并

我正在尝试将excel数据推送到word文档中。word文档是预先存在的文档,需要用值和批次ID填充。我设置了word文档,以便所有需要从excel中填写数据的点都是丰富的内容控件。我将Rich Content控件插入到文档中需要填写的每个位置

[![在此处输入图像描述][1][1] Excel数据

仅为样本数据粘贴了10行

[![在此处输入图像描述][2][2]

Word文档。“权重”列和“批次”列中的丰富内容控件

我想完成的是将excel电子表格中的“权重”推到word文档的“权重”列中,并将excel电子表格中的“批量批次”推到word文档的“批次”列中

Sub PushDatatoWord()
  'Declare the appropriate variables
  Dim wordApp As Word.Application
  Dim wDoc As Word.Document
  Dim r As Integer

'Create a link to the microsoft word application
  Set wordApp = CreateObject("word.application")
'Create variable of the word document
Set wDoc = wordApp.Documents.Open("Worddoc.docx")
wordApp.Visible = True
r = 2

For i = 1 To 20
    wDoc.ContentControls(i).Range.Text = Sheets("Boxes_Push").Cells(r, 3)
    r = r + 1
Next i

wordApp.Documents.Close
wordApp.Quit

End Sub
上面是我编写的代码,它推送值,但没有将它们全部放入正确的列中,代码在word文档的所有内容控件中循环,并将值放入下一个控件中。如何分离word文档上的内容控件,以便将数据发送到相应的列

编辑到原始帖子:

[![Excel工作表,范围为B2、C15][3][3]

[![Word文档与命名内容控件][4][4]

'1. Name all content controls on word document. weight1, weight2, weight3, etc. lot1, lot2, lot3, etc.
'2. Create a 2d array with the values and lot . Read range values to arrary.
'3. Use SelectContentControlsByTitle() in a foreach loop with the array created.


Sub dataToWord()

    Dim wordApp As Word.Application
    Dim wDoc As Word.Document
    Dim r As Long
    Dim arr As Variant
        
    'create connection to the word application
    
    Set wordApp = CreateObject("word.application")
    
    'Set the word document, content controls named weight1, weight2, etc and lot1, lot2, etc
    
    Set wDoc = wordApp.Documents.Open("C:\Users\tyler.masson\Desktop\PushToWord\testpush_withnames.docx")
    wordApp.Visible = True
    r = 2
 
    'range created on worksheet from cells B2,C15 called "range"
    arr = Range("range").Value
    For Each i In arr
        wDoc.SelectContentControlsByTitle(Weight).Range.Text = Sheets("testpush").Cells(r, 2)
        r = r + 1
        
    Next i
    
   
    wordApp.Documents.Close
    wordApp.Quit


End Sub
我在for-each循环中遇到问题。我不完全确定如何按标题(权重和批次?)循环遍历每个内容控件,然后将创建的数组中的值分配给这些内容控件

我真的很感谢你在这方面的帮助。 [1]: [2]: [3]:
[4] :

类似的方法应该可以:

Sub dataToWord()

    Dim wordApp As Word.Application
    Dim wDoc As Word.document, tbl As Word.Table
    Dim r As Long, c As Long, maxRows As Long, i As Long
    Dim arr As Variant
        
'    Set wordApp = CreateObject("word.application")
'    Set wDoc = wordApp.Documents.Open("C:\Users\tyler.masson\Desktop\PushToWord\testpush_withnames.docx")
'    wordApp.Visible = True
    
    'using an already-open doc for testing
    Set wordApp = GetObject(, "word.application")
    Set wDoc = wordApp.activeDocument
    
    Set tbl = wDoc.Tables(1) 'assuming just one table

    r = 2
    c = 2
    maxRows = 10 'how many rows in the first set of columns
                 '  before we need to move over to the next set?

    'maxRows = tbl.Rows.Count - 1 'dynamic count
    
    'range created on worksheet from cells B2,C15 called "range"
    arr = Range("range").Value
    For i = LBound(arr, 1) To UBound(arr, 1)
        'put the values directly in the cells
        tbl.Cell(r, c).Range.Text = arr(i, 1)
        tbl.Cell(r, c + 2).Range.Text = arr(i, 2)
        
        'time to switch to next column?
        If i = maxRows Then
            c = c + 5
            r = 2
        Else
            r = r + 1
        End If
        
    Next i
End Sub

您是否需要内容控件,或者您可以直接将文本放入单元格中?首先:在Word文档中准备具有给定名称的内容控件,例如weight1、weight2、Weight3等,以及lot1、lot2的相同名称。。。第二:将范围值读取到2D数组(名称-值对)。第三:在数组的foreach循环中使用SelectContentControlsByTitle()。请注意,不要在2020年使用整数类型-而是使用Long…Excel数据和Word数据之间似乎没有任何关系。Excel中的批号前缀为PT,但Word中缺少前缀,此外,此处和“权重”列中的数字为小数,而Excel中的数字为整数。怎么回事?@RafałB。谢谢你的评论。我已尝试集成您在下面代码中提供的概念:我对VBA相当陌生,因此我不确定是否正确创建了数组,或者是否使用contentcontrolsbytitle()执行for each循环。我已经将word文档中的所有内容控件分配给了weight1、weight2等,以及lot1、lot2等。我将新代码添加到了我原来文章的底部。我也会包括新的施里恩镜头。谢谢你的帮助@TimWilliams我们有过这样的例子:人们输入了错误的号码!很容易出现人为错误。这就是我努力完成这项任务的原因。用户只需在电子表格中输入一次权重值,它就会被转移到表单中。@Tim Williams非常感谢您!这很有效。假设有45个框(excel文档中有45行),word文档中每列有25个空格。我会更改最大行数=25还是最大行数=45?对于将值传输到word文档,如果I=maxrows,我是否会更改?我非常感谢你的帮助。我试图使excel文档中的行数在1-60之间。有时发送4箱,有时发送50箱。再次感谢
maxRows
是您可以在Word表的左侧填写的行数,占该表中插槽总数的一半
maxRows=10
应该是您需要更改的唯一行。或者,您可以使用
(tbl.Rows.Count-1)
-将
-1
用于说明标题行。看到我的编辑很好,非常感谢!我将在我的工作簿项目中尝试一下。@Tim Williams,如果你有时间,你可以看看我的新问题吗?我现在正试图将数据推到已经创建的标签上-真是让我头疼!!!我需要扩展我的代码!我现在需要得到word文档第2页上的案例框51-100。与第1页格式完全相同,我将如何实现这一点?我试着在word中查找如何连接到下一页,但没有结果。我尝试设置Set tbl=wDoc.Tables(2),使其包含word文档的两个表,但不确定如何将数据推送到第2页的那些列和行。非常感谢您的帮助!再次感谢。