在工作簿保存之前如何捕获目标path?

我有一个工作簿“C:/abc.xlsm”。

然后我尝试将其保存为“C:/mno/xyz.xlsm”。

我想陷阱保存操作(我可以使用Workbook_BeforeSave来做到这一点),然后我需要看看所需的目标path是什么。

例如,我想能够做到像:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) Dim destinationPath As String destinationPath = CaptureDestionationPath If InStr(1, destinationPath, "C:") Then Dim msgboxAnswer As Integer msgboxAnswer = MsgBox("It seems you are trying to save the file on your local drive. Is that intended", vbYesNo + vbQuestion, "") If msgboxAnswer = vbNo Then Cancel = True End If End If End Sub 

至于我所尝试的:通过工作簿对象属性,并进行networkingsearch。

例如: Before-Save被取消,并显示您的另存为对话框。 这个对话框返回你可以validation的保存path。 最后事件被禁用,所以Me.SaveAs fileSaveName方法不会再次引发Before-Save事件。

注意:您应该添加variablesSaveAsUI if-check来区分Save-As和简单的Save。

 Option Explicit Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) Cancel = True Dim fileSaveName fileSaveName = Application.GetSaveAsFilename If fileSaveName = False Then Exit Sub End If If InStr(1, fileSaveName, "C:") Then Dim msgboxAnswer As Integer msgboxAnswer = MsgBox("It seems you are trying to save the file on your local drive. Is that intended", vbYesNo + vbQuestion, "") If msgboxAnswer = vbNo Then Exit Sub End If End If Application.EnableEvents = False Me.SaveAs fileSaveName Application.EnableEvents = True End Sub