xlDialogSaveAs makes Excel crash - vba

I wanted to create a small macro that forces the user to use the SaveAs dialog in MS Excel 2010 ("MS Office Professional Plus 2010" in case it makes a difference) instead of just saving the file under the same name. I saved this procedure under the workbook object:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Not SaveAsUI Then
msg = "Use the 'Save As' dialog to save a new version of the file"
Style = vbOKCancel
Query = MsgBox(msg, Style)
If Query = vbOK Then
Application.Dialogs(xlDialogSaveAs).Show
ElseIf Query = vbCancel Then
Cancel = True
End If
End If
End Sub
It all works well: I press "Ctrl-S" and get the prompt. I click "OK" and use the SaveAs dialog to save the file under a different name. But as soon as I hit the "Save" button in the dialog, Excel crashes.
I am probably using the xlDialogSaveAs command in a wrong way but I just cannot figure out why this does not work. There are no error messages in the debugger. There is no other VBA code anywhere else in the workbook. I am trying to save the workbook as an .xlsm file (the SaveAs dialog correctly defaults to it).
Is there anyone out there who can help me?

Try,
If Query = vbOk Then
Application.Dialogs(xlDialogSaveAs).Show
End If
Cancel = True.
I suspect there is a problem trying to save in the original call and trying to save in the new dialog you open up

Related

Creating a Command Button in Word to Save As but the file format must be Macros-Enabled

I have little coding experience and I've trawled through these forums but can't find an answer specifically for Microsoft Word.
I've created two command buttons where the first requires the user to click and it enables them to Save As the form, then they will click the next button to 'Click to Send' which I've done the code easily for the 'Click to Send'.
The problem is with the Save As button is that I cannot get it to automatically change the default Save As type to "Word Macro-Enabled Document" as it currently keeps just defaulting to "Word Document"
I know there's ways of adding multiple functions to one click command buttons but I really want these separate for now. The current code I'm using for the Save As button is:
Private Sub CommandButton2_Click()
Application.FileDialog(msoFileDialogSaveAs).Show
End Sub
Any help will be immensely appreciated. Thank you!
This will prompt the SaveAs File dialog with .docm as the default file type:
Private Sub CommandButton2_Click()
With Application.FileDialog(msoFileDialogSaveAs)
.FilterIndex = 2 '.docm file format
.Show
If .SelectedItems.Count <> 0 Then
ThisDocument.SaveAs2 .SelectedItems(1), wdFormatXMLDocumentMacroEnabled
End If
End With
End Sub

Change print method from PDF to default printer for a form printout

Hi I am trying to get the print out of a userform. While printing its always prompting to save the userform (a prompt box is being displayed). I don't want it.
I want to get the printout directly after choosing the printer without saving it.
Code below:
Private Sub CommandButton2_Click()
CommandButton2.Visible = False
CommandButton1.Visible = False
Application.Dialogs(xlDialogPrinterSetup).Show
Me.PrintForm
CommandButton2.Visible = True
CommandButton1.Visible = True
End Sub
You may not like this answer, because it involves more workt, but you probably want the userform to add the data into a formatted worksheet and then use the printout method of the worksheet.
https://msdn.microsoft.com/en-us/vba/excel-vba/articles/sheets-printout-method-excel
Worksheets.("yourWorksheet").printout _
activeprinter:= yourPDFprinter, _
PrintTofFile:= True, _
PrToFileName:= filePathAndName
If you want to do it through the UserForm, you're probably going to end up making a bunch of Windows API calls.
At the end of your code you can force a workbook to close without saving any changes, type the following code in a Visual Basic module of that workbook:
Sub Auto_Close()
ThisWorkbook.Saved = True
End Sub
Because the Saved property is set to True, Excel responds as though the workbook has already been saved and no changes have occurred since that last save.
EDIT:
Now I know what you're after I am going to presume you've created a button press for your form, so I think something like https://www.mrexcel.com/forum/excel-questions/544657-specifying-printer-printing-userform-using-vba.html . This will help you with what you want, as it will show you how to change the default print method, form PDF.

Excel "Workbook_BeforeClose" event not firing again after canceled

Update: After more research, I found this duplicate question: Excel 2016 Workbook.BeforeClose event firing every other time bug. It seems I was using the wrong keywords, and that this is a bug, not a problem with my code. However, I cannot seem to download the version mentioned in the solution. I am running Windows 7 and using Microsoft Office 365 Pro Plus, and Office is stating that the most up to date version available is 16.0.6965.2105
I am trying to use the Workbook_BeforeClose event to test whether a checkbox is checked or not. If not, the user is prompted on whether they want to continue closing with out checking the box. If they choose "Yes," the sheet is cleared and the workbook is saved. If they choose, "No," the box is checked and, "Cancel" is set to true.
This works fine the first time the Workbook_BeforeClose event runs. However, the second time the sheet is closed, the standard Excel "Want to save your changes to..." dialogue box comes up and the Workbook_BeforeClose event does not fire. If I click cancel on the dialogue box and close the workbook a third time, the event fires. Something is being reset when, "Cancel," is clicked in the dialogue box, but I can't figure out what it is. My code is below:
Public Closing as Boolean
Sub Workbook_BeforeClose(Cancel As Boolean)
Debug.Print "Workbook_BeforeClose"
If Closing = True Then Exit Sub 'Closing is used as a switch to stop the event from looping on Application.ThisWorkbook.Close below
Closing = True
If Sheets(1).DraftCheckBox = False Then
If MsgBox("This file is not being saved as a draft. This workbook will be cleared if the draft box is not checked." & vbCr & vbCr & "Would you like to continue?", vbYesNo, "Warning") = vbYes Then
'If "Yes" selected
'Stuff happens here
Application.ThisWorkbook.Close savechanges:=True
Else
'If "No" selected
Sheets(1).DraftCheckBox = True
Cancel = True
End If
End If
Closing = False
End Sub
I know for a fact that Application.EnableEvents is set to True. I also know that the event itself is not firing because there is a stop on the very first line at "Debug.Print" This stop is activated after the first close and I can step through the code. It does not activate at all after the second close, either before or after the dialogue box. Is there anything that I should be doing to prevent the dialogue box from coming up and to force the Workbook_BeforeClose event every time the workbook is closed?
You are already closing the workbook you don't want to call close again, use Save instead.
You also don't need the extra variable if you use the code below. One big question remains...how is your user imitating the Close event? If by the red X then the code below should work. If by a button on your form what does that button code look like?
Note: This is untested code as I don't have your workbook.
Sub Workbook_BeforeClose(Cancel As Boolean)
If Sheets(1).DraftCheckBox = False Then
If MsgBox("This file is not being saved as a draft. " + _
"This workbook will be cleared if the draft box is not checked." _
& vbCr & vbCr & "Would you like to continue?", _
vbYesNo, "Warning") = vbYes Then
'If "Yes" selected
'Stuff happens here
Application.ThisWorkbook.Save
Cancel = False 'Just to be sure.
Else
'If "No" selected
Sheets(1).DraftCheckBox = True
Cancel = True
End If
End If
End Sub
HTH

Prevent "Save" in Word (Save As is okay)

I'm just wondering how to prevent (stop) a user from saving a document (just "Save", "Save As" is okay). I have a .docm that I'm using as a template, where the user just fills in three fields. I don't want them to accidentally fill it in, then save over the template.
I've seen a few SO threads that give code, but when I put the following in "ThisDocument" under "Microsoft Word Objects", nothing happens when I hit save:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
MsgBox ("Please 'Save As' instead.")
Debug.Print "Someone tried to save."
Cancel = True
End Sub
I'm expecting when I hit the save icon (or CTRL+S) the messagebox to pop up. ..that doesn't happen (nor does the debug.print line work). What am I overlooking?
Related question: Should I just forget doing the above, and just create a true template document (.dotm)?
Yes, your users should be creating new documents from a template. Save the template in the users' Templates folder so that they can access it via File/New when they want to create a new document...
Concerning the code you show: Workbook is for Excel and won't trigger in Word. In Word the event is Document_BeforeSave and you'd want to set the SaveAsUI parameter to True to force the dialog box to show.

Show Excel 2007 Ribbon in XLS file using Excel VBA

I have an excel dashboard which works such that before the excel file is closed, I would like to display all the EXCEL ribbon, so that next time excel is opened, the application / excel will show the ribbon. At present, it does not show the ribbon if excel is opened.
Private Sub Workbook_BeforeClose(cancel As Boolean)
On Error Resume Next
Application.ScreenUpdating = True
ActiveWindow.DisplayWorkbookTabs = True
Application.DisplayFormulaBar = True
Application.DisplayFullScreen = False
Application.DisplayStatusBar = True
Application.DisplayScrollBars = True
Application.ScreenUpdating = True
Sheets("Introduction").Select
End Sub
This is an .xls file with Macro and supposed to work in Excel 2003 and Excel 2007.
Also, if "Cancel" is clicked, I do not want to show any of the above / ribbon, as user is supposed to get a protected view of the excel dashboard.
If the ribbon is closed by default, you can open it again by double clicking on one of the tabs (for instance, the Home tab).
(See this for more details).
If, however, you wish to write an event to take place when the workbook opens, then use the Workbook_Open() event from the ThisWorkbook Excel Object.
try this Application.ExecuteExcel4Macro " show.toolbar(""Ribbon"",true)"
to hide
Application.ExecuteExcel4Macro " show.toolbar(""Ribbon"",false)"