首页 > 解决方案 > 当使用“.paste”方法将数据从 MS Excel 中的单元格粘贴到 MS Word 表中的单元格时,为什么 VBA 会退出调试模式?

问题描述

下面的代码旨在用于从excel列中的单元格顺序复制字符串(i = 3到61),找到包含相同.doc文件的许多副本的目录文件夹,并将每个字符串粘贴到第二行,每个 .doc 文件中第一个表的第一列。

问题:程序un故意继续循环并在第一次执行以下行后完成其余代码的运行:

wddoc.Tables(1).Cell(2, 1).Range.Paste

即使我使用 F8 进入每一行代码以到达这行代码,也会发生这种情况。代码完成运行,而没有将任何内容粘贴到目录中的其余文件中。(excel文档第3行的字符串成功粘贴到计划模板-Copy(10).docx 但剩余的字符串没有粘贴到其余文件中)

编码:

Option Explicit
Sub CopyExcelToWord(path As String)

'variables----------------------------------------------------------------

'Decare Object variables for the Word application and file or documentl
Dim wdapp As Object, wddoc As Object, i As Integer

'Declare a String variable for the directory root and current file in that directory
Dim currentPath As String


'main process----------------------------------------------------------

'error handling is extremely important in making Excel work with Word
On Error Resume Next
'GetObject needs two parameters. The first is optional
Set wdapp = GetObject(, "Word.Application")
If Err.Number = 429 Then
Err.Clear
'we create a new instance of MS Word
Set wdapp = CreateObject("Word.Application")
End If

'Our application is made visible

wdapp.Visible = True

currentPath = Dir(path, vbDirectory)

For i = 3 To 61
Do Until currentPath = vbNullString
Debug.Print currentPath



        If Left(currentPath, 1) <> "." And Left(currentPath, 1) <> "" Then
        Debug.Print path & currentPath
           
            Sheet1.Range(Cells(i, 2), Cells(i, 2)).Copy
            'we activate our MS Word instance
            wdapp.Activate
            Set wddoc = wdapp.Documents(path & currentPath)
            If wddoc Is Nothing Then Set wddoc = wdapp.Documents.Open(path & currentPath)
            wddoc.Activate
            wddoc.Tables(1).Cell(2, 1).Range.Paste
            'Free alocated memory and close
            wdapp.Quit
            Set wddoc = Nothing
            Set wdapp = Nothing
        'The following line of code removes the cell selection in Excel
        Application.CutCopyMode = False
        currentPath = Dir()
        Else
        
        currentPath = Dir()
        
        End If


           
Loop


Next
End Sub

打印件(我在其中省略了一部分路径的地方放置了一个“ ... ”):

. . ... 计划模板 - 复制 (10).docx L C:**...**\ 计划模板 - 复制 (10).docx

该程序无意中运行了其余代码。excel 文档第 3 行中的字符串已成功粘贴到计划模板中 - Copy (10).docx,但其余字符串未粘贴到其余文件中)

plan template Copy (11).docx L C:* ...**\plan template - Copy (11).docx 课程计划模板 - Copy (12).docx L C:* ... \plan template -Copy (12) .docx 计划模板 - 复制 (13).docx L C:**... \计划模板 - L ... C:* ...**\计划模板 - 复制 (9).docx 课程计划模板.docx L C :* ...**\plan template.docx

标签: excelvbams-wordcopy-pasteonerror

解决方案


我不确定解决这个问题会解决你的问题,但你有

        wddoc.Tables(1).Cell(2, 1).Range.Paste
        'Free alocated memory and close
        wdapp.Quit
        Set wddoc = Nothing
        Set wdapp = Nothing

一旦你完成了 wdapp.Quit,你就不再有 wdapp,所以在你的“For i”循环的下一次迭代中,什么都不会起作用。

但是如果你想保存你的wddoc,你不能依靠Set wddoc = Nothing它来做。您需要执行显式关闭或保存并关闭

所以例如

        wddoc.Tables(1).Cell(2, 1).Range.Paste
        wddoc.Close -1 ' SaveChanges:=Word.wdSaveOptions.wdSaveChanges
        ' Only do this outside your "For i =" loop
        'Free alocated memory and close
        'wdapp.Quit
        Set wddoc = Nothing
        ' Only do this outside your "For i =" loop
        ' Set wdapp = Nothing

推荐阅读