我正在研究_Workbook.SaveAs Excel的API
如果有一个同名文件,我需要一直替换。不应该有一个对话框询问我是否要覆盖该文件。
现在,代码是:
app->put_DisplayAlerts(LOCALE_USER_DEFAULT, VARIANT_FALSE);
activeWorkbook->SaveAs(filePath,
fileType,
vtMissing,
vtMissing,
vtMissing,
vtMissing,
Excel::xlNoChange);
app->put_DisplayAlerts(LOCALE_USER_DEFAULT, VARIANT_TRUE);
问题是这会禁用所有警报。我还想要其他提醒。
例如,当用户尝试将带有宏的工作簿保存为“.xlsx”格式时,应该有一个警告对话框,告诉用户不会包含宏。
问题
有没有办法为所有版本的Excel实现此目的?
提前致谢=]
答案 0 :(得分:1)
在Excel VBA中,您只需执行以下操作:
Excel.Application.DisplayAlerts = False
这是我在将文件保存到现有文件时通常使用的代码。 我现在也想查看我试图覆盖的文件是否已被其他用户通过以下函数打开以供编辑(我从SO获得):
Enum IsFileOpenStatus
ExistsAndClosedOrReadOnly = 0
ExistsAndOpenSoBlocked = 1
NotExists = 2
End Enum
Function IsFileReadOnlyOpen(FileName As String) As IsFileOpenStatus
'ExistsAndClosedOrReadOnly = 0
'ExistsAndOpenSoBlocked = 1
'NotExists = 2
With New FileSystemObject
If Not .FileExists(FileName) Then
IsFileReadOnlyOpen = 2 ' NotExists = 2
Exit Function 'Or not - I don't know if you want to create the file or exit in that case.
End If
End With
Dim iFilenum As Long
Dim iErr As Long
On Error Resume Next
iFilenum = FreeFile()
Open FileName For Input Lock Read As #iFilenum
Close iFilenum
iErr = Err
On Error GoTo 0
Select Case iErr
Case 0: IsFileReadOnlyOpen = 0 'ExistsAndClosedOrReadOnly = 0
Case 70: IsFileReadOnlyOpen = 1 'ExistsAndOpenSoBlocked = 1
Case Else: IsFileReadOnlyOpen = 1 'Error iErr
End Select
End Function 'IsFileReadOnlyOpen
因此,在Excel VBA中保存文件时,我会遇到以下内容:
Excel.Application.DisplayAlerts = False
If IsFileReadOnlyOpen(myFilePathName) <> ExistsAndOpenSoBlocked Then
Excel.ActiveWorkbook.SaveAs myFilePathName, , , , True
End If
Excel.Application.DisplayAlerts = True