简体   繁体   English

使用 contentcontrolsbytitle() 将数据从 Excel 传输到 Word 文档

[英]Transfer data from Excel to Word document using contentcontrolsbytitle()

I am trying to transfer data from the Excel workbook to a pre-existing Word document.我正在尝试将数据从 Excel 工作簿传输到预先存在的 Word 文档。

Within the Excel workbook, I have created a name for shipmentnumber, PO, Shipto, and then a name for each weight and lots on the worksheet named "ShipmentSummary".在 Excel 工作簿中,我为货件编号、PO、Shipto 创建了一个名称,然后在名为“ShipmentSummary”的工作表上为每个重量和批次创建了一个名称。

I have added content controls on the Word document called shipmentnumber, PO, shipto and then a title for each spot a weight and lot number needs to go.我在 Word 文档中添加了名为 shippingnumber、PO、shipto 的内容控件,然后为每个货点添加了标题,重量和批号需要 go。

I want the shipmentnumber from Excel to be transferred to the Word document with the content controls called "shipment", PO from Excel to be transferred to the Word document with the content controls called "PO", ShipTo from Excel to be transferred to the Word document with the content controls called "ShipTo".我希望将 Excel 中的货件编号转移到带有名为“shipment”的内容控件的 Word 文档中,将来自 Excel 的 PO 转移到带有名为“PO”的内容控件的 Word 文档中,将 ShipTo 从 ZC1D81AF5835844B4E9D3 转移到 91044B4E9D3包含名为“ShipTo”的内容控件的文档。 There are 50 of each shipmentnumber,PO and shipto controls on the Word document. Word 文档中每个shipmentnumber、PO 和shipto 控件有50 个。 I then want the weight values from the worksheet named "VBA_data" to be transferred to the Word document with content controls title w1-w50.然后,我希望将名为“VBA_data”的工作表中的权重值传输到内容控件标题为 w1-w50 的 Word 文档中。 Lastly I want the lot values from the worksheet name "VBA_data" to be transferred to the Word document with control title l1-l50.最后,我希望将工作表名称“VBA_data”中的批次值传输到控件标题为 l1-l50 的 Word 文档中。

Sub ReplaceControlsOnCaseLabel()
    Dim wordApp As Word.Application
    Dim wDoc As Word.Document
    Dim cc As ContentControl
    Dim rngCC As Word.Range
    
   set the word application and word document
    Set wordApp = CreateObject("word.application")
    Set wDoc = wordApp.Documents.Open("C:\Users\tyler.masson\Desktop\Shipment_VBA\Case Labels.docx")
    wordApp.Visible = True

'Send value of shipment number in excel workbook to word document with content controls titled "shipment", there are 50 in total.
    For Each cc In wDoc
        If cc.Title = "shipment" Then
            ActiveDocument.Sheets("ShipmentSummary").Range("shipmentnumber").Value
        End If
    Next
    
 'Send value of PO in excel workbook to word document with content controls titled "PO", there are 50 in total.
    For Each cc In wDoc
        If cc.Title = "PO" Then
            ActiveDocument.Sheets("ShipmentSummary").Range("PO").Value
        End If
    Next
    
 'Send value of shipto in excel workbook to word document with content controls titled "shipto", there are 50 in total.
    For Each cc In wDoc
        If cc.Title = "shipto" Then
            ActiveDocuments.Sheets("ShipmentSummary").Range("ShipTo").Value
        End If
    Next
    
    
'Send values of each weight from worksheet "VBA_data" to the word document with contentcontrols title w1-w50
    With wordApp.ActiveDocument
        wDoc.SelectContentControlsByTitle("w1") = ActiveDocument.Sheets("VBA_data").Range("w1").Value
        wDoc.SelectContentControlsByTitle("w2") = ActiveDocument.Sheets("VBA_data").Range("w2").Value
        wDoc.SelectContentControlsByTitle("w3") = ActiveDocument.Sheets("VBA_data").Range("w3").Value
        wDoc.SelectContentControlsByTitle("w4") = ActiveDocument.Sheets("VBA_data").Range("w4").Value
        wDoc.SelectContentControlsByTitle("w5") = ActiveDocument.Sheets("VBA_data").Range("w5").Value
        wDoc.SelectContentControlsByTitle("w6") = ActiveDocument.Sheets("VBA_data").Range("w6").Value
        wDoc.SelectContentControlsByTitle("w7") = ActiveDocument.Sheets("VBA_data").Range("w7").Value
        wDoc.SelectContentControlsByTitle("w8") = ActiveDocument.Sheets("VBA_data").Range("w8").Value
        wDoc.SelectContentControlsByTitle("w9") = ActiveDocument.Sheets("VBA_data").Range("w9").Value
        wDoc.SelectContentControlsByTitle("w10") = ActiveDocument.Sheets("VBA_data").Range("w10").Value
    End With


