gpt4 book ai didi

vba - EXCEL VBA : Ignore word document errors and read content

转载 作者:行者123 更新时间:2023-12-03 07:50:03 25 4
gpt4 key购买 nike

I am writing VBA code to read the word document content and paste them in excel sheets, everything is fine,

but while reading multiple word documents Some of the word document shows the error messages for example "Word could not fire an event", due to this the program hangs up.

Can anyone suggest a VBA code to ignore these type of error and read the word content.

I have my placed code below.


Dim oDoc As Word.Document
Set oDoc = GetObject("D:\176013(1).doc")
str = oDoc.Content.text
MsgBox (str)
oDoc.Close (0)

Based on the answers


Dim wdApp As Word.Application
Dim oDoc As Word.Document
Set wdApp = CreateObject("word.Application")
wdApp.DisplayAlerts = **** 'unable to give false here, it shows only three options wdAlertsNone, wdAlertsMessageBox, wdAlertsAll
Set oDoc = wdApp.Documents.Open("D:\176013(1).doc")
str = oDoc.Content.text
oDoc.Close (0)
wdApp.Quit False

最佳答案

尝试
Application.DisplayAlerts = False
如果使用其他应用程序对象,则必须在该对象上设置DisplayAlerts = False

Dim app As Word.Application
Set app = CreateObject("Word.Application")
Dim oDoc As Word.Document
app.DisplayAlerts = wdAlertsNone
On Error Resume Next
Set oDoc = app.Documents.Open("D:\176013(1).doc")
On Error GoTo 0
str = oDoc.Content.text
MsgBox (str)
oDoc.Close (0)
app.Quit

注意: On Error Resume Next语句有风险。它希望您知道跳过的错误。如果错误损坏了文档,则可能无法读取该文档的内容(在下一行中),但是语句 On Error GoTo 0将错误处理重置为默认值,因此,如果在该行中收到错误,错误将被显示。

关于vba - EXCEL VBA : Ignore word document errors and read content,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32203404/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com