Access 2007中的陷阱VB错误 [英] Trap VB Error in Access 2007

查看:82
本文介绍了Access 2007中的陷阱VB错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在运行Word对象模型之前检查Word文件是否已打开。在其他地方找到的VB代码说这应该工作但是当Word文件打开时,我无法捕获产生的VB错误70.似乎通常的访问错误捕获代码仅适用于Access生成的错误;不是那些由VB产生的。

任何想法都会受到赞赏。我想要捕获错误的原因是,如果在文件已经打开时调用打开文件的对象代码,程序只会挂起/沙漏。

我的代码如下:

公共函数IsFileLocked(sFile As String)As Boolean

出错OnTo Err_FileLocked

'下面是确定文件是否打开的位。
打开sFile用于二进制访问读写锁定读写为#1
关闭#1

Err_FileLocked:
如果Err.Number<> 0然后
MsgBox"E R R O R - 您尝试访问的文件", &安培; vbCrLf& "已经打开了。" &安培; vbCrLf& vbCrLf& _
"请关闭文件,然后重试",vbCritical,"IsFileLocked"

IsFileLocked = True
Err.Clear
结束如果

结束功能

公共子OpenFile()

On Error GoTo ErrorHandler

如果不是IsFileLocked(" c:\ temp) \这是一个test.docx")然后另外
退出功能
结束如果

对象作为Word.Application
将dim myDoc作为Word .Document

'关闭之前的任何链接 - 只是管家
设置objWord =无设置myDoc = Nothing

设置objWord = CreateObject(" Word) .Application")
设置myDoc = objWord.Documents.Open(" c:\ temp \这是一个test.docx")
objWord.Visible = True
End Sub

+ +++++++++++++++++++++++++++++++++ 70次运行,我无法捕获它

功能fIsDocFileOpen(ByVal strDocFileName As String)As Boolean
'******************** ***************************** * '名称:fIsDocFileOpen(功能)
'目的:检查Word文档是否已打开
'作者:Dev Ashish
'日期:1999年2月11日,05: 50:58 PM
'被叫:任何
'呼叫:无
'输入:strDocFileName:Word文档的完整路径
'输出:如果文件打开则为True,false否则
'****************
On Error GoTo ErrHandler
Dim intFree As Integer
intFree = FreeFile()
打开strDocFileName输入锁定读取为intFree
fIsDocFileOpen = False ExitEere:
On Error Resume Next
关闭#intFree
退出功能
ErrHandler:
fIsDocFileOpen = True
恢复ExitHere
结束功能

解决方案

我不确定你的问题属于哪个类别的论坛,但如果你的问题在VB.Net之外,请尝试以下链接

< a class = expcol> 对于VBA,Office(VSTO),宏和VBScript问题

kaymaf


I am trying to check whether a Word File is already open before running the Word Object model. VB Code found elsewhere says this should work BUT when the Word file is open, I cannot trap the resulting VB Error 70. It seems that the usual Access Error trapping code only works for Access generated errors; not those generated by VB.

Any thoughts would be appreciated. The reason I want to trap the error is that if the Object code to open the File is called when the file is already open, the programme just hangs/hourglass on.

My code is below:

Public Function IsFileLocked(sFile As String) As Boolean

On Error GoTo Err_FileLocked

'Below is the bit that determines if the file is open.
Open sFile For Binary Access Read Write Lock Read Write As #1
Close #1

Err_FileLocked:
If Err.Number <> 0 Then
MsgBox "E R R O R - The file that your are trying to access," & vbCrLf & "is already open." & vbCrLf & vbCrLf & _
"Please close the file and try again", vbCritical, "IsFileLocked"

IsFileLocked = True
Err.Clear
End If

End Function

Public Sub OpenFile()

On Error GoTo ErrorHandler

If Not IsFileLocked("c:\temp\This is a test.docx") Then
Else
Exit Function
End If

Dim objWord As Word.Application
Dim myDoc As Word.Document

' close any previous links - just housekeeping
Set objWord = Nothing
Set myDoc = Nothing

Set objWord = CreateObject("Word.Application")
Set myDoc = objWord.Documents.Open("c:\temp\This is a test.docx")
objWord.Visible = True

End Sub


+++++++++++++++

I have even tried this code variant but the same VB Error 70 runs and I cannot trap it

Function fIsDocFileOpen(ByVal strDocFileName As String) As Boolean
'************************************************* *
'Name: fIsDocFileOpen (Function)
'Purpose: Checks to see if the Word document is already open
'Author: Dev Ashish
'Date: February 11, 1999, 05:50:58 PM
'Called by: Any
'Calls: None
'Inputs: strDocFileName: Full path to the Word document
'Output: True if file is open, false otherwise
'*******************************************
On Error GoTo ErrHandler
Dim intFree As Integer
intFree = FreeFile()
Open strDocFileName For Input Lock Read As intFree
fIsDocFileOpen = False
ExitHere:
On Error Resume Next
Close #intFree
Exit Function
ErrHandler:
fIsDocFileOpen = True
Resume ExitHere
End Function

解决方案

Im not exactly sure which category of the forum your question belog to but try the link below if your question is outside VB.Net

For VBA, Office (VSTO), Macros and VBScript Questions

kaymaf


这篇关于Access 2007中的陷阱VB错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