简体   繁体   English

将Word段落复制到Excel单元格

[英]Copy Word Paragraph to Excel Cells

I am trying to copy Word paragraphs to Excel cells, but I am hung up on Runtime error 9: Subscript out of range. 我试图将Word段落复制到Excel单元格,但是我挂断了运行时错误9:下标超出范围。

I have searched. 我已经搜寻了。 Everything I read says it cannot find the file, but the file is in the same folder, and the name is not mis-spelled, and the extension is correct. 我读过的所有内容都说找不到该文件,但是该文件位于同一文件夹中,并且名称拼写正确,并且扩展名正确。 So, I am stumped. 所以,我很沮丧。 The original code comes from here: How to copy a formatted paragraph from Word 2013 to Excel? 原始代码来自此处: 如何将格式化的段落从Word 2013复制到Excel? .

    Private Sub Load_Schedule()
    Dim ParaCount As Integer
    Dim wDoc As Word.Document
    Dim wb As Workbook
    Dim ws As Worksheet
    Set wDoc = ActiveDocument
    Set wb = Workbooks("new.xlsm")        
    Set ws = wb.Sheets("Sheet1")
        ws.Activate
        ws.Columns(1).AutoFit
    For ParaCount = 1 To wDoc.Paragraphs.Count
        wDoc.Paragraphs(ParaCount).Range.FormattedText.Copy
        Sheets(ws).Cells(ParaCount, 1).PasteSpecial 
        Paste:=xlPasteFormats
    Next ParaCount
    End Sub

The error comes on this line: Set wb = Workbooks("new.xlsm") 该行出现错误:Set wb = Workbooks(“ new.xlsm”)

As you work with both applications, you should use full declarations like Word.Document and Excel.Workbook (if you already referenced the appropriate libraries). 在使用这两个应用程序时,应使用完整的声明,如Word.DocumentExcel.Workbook (如果您已经引用了适当的库)。

An already opened Excel file can be referenced without path. 可以引用没有路径的已经打开的Excel文件。

The Paste:= ... parameter belongs to the previous code line, so you have to add a blank + undersore at the end of the previous line or put them together into one line. Paste:= ...参数属于上一行代码,因此您必须在上一行的末尾添加一个空白+下划线,或将它们放在一起成为一行。

Please reference your worksheet's cell by ws.Cells ... and not by Sheets(ws) , as your "ws" already is a worksheet object and not a string. 请通过ws.Cells ...而不是Sheets(ws)引用工作表的单元格,因为“ ws”已经是工作表对象而不是字符串。

The further answer depends, if you run your code from Word-VBA or from Excel-VBA. 进一步的答案取决于您是从Word-VBA还是从Excel-VBA运行代码。


Word VBA Word VBA

If you want to reference an Excel file from Word-VBA, you need the Excel.Application object additionally. 如果要从Word-VBA引用Excel文件,则还需要Excel.Application对象。
If Excel is already started, you can use the existing application object - otherwise you create one and make it visible. 如果已经启动Excel,则可以使用现有的应用程序对象-否则,创建一个对象并将其显示。

Same with your Excel file: If it's already open, you use it - if not, you open it. 与Excel文件相同:如果已经打开,则使用它-如果没有,则打开它。

Private Sub LoadSchedule()
    Dim ParaCount As Integer
    Dim wDoc As Word.Document
    Dim objExcel As Excel.Application
    Dim wb As Excel.Workbook
    Dim ws As Excel.Worksheet

    On Error Resume Next
    Set objExcel = GetObject(, "Excel.Application")
    On Error GoTo 0
    If objExcel Is Nothing Then
        Set objExcel = CreateObject("Excel.Application")
        objExcel.Visible = True
    End If

    On Error Resume Next
    Set wb = objExcel.Workbooks("new.xlsm")
    On Error GoTo 0
    If wb Is Nothing Then
        Set wb = objExcel.Workbooks.Open(objExcel.DefaultFilePath & "\new.xlsm")
        ' or ThisDocument.Path or whatever path
    End If

    Set wDoc = ActiveDocument
    Set ws = wb.Sheets("Sheet1")
    For ParaCount = 1 To wDoc.Paragraphs.Count
        wDoc.Paragraphs(ParaCount).Range.FormattedText.Copy
        ws.Cells(ParaCount, 1).PasteSpecial Paste:=xlPasteFormats
    Next ParaCount

    ws.Columns(1).AutoFit
    'ws.Activate
End Sub

Excel VBA Excel VBA

In Excel you can try to reference an already opened Word file directly as ActiveDocument without getting the Word.Application additionally. 在Excel中,您可以尝试直接将已打开的Word文件作为ActiveDocument引用,而无需另外获取Word.Application。

Private Sub LoadSchedule()
    Dim ParaCount As Integer
    Dim wDoc As Word.Document
    Dim wb As Excel.Workbook
    Dim ws As Excel.Worksheet

    On Error Resume Next
    Set wb = Workbooks("new.xlsm")
    On Error GoTo 0
    If wb Is Nothing Then
        Set wb = Workbooks.Open(Application.DefaultFilePath & "\new.xlsm")
    End If

    Set wDoc = ActiveDocument
    Set ws = wb.Sheets("Sheet1")
    For ParaCount = 1 To wDoc.Paragraphs.Count
        wDoc.Paragraphs(ParaCount).Range.FormattedText.Copy
        ws.Cells(ParaCount, 1).PasteSpecial Paste:=xlPasteFormats
    Next ParaCount

    ws.Columns(1).AutoFit
    'ws.Activate
End Sub

You need to specify the full path to the excel file - you say it's the same as the word document so this will work: 您需要指定excel文件的完整路径-您说它与word文档相同,因此可以使用:

Sub GetXLFileInWord()
Dim xl As Excel.Application
Set xl = New Excel.Application
Dim wb As Excel.Workbook
Set wb = xl.Documents.Open(ThisDocument.Path & "\new.xlsm")

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

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