将Word段落复制到Excel单元格

时间:2019-06-07 12:45:47

标签: excel vba ms-word

我试图将Word段落复制到Excel单元格,但是我挂断了电话 运行时错误9:下标超出范围。

我已经搜索过。我读过的所有内容都说找不到该文件,但是该文件位于同一文件夹中,并且名称拼写正确,并且扩展名正确。所以,我很沮丧。原始代码来自这里: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

此行出现错误:Set wb = Workbooks(“ new.xlsm”)

2 个答案:

答案 0 :(得分:0)

在同时使用两个应用程序时,应使用完整的声明,例如Word.DocumentExcel.Workbook(如果您已经引用了适当的库)。

可以打开没有路径的已打开Excel文件。

Paste:= ...参数属于上一行代码,因此您必须在前一行的末尾添加一个空白+下划线,或将它们放在一起成为一行。

请通过ws.Cells ...而不是Sheets(ws)引用工作表的单元格,因为您的“ ws”已经是工作表对象而不是字符串。

进一步的答案取决于您是从Word-VBA还是从Excel-VBA运行代码。


Word VBA

如果要从Word-VBA引用Excel文件,则还需要Excel.Application对象。
如果已经启动Excel,则可以使用现有的应用程序对象-否则创建一个对象并将其显示。

与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中,您可以尝试直接以ActiveDocument引用已打开的Word文件,而无需另外获取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

答案 1 :(得分:-1)

您需要指定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")