0

我有一个 VBA 代码,它强制另存为对话框在尝试保存 xltm 时将默认另存为类型显示为 xlsm。请查看随附的代码,如果代码不正确,请纠正我

Application.EnableEvents = False 
Application.DisplayAlerts = False 
If SaveAsUI = True Then 
    bInProcess = True 
'The following statements shows the save as dialog box with default path
    Set FileSaveName = Application.FileDialog(msoFileDialogSaveAs)
    FileSaveName.InitialFileName = ThisWorkbook.Name
    FileSaveName.FilterIndex = 2   'select to save with a ".xlsm" extension
    FileSaveName.Title = "Save As"
    intchoice = FileSaveName.Show
    If intchoice = 0 Then
    Else
        FileSaveName.Execute
    End If
Else 'Normal Save 
    bInProcess = True 
    Cancel = True
    ThisWorkbook.Save 
End If
Application.EnableEvents = True
Application.DisplayAlerts = True

上面的代码在尝试使用 (ctrl+s) 保存时工作正常。如果我试图通过 excel 关闭窗口选项关闭。Excel 显示默认的另存为弹出窗口。如果我从该另存为弹出窗口中单击“保存”选项,则不会调用 workbook_beforesave 事件(显示另存为对话框,默认数据类型从 xlsm 更改为 xls)。我不知道我犯了什么错误?请帮我摆脱这个..

提前致谢!!!

4

3 回答 3

0

经过重新阅读和更多测试后,我了解到您问题中的代码已经在您创建的 Workbook_BeforeSave 事件中。你得到的第一个答案实际上是在正确的方向,你需要在 Workbook_BeforeClose 事件中添加额外的代码来处理右上角的 X。

您想要的是一个非常棘手的组合,并且很难在 Excel 中实现。其原因有几个方面。如果您使用右上角的 X 关闭工作簿,这将触发 Workbook_BeforeClose,文档预计将在该事件中关闭。如果由于某种原因用户取消关闭,这会给您带来另一个意外状态,当再次按下 X 时,Workbook_BeforeClose 似乎没有再次被触发,但现在 Workbook_BeforeSave(内置版本)被触发。

这是让您开始实现 xltm 保存的开始,但正如所说,它将受到限制,因为您强制用户保存工作簿并退出或不保存但仍退出工作簿。它有点脏(转到标签等),但你明白我的意思。

Excel 中有许多关闭/保存组合,很难捕捉到所有正确的组合,因此您可能想决定以完全不同的方式处理它......

Private Sub Workbook_BeforeClose(Cancel As Boolean)

  If ActiveWorkbook.Saved = True Then
     Cancel = False
  Else

     Dim iReply As Byte, iType As Integer

     Dim events As Boolean
     Dim alerts As Boolean

     events = Application.EnableEvents
     alerts = Application.DisplayAlerts

     Application.EnableEvents = False
     Application.DisplayAlerts = False

  StartQuestion:

     ' Define buttons argument.
     iType = vbYesNo + vbQuestion + vbDefaultButton2
     iReply = MsgBox("Would you like to save now?", iType)

     Select Case iReply
       Case Is = vbYes         ' user chose Yes save current workbook

         'The following statements shows the save as dialog box with default path
         Set FileSaveName = Application.FileDialog(msoFileDialogSaveAs)

         FileSaveName.InitialFileName = ThisWorkbook.Name
         FileSaveName.FilterIndex = 2   'select to save with a ".xlsm" extension
         FileSaveName.Title = "Save As ... "

         intchoice = FileSaveName.Show

         If intchoice = 0 Then
         Else
            FileSaveName.Execute
         End If

         If ActiveWorkbook.Saved = True Then
            ActiveWorkbook.Close
            Cancel = False
         Else
            GoTo StartQuestion
         End If

      Case Is = vbNo           ' user chose No, don't save

        ActiveWorkbook.Saved = True
        ActiveWorkbook.Close
        Cancel = False

     End Select

     Application.EnableEvents = events
     Application.DisplayAlerts = alerts

   End If

End Sub
于 2016-01-24T18:41:49.523 回答
0

感谢你的帮助。我想出了解决方案。

Private Sub Workbook_BeforeClose(Cancel As Boolean)

StartQuestion:
Cancel = True
'Evaluate if workbook is saved and emulate default propmts
With ThisWorkbook
     Select Case MsgBox("Do you want to save the changes you made to '" & .Name & "'?", _
         vbYesNoCancel + vbExclamation)
         Case Is = vbYes
             Call CustomSave(vbYes)
             If cancelclicked = False Then
                ThisWorkbook.Saved = True
             Else
                GoTo StartQuestion
             End If
         Case Is = vbNo
             ThisWorkbook.Saved = True
         Case Is = vbCancel
             Exit Sub
     End Select
End With
Cancel = False
End Sub

Sub CustomSave(ans As Long)
Dim MinExtensionX
Dim Arr() As Variant
Dim lngLoc As Variant
Dim events As Boolean
Dim alerts As Boolean
If ActiveWorkbook.Saved = True Then
     Cancel = False
Else
     events = Application.EnableEvents
     alerts = Application.DisplayAlerts

     Application.EnableEvents = False
     Application.DisplayAlerts = False

StartQuestion:
    Select Case ans
    Case Is = vbYes         ' user chose Yes save current workbook
        MinExtensionX = Mid(ActiveWorkbook.Name, InStrRev(ActiveWorkbook.Name, ".") + 1)
        Arr = Array("xlsx", "xlsm", "xlsb", "xls", "xml", "mht", "mhtml", "htm", "html", "xltx", "xltm", "xlt", "txt", "csv", "prn", "dif", "slk", "xlam", "xla", "pdf", "xps", "ods") 'define which extensions you want to allow
        On Error Resume Next
        lngLoc = Application.WorksheetFunction.Match(MinExtensionX, Arr(), 0)
        If IsEmpty(lngLoc) Then '
            'The following statements shows the save as dialog box with default path
             Set FileSaveName = Application.FileDialog(msoFileDialogSaveAs)

             FileSaveName.InitialFileName = ThisWorkbook.Name
             FileSaveName.FilterIndex = 2   'select to save with a ".xlsm" extension
             FileSaveName.Title = "Save As ... "

             intchoice = FileSaveName.Show
             If intchoice = 0 Then
                cancelclicked = True
             Else
                FileSaveName.Execute
             End If
        Else
            ThisWorkbook.Save
        End If
 End Select
 End If
 End Sub
于 2016-01-25T13:35:30.653 回答
0

您需要将代码放在这些行之间

Private Sub Workbook_BeforeClose(Cancel As Boolean)

End Sub
于 2016-01-24T17:10:01.747 回答