【发布时间】:2019-07-13 02:26:24
【问题描述】:
为了将 Excel 工作簿导出为 .PDF 文件,当 .PDF 文件已创建并打开时,我收到错误 70 权限被拒绝。
错误出现在下面这行代码中:
Open filename For Input Lock Read As #filenum
我尝试通过更改模式(必需。指定文件模式的关键字:追加、二进制、输入、输出或随机)来修改 Open 语句 (https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/open-statement)。如果未指定,则打开文件以进行随机访问。 ) 和锁(可选。指定其他进程对打开文件的操作限制的关键字:Shared、Lock Read、Lock Write 和 Lock Read Write。)。但我仍然收到错误消息。
Sub exportPDF_Click()
Dim filename, filePath, PathFile As String
filename = "Name of the File"
filePath = ActiveWorkbook.Path
On Error GoTo errHandler
If Len(filename) = 0 Then Exit Sub
PathFile = filePath & "\" & filename & ".pdf"
' Check if file exists, prompt overwrite
If existFile(PathFile) Then
If MsgBox("The file already exists." & Chr (10) & "Overwrite
existing file?", _
vbQuestion + vbYesNo, "Existing File") = vbNo Then
Do
PathFile = Application.GetSaveAsFilename _
(InitialFileName:=filePath, _
FileFilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select a folder and a name to save the
file."
' Handle cancel
If PathFile = False Then Exit Sub
' Loop if new filename still exists
Loop While existFile(PathFile)
End If
End If
If fileOpened(PathFile) Then
GoTo errHandler
Else
ThisWorkbook.ExportAsFixedFormat _
Type:=xlTypePDF, _
filename:=PathFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
End If
Exit Sub
errHandler:
' Display a message stating the file in use.
MsgBox "The PDF file was not created." & Chr (10) & Chr (10) &
filename & ".pdf" & "has been opened by another user!"
End Sub
'=============================
Function existFile(rsFullPath As String) As Boolean
existFile = CBool(Len(Dir$(rsFullPath)) > 0)
End Function
'=============================
'=============================
Function fileOpened(PathFile As String)
' Test to see if the file is open.
fileOpened = IsFileOpen(PathFile)
End Function
'=============================
'=============================
' This function checks to see if a file is open or not. If the file is
' already open, it returns True. If the file is not open, it returns
' False. Otherwise, a run-time error occurs because there is
' some other problem accessing the file.
Function IsFileOpen(filename As String)
Dim filenum As Integer, errnum As Integer
On Error Resume Next ' Turn error checking off.
filenum = FreeFile() ' Get a free file number.
' Attempt to open the file and lock it.
Open filename For Input Lock Read As #filenum '<--- error line
Close filenum ' Close the file.
errnum = Err ' Save the error number that occurred.
On Error GoTo 0 ' Turn error checking back on.
' Check to see which error occurred.
Select Case errnum
' No error occurred.
' File is NOT already open by another user.
Case 0
IsFileOpen = False
' Error number for "Permission Denied."
' File is already opened by another user.
Case 70
IsFileOpen = True
' Another error occurred.
Case Else
Error errnum
End Select
End Function
'=============================
预期的结果是一个 MsgBox 说:
“未创建 PDF 文件。
File.pdf 的名称已被其他用户打开!"
我在这里缺少什么?
【问题讨论】:
-
您已经在代码底部的“Error errnum”之前注释掉了“case else”,因此如果文件打开,则该过程会进入错误 errnum。无论如何,那行应该是 RaiseError errnum。
-
对@HarassedDad 感到抱歉。我修复了它,但错误仍然存在。感谢您的评论。
-
Loop While existFile(PathFile)看起来很可疑。您正在继续Do循环,该循环提示用户获取新的文件名/路径,但在该循环中,您不会检查文件是否打开。 -
谢谢@DavidZemens。我修复了代码。如果您现在可以重现该错误,请告诉我。
-
不。如果文件不存在,我会得到一个 err.Number = 53。如果该文件确实存在,那么我在
If PathFile = False Then Exit Sub处得到一个 err.Number = 13。
标签: excel vba export-to-pdf