Copy Word Paragraph to Excel Cells

杀马特。学长 韩版系。学妹 提交于 2021-01-28 07:00:52

问题


I am trying to copy Word paragraphs to Excel cells, but I am hung up on Runtime error 9: Subscript out of range.

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?.

    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")


回答1:


As you work with both applications, you should use full declarations like Word.Document and Excel.Workbook (if you already referenced the appropriate libraries).

An already opened Excel file can be referenced without path.

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.

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.

The further answer depends, if you run your code from Word-VBA or from Excel-VBA.


Word VBA

If you want to reference an Excel file from Word-VBA, you need the Excel.Application object additionally.
If Excel is already started, you can use the existing application object - otherwise you create one and make it visible.

Same with your Excel file: If it's already open, you use it - if not, you open it.

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

In Excel you can try to reference an already opened Word file directly as ActiveDocument without getting the Word.Application additionally.

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



回答2:


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:

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")


来源:https://stackoverflow.com/questions/56494639/copy-word-paragraph-to-excel-cells

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!