简体   繁体   English

复制Excel单元格并将其粘贴到特定的Word文档

[英]copy excel cell and paste it to specific word doc

I'm new to VBA and I need to copy one cell (that contains Data) from excel to a specific (not from a template) word document. 我是VBA的新手,我需要将一个单元格(包含数据)从excel复制到特定的 (而不是从模板)word文档中。 the full path of the specific file will be in a cell next to the targeted cell - offset(0,1). 特定文件的完整路径将位于目标单元格旁边的单元格中-offset(0,1)。 All of that obviously in a loop because I have a big list and a lot of files. 所有这些显然都处于循环中,因为我的清单很大,文件很多。

this is my code (the code is made of some part I picked up while searching) - but I get an 这是我的代码(该代码由我在搜索时捡到的某些部分组成)-但是我得到了

object error 对象错误

Sub OpenWordFile()
    Dim oWord As Object
    Dim xRg As Object
    Dim xCell As Range
    Dim xVal As Range
    Dim Workbook As Workbook
    Dim FileName As Variant



    'Word Object

    Set oWord = CreateObject(Class:="Word.Application")
    oWord.Visible = True

    'Open Word Document (need to be multiple files in a loop)

    'oWord.documents.Open FileName:="C:\Users\tamirre\Desktop\New folder\135-185844.doc"      ' OPEN AN EXISTING FILE.
    'Set oWord = Nothing

    'Activating Excel to copy Cells

    ThisWorkbook.Worksheets("Sheet1").Activate
    Set xRg = Application.InputBox("Please select Cells to copy to word docs:", ActiveWindow.RangeSelection.Address, , , , , 8)
    If xRg Is Nothing Then Exit Sub

    For Each xCell In xRg
        xVal = xCell.Value
        Set FileName = xVal.Offset(0, 1) 'Cell Must Contain name and full path of the doc file
        xVal.Copy
            oWord.documents.Open FileName:="FileName"
                Selection.EndKey Unit:=wdStory
                Selection.TypeParagraph
                Selection.PasteExcelTable True, False, False
    Next


End Sub

InputBox returns a String , not an Object . InputBox返回一个String ,而不是一个Object

Change the Dim line to this: Dim行更改为此:

Dim xRg As String

and change the InputBox lines to this: 并将InputBox行更改为此:

xRg = InputBox("Please select Cells to copy to word docs:", "Range Selection", ActiveWindow.RangeSelection.Address)
If xRg ="" Then Exit Sub

Then if you want to turn it into a Range object: 然后,如果要将其转换为Range对象:

Dim xRange As Object
Set xRange = Range(xRg)

However, I do not recommend doing what you are doing this way. 但是,我不建议您以此方式进行操作。 There are too many chances the user will enter something invalid and you will get errors. 用户输入无效内容的机会太多,您会得到错误。

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

相关问题 VBA将Word Doc中的下拉列表的选定值复制到特定的Excel单元格中 - VBA to copy selected value of dropdown in Word Doc into specific Excel cell VBA在word doc中查找特定文本并将此文本从word doc复制到excel中的单元格中 - VBA to find specific text in word doc and copy this text from word doc into a cell in excel 将 excel 文件中的 append 文件复制并粘贴到 word doc - Copy and Paste or append file from excel to word doc 将单元格值从excel复制并粘贴到word,而无需更改格式 - Copy and paste cell value from excel to word, without changing the format VBA从Word复制多行文本粘贴到Excel单元格 - VBA Copy multiline text from Word paste into Excel cell Excel - 在一行中搜索特定文本,然后将该值复制并粘贴到特定单元格中 - Excel - search for specific text in a row then copy and paste that value into a specific cell 将多个嵌套表复制并粘贴到特定的Word表单元格中 - Copy and paste multiple nested tables into a specific Word table cell Excel 根据单元格值在特定工作表上复制粘贴范围 - Excel copy-paste range on specific sheet according to cell value Excel VBA将特定的单元格复制并粘贴到另一个工作表 - Excel VBA copy and paste a specific cell to another Sheet Excel VBA:从另一个工作簿循环中复制和粘贴特定单元格 - Excel VBA: Copy and Paste Specific Cell from Another Workbook Loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM