VBA代码从Word提取形式数据到Excel。突然,代码不再工作,但是没有错误或错误消息,为什么?

发布于 2025-01-30 15:38:22 字数 2420 浏览 4 评论 0原文

几个月前,我设置了下面的代码并进行了测试,并让同事对其进行了测试,而且工作正常。我刚刚打开了它,它不再有效,但是我没有对任何错误或错误提出警报。宏运行,我可以看到已格式化的相关单元格(即代码的最后一部分),但是MS Word似乎在任何时候都没有打开。我设置了一个简单的“文档”,以检查Excel是否仍在使用Word,这很好。谁能帮忙?

NB(1)我已在以下内容中编辑了细节,但是检查了文件夹目录和文件名是否正确。

NB(2)我最近从Windows 2010和相关的Office应用程序移动到365。但是,我们目前同时同时运行系统,并且问题似乎都出现在两者上。

NB(3)代码是当前的一个私人子附加到控制按钮的私人子,但是即使放入模块时,它也行不通。

NB(4)如果我介入F8,它将被卡住为“ WDAPP.QUIT”,并且我必须通过任务管理器关闭Excel,但是仍然没有错误警报。

NB(5)我删除了关闭和退出单词的说明,并且根据任务管理器似乎没有任何单词的实例。

请帮助:-)

代码:

Private Sub REDACTEDTITLE_Click()

Dim wdApp As New Word.Application
Dim myForm As Word.Document
Dim CCtl As Word.ContentControl
Dim myFolder As String, strFile As String
Dim myWorksheet As Worksheet, i As Long, j As Long

myFolder = "R:\REDACTED_FOLDER_PATH"

Application.ScreenUpdating = False

If myFolder = "" Then Exit Sub

Sheets("Data").Select
Set myWorksheet = ActiveSheet
ActiveSheet.Range("B13:CB27").Clear

i = myWorksheet.Cells(13, 2).End(xlUp).Row
strFile = Dir(myFolder & "\*.docx", vbNormal)

While strFile <> ""
i = i + 1

Set myForm = wdApp.Documents.Open(Filename:=myFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)

With myForm
j = 1
For Each CCtl In .contentcontrols
j = j + 1
myWorksheet.Cells(i, j) = CCtl.Range.Text

Next

End With

myForm.Close SaveChanges:=False
strFile = Dir()
Wend
wdApp.Quit
Set myForm = Nothing: Set wdApp = Nothing: Set myWorksheet = Nothing
Application.ScreenUpdating = True

Range("B13:CB27").HorizontalAlignment = xlLeft
Range("B13:CB27").VerticalAlignment = xlTop
Range("B13:CB27").WrapText = True
Range("B13:CB27").Interior.Color = RGB(197, 220, 243)
Range("B13:CB27").Borders(xlEdgeBottom).Weight = xlThin
Range("B13:CB27").Borders(xlEdgeLeft).Weight = xlThin
Range("B13:CB27").Borders(xlEdgeRight).Weight = xlThin
Range("B13:CB27").Borders(xlEdgeTop).Weight = xlThin
Range("B13:CB27").Borders(xlInsideHorizontal).Weight = xlThin
Range("B13:CB27").Borders(xlInsideVertical).Weight = xlThin
Range("B13:CB27").Borders(xlEdgeBottom).Color = RGB(255, 255, 255)
Range("B13:CB27").Borders(xlEdgeLeft).Color = RGB(255, 255, 255)
Range("B13:CB27").Borders(xlEdgeRight).Color = RGB(255, 255, 255)
Range("B13:CB27").Borders(xlEdgeTop).Color = RGB(255, 255, 255)
Range("B13:CB27").Borders(xlInsideHorizontal).Color = RGB(255, 255, 255)
Range("B13:CB27").Borders(xlInsideVertical).Color = RGB(255, 255, 255)

End Sub

I set up the code below several months ago and tested it, and had colleagues test it and it was working fine. I have just opened it and it no longer works, but I am not getting an alert of any error or bug. The macro runs and I can see the relevant cells being formatted (i.e. the final part of the code), but MS Word does not appear to open at any point. I set up a simple "Document.Open" sub to check that Excel was still working with Word, and that was fine. Can anyone help?

N.B. (1) I have redacted the specifics in the following, but have checked that the folder directories and file names are correct.

N.B. (2) I have recently moved from Windows 2010 and the associated Office applications to 365. However, we currently have both system running concurrently and the problem seems to appear on both.

N.B. (3) The code is current a Private Sub attached to an control button, but even when put into a Module it does not work.

N.B. (4) if I Step In with F8 it gets stuck as "wdApp.Quit" and I have to close Excel through Task Manager, but there is still no error alert.

N.B. (5) I removed the instructions to close and quit Word and there does not appear to be any instance of Word open according to task manager.

Please help:-)

Code:

Private Sub REDACTEDTITLE_Click()

Dim wdApp As New Word.Application
Dim myForm As Word.Document
Dim CCtl As Word.ContentControl
Dim myFolder As String, strFile As String
Dim myWorksheet As Worksheet, i As Long, j As Long

myFolder = "R:\REDACTED_FOLDER_PATH"

Application.ScreenUpdating = False

If myFolder = "" Then Exit Sub

Sheets("Data").Select
Set myWorksheet = ActiveSheet
ActiveSheet.Range("B13:CB27").Clear

i = myWorksheet.Cells(13, 2).End(xlUp).Row
strFile = Dir(myFolder & "\*.docx", vbNormal)

While strFile <> ""
i = i + 1

Set myForm = wdApp.Documents.Open(Filename:=myFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)

With myForm
j = 1
For Each CCtl In .contentcontrols
j = j + 1
myWorksheet.Cells(i, j) = CCtl.Range.Text

Next

End With

myForm.Close SaveChanges:=False
strFile = Dir()
Wend
wdApp.Quit
Set myForm = Nothing: Set wdApp = Nothing: Set myWorksheet = Nothing
Application.ScreenUpdating = True

Range("B13:CB27").HorizontalAlignment = xlLeft
Range("B13:CB27").VerticalAlignment = xlTop
Range("B13:CB27").WrapText = True
Range("B13:CB27").Interior.Color = RGB(197, 220, 243)
Range("B13:CB27").Borders(xlEdgeBottom).Weight = xlThin
Range("B13:CB27").Borders(xlEdgeLeft).Weight = xlThin
Range("B13:CB27").Borders(xlEdgeRight).Weight = xlThin
Range("B13:CB27").Borders(xlEdgeTop).Weight = xlThin
Range("B13:CB27").Borders(xlInsideHorizontal).Weight = xlThin
Range("B13:CB27").Borders(xlInsideVertical).Weight = xlThin
Range("B13:CB27").Borders(xlEdgeBottom).Color = RGB(255, 255, 255)
Range("B13:CB27").Borders(xlEdgeLeft).Color = RGB(255, 255, 255)
Range("B13:CB27").Borders(xlEdgeRight).Color = RGB(255, 255, 255)
Range("B13:CB27").Borders(xlEdgeTop).Color = RGB(255, 255, 255)
Range("B13:CB27").Borders(xlInsideHorizontal).Color = RGB(255, 255, 255)
Range("B13:CB27").Borders(xlInsideVertical).Color = RGB(255, 255, 255)

End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文