'Send values of each lot from worksheet "VBA_data" to the word document with contentcontrols title l1-l50
    With wordApp.ActiveDocument
        w wDoc.SelectContentControlsByTitle("l1") = ActiveDocument.Sheets("VBA_data").Range("l1").Value
        wDoc.SelectContentControlsByTitle("l2") = ActiveDocument.Sheets("VBA_data").Range("l2").Value
        wDoc.SelectContentControlsByTitle("l3") = ActiveDocument.Sheets("VBA_data").Range("l3").Value
        wDoc.SelectContentControlsByTitle("l4") = ActiveDocument.Sheets("VBA_data").Range("l4").Value
        wDoc.SelectContentControlsByTitle("l5") = ActiveDocument.Sheets("VBA_data").Range("l5").Value
        wDoc.SelectContentControlsByTitle("l6") = ActiveDocument.Sheets("VBA_data").Range("l6").Value
        wDoc.SelectContentControlsByTitle("l7") = ActiveDocument.Sheets("VBA_data").Range("l7").Value
        wDoc.SelectContentControlsByTitle("l8") = ActiveDocument.Sheets("VBA_data").Range("l8").Value
        wDoc.SelectContentControlsByTitle("l9") = ActiveDocument.Sheets("VBA_data").Range("l9").Value
        wDoc.SelectContentControlsByTitle("l10") = ActiveDocument.Sheets("VBA_data").Range("l10").Value
    End With
        
'Another way to send values to contentcontrolbytitle() ???
    'weight1 = wDoc.SelectContentControlsByTitle("w1") = ActiveDocument.Sheets("VBA_data").Range("w1").Value
    'weight2 = wDoc.SelectContentControlsByTitle("w2") = ActiveDocument.Sheets("VBA_data").Range("w2").Value
    'weight3 = wDoc.SelectContentControlsByTitle("w3") = ActiveDocument.Sheets("VBA_data").Range("w3").Value
    'weight4 = wDoc.SelectContentControlsByTitle("w4") = ActiveDocument.Sheets("VBA_data").Range("w4").Value
    'weight5 = wDoc.SelectContentControlsByTitle("w5") = ActiveDocument.Sheets("VBA_data").Range("w5").Value
    'weight6 = wDoc.SelectContentControlsByTitle("w6") = ActiveDocument.Sheets("VBA_data").Range("w6").Value
    'weight7 = wDoc.SelectContentControlsByTitle("w7") = ActiveDocument.Sheets("VBA_data").Range("w7").Value
    'weight8 = wDoc.SelectContentControlsByTitle("w8") = ActiveDocument.Sheets("VBA_data").Range("w8").Value
    'weight9 = wDoc.SelectContentControlsByTitle("w9") = ActiveDocument.Sheets("VBA_data").Range("w9").Value
    'weight10 = wDoc.SelectContentControlsByTitle("w10") = ActiveDocument.Sheets("VBA_data").Range("w10").Value

    
End Sub

Untested:未经测试:

Sub ReplaceControlsOnCaseLabel()
    
    Dim wordApp As Word.Application
    Dim wDoc As Word.document
    Dim cc As ContentControl, wsData As Worksheet
    Dim rngCC As Word.Range, wsShipment As Worksheet, i As Long
    
    'set the word application and word document
    Set wordApp = CreateObject("word.application")
    Set wDoc = wordApp.Documents.Open("C:\Users\tyler.masson\Desktop\Shipment_VBA\Case Labels.docx")
    wordApp.Visible = True

    Set wsShipment = ActiveWorkbook.Sheets("ShipmentSummary") 'or ThisWorkbook
    Set wsData = ActiveWorkbook.Sheets("VBA_data")

    SetCCValueByTitle wDoc, "shipment", wsShipment.Range("shipmentnumber").Value
    SetCCValueByTitle wDoc, "PO", wsShipment.Range("PO").Value
    SetCCValueByTitle wDoc, "shipto", wsShipment.Range("ShipTo").Value
    
    For i = 1 To 10
        SetCCValueByTitle wDoc, "w" & i, wsData.Range("W" & i).Value
        SetCCValueByTitle wDoc, "l" & i, wsData.Range("L" & i).Value
    Next i
    
End Sub

'set text to CCValue in all controls with Title = CCTitle 
Sub SetCCValueByTitle(doc As Word.document, CCTitle, CCValue)
    Dim cc As Word.ContentControl, ccs As Word.ContentControls
    Set ccs = doc.SelectContentControlsByTitle(CCTitle)
    'warn if none found
    If ccs.Count = 0 Then MsgBox "No controls with title '" & CCTitle & "'"
    For Each cc In ccs
        cc.Range.Text = CCValue
    Next cc
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM