Close Save As window without saving - vba

I want to save a Word document according to this condition:
IF 'check something' Then:
Dim myDialog As Object
Set myDialog = Dialogs(wdDialogFileSaveAs)
myDialog.Display
myDialog.Execute
End if
Continue doing all the rest...
If the Save As window opens and the user closes it without saving by clicking the (X) on the upper corner the sub continues to run as if the user decides to save the file.
How can I exit the sub if the user closes the Save As window?

Try this. I have used Excel VBA, but you can easily change it for Word.
Sub Test()
myDialog = False
myDialog = Application.Dialogs(xlDialogSaveAs).Show
If myDialog = False Then
MsgBox "Don't Execute"
Exit Sub
Else
MsgBox "Execute Stuff"
End If
MsgBox "After If Condition"
End Sub

Related

Hide sheets VBA - Excel bug?

I am currently doing a VBA code that needs to hide some sheets when the Excel file is closed and almost everything is working fine expects when I do the following steps:
make some change/insert data in the sheets
click the save button
make another change (that I do not want to save)
click to close the file and click not to save it
The problem is that I hide the sheets but since I do not save the file (because i do not want to save the changes made at step 3) the sheets are not hidden when I reopen the file. I cannot do this with the Workbook_open method because it is not allowed (at my project).
To do this I am rewriting the beforeclose method, as follows:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim Msg As String
Dim ireply As Integer
If Not Me.Saved Then
Msg = "Do you want to save the file?"
ireply = MsgBox(Msg, vbQuestion + vbYesNoCancel)
Select Case ireply
Case vbYes
Call hidesheets
Me.Save
Case vbNo
Me.Saved = True
Application.Quit
Case vbCancel
Cancel = True
Exit Sub
End Select
Else
Call hidesheets
Me.Save
End If
End Sub
Sub hidesheets()
ThisWorkbook.Sheets("Cars").Visible = xlVeryHidden
ThisWorkbook.Sheets("Brands").Visible = xlVeryHidden
ThisWorkbook.Sheets("Models").Visible = xlVeryHidden
ThisWorkbook.Sheets("Price").Visible = xlVeryHidden
End Sub
My questions is, it is possible just to save the hidden sheets configurations/settings without saving the information/data changed/inserted by the user?
PS: when I save the file and make any change the code works fine, i.e. hides the sheets.
Thank you all in advance
Regards
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
ThisWorkbook.Sheets("Cars").Visible = xlVeryHidden
ThisWorkbook.Sheets("Brands").Visible = xlVeryHidden
ThisWorkbook.Sheets("Models").Visible = xlVeryHidden
ThisWorkbook.Sheets("Price").Visible = xlVeryHidden
End Sub
You have to do the other way around
1) Set your workbook having those four sheets as very hidden per default
set them as such and then save your workbook to have it assume its default configuration
2) When you open it you make those sheets visible
Private Sub Workbook_Open()
ThisWorkbook.Sheets("Cars").Visible = True
ThisWorkbook.Sheets("Brands").Visible = True
ThisWorkbook.Sheets("Models").Visible = True
ThisWorkbook.Sheets("Price").Visible = True
End Sub
3) When you close it, you set those sheets back invisible
Private Sub Workbook_BeforeClose(Cancel As Boolean)
'Your code to be processed upon closing the sheet
'...
Call hidesheets '<--| hide your sheets
End Sub

Create a backup of a document

My macro should make a backup (in a specified path) of a file that is currently being saved, a procedure that is often discussed since it is a feature that Word lacks.
Private Sub Document_Close()
Dim blank As Range
For Each blank In ActiveDocument.StoryRanges
If Len(blank.Text) = 1 Then Exit Sub
Next
End Sub
Sub FileSave()
Dim BackupPath As String, objF As Object, retVal As Long, Rslt
BackupPath = "C:\Users\" & Environ("UserName") & "\Documents\BackupWord\"
With ActiveDocument
If .Path = "" Then: If Application.Dialogs(wdDialogFileSaveAs).Show <> -1 Then Exit Sub
If Len(Trim(.Range.Text)) = 1 Then Exit Sub
.Save
If Dir(BackupPath, vbDirectory) = "" Then
MkDir BackupPath
MsgBox "Backup folder has been created.", vbInformation
End If
If .Path & "\" = BackupPath Then
MsgBox "WARNING! Backup folder is the same as the source folder", vbExclamation
Exit Sub
End If
Set objF = CreateObject("Scripting.FileSystemObject")
retVal = -1
On Error Resume Next
retVal = objF.CopyFile(.FullName, BackupPath & .Name, True)
On Error GoTo 0
Set objF = Nothing
If retVal <> 0 Then MsgBox "Backup has not been copied to folder " BackupPath, vbExclamation
End With
End Sub
I will describe what the macro currently does.
FileSave procedure is intercepted.
The macro checks if an active document is saved. If it is, no extra action is required and the macro closes.
If an active document in not saved, the usual "Save As" dialogue appears. If the user chooses to not save the file then the macro closes.
If document is not saved, the macros saves it.
The macro looks for a backup folder. If it is not found, the macro creates it and shows a message box.
Then the macro checks if the source folder is the same as the backup folder. If they are the same, the macro shows a message and closes.
The active (current) document is copied to the backup folder. If it fails, a message box is displayed.
My macros fail to make a backup on two occasions.
When I open Word (no document opened, just blank page), modify it
and choose to close Word, a SaveAs dialog is shown. Then I choose to
save and the document is saved correctly but a backup copy is not
created.
When a document exists on, say, harddrive, pendrive etc.
and I will modify it and choose to close Word a SaveAs dialog is shown.
Then I choose to save and the document is saved correctly but a
backup copy is not created.
You would need a class, i believe at addin level to track all sheets, something like...
Option Explicit
Private WithEvents wd As Word.Application
Public Sub initialise(w As Word.Application)
Set wd = w
End Sub
Private Sub wd_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
' Your code here
End Sub
In your addin (.dotm) file you'd have the following
Option Explicit
Public c As clsCustomWord
Sub AutoExec()
Set c = New clsCustomWord
c.initialise Application
End Sub
Hope this helps

Preventing Excel prompt from Word VBA

I'm working with VBA on Word and Excel. I have the Word running a userform which will automatically open an Excel file. User should fill some data in Excel file and then go back to the Word userform. When user finish filling all fields in Word userform, it will run some VBA code on Word that copy data from Excel to Word. After finished, the Excel file will be closed automatically. Therefore, I need to prevent user from closing the Excel app manually.
In order to do that, I use these code in Excel VBA in Sub Workbook_BeforeClose. If user close the Excel application window, it will show a message box that ask whether the user is still working with the Word userform. The code as follows:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
answer = MsgBox("Are you still working with Word userform?", vbYesNo)
If answer = vbYes Then
Cancel = True
MsgBox "This workbook should not be closed. It will be automatically closed when you finish working with Ms. Word Template Userform."
Else
Application.ThisWorkbook.Saved = True
End If
End Sub
In the Word VBA, I have code to close the Excel file:
Sub closeExcelApp()
If Not excelApp Is Nothing Then
excelApp.DisplayAlerts = False
excelWb.Close savechanges:=False
Set excelWb = Nothing
excelApp.Quit
End If
Set excelApp = Nothing
End Sub
This Sub will be called when the Word VBA code done copying data from Excel to Word. However, calling this Sub will cause the Workbook_BeforeClose called. Meanwhile, I don't want the Workbook_BeforeClose called when I call this closeExcelApp sub from Word VBA.
Any suggestion?
You can just disable events:
Sub closeExcelApp()
If Not excelApp Is Nothing Then
excelApp.DisplayAlerts = False
excelApp.EnableEvents = False
excelWb.Close savechanges:=False
Set excelWb = Nothing
excelApp.Quit
End If
Set excelApp = Nothing
End Sub
As explained in comments, add this line on top of the modules : Public ClosingFromWord As Boolean and set this boolean to False when you start your code execution.
As you are working between apps, the easiest way will be to write the value of the boolean in a cell in Excel from Word and read/laod this value in the Workbook_BeforeClose to avoid going through the whole code of that routine.
And modify you code to look like this :
Sub closeExcelApp()
ClosingFromWord = True
excelApp.Workbooks(1).Sheets(1).Cells(1,1) = ClosingFromWord
If Not excelApp Is Nothing Then
excelApp.DisplayAlerts = False
excelWb.Close savechanges:=False
Set excelWb = Nothing
excelApp.Quit
End If
Set excelApp = Nothing
ClosingFromWord = False
excelApp.Workbooks(1).Sheets(1).Cells(1,1) = ClosingFromWord
End Sub
So while you execute closeExcelApp, the boolean will be set to True and the Workbook_BeforeClose won't be executed in his totality as it'll be exited with If ClosingFromWord Then Exit Sub :
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ClosingFromWord = Workbooks(1).Sheets(1).Cells(1,1)
If ClosingFromWord Then Exit Sub
answer = MsgBox("Are you still working with Word userform?", vbYesNo)
If answer = vbYes Then
Cancel = True
MsgBox "This workbook should not be closed. It will be automatically closed when you finish working with Ms. Word Template Userform."
Else
Application.ThisWorkbook.Saved = True
End If
End Sub

Autofill Read only Excel 2010 xlsm SaveAs file name box

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim saveIt As Boolean
If ThisWorkbook.Name = "Test.xlsm" Then
Sheets("Sheet1").Select
If Not IsEmpty(A1.Value) Then
MsgBox "This workbook is 'read only' Please rename this workbook."
strName = "Please enter a new file name."
ThisPAth = Application.ActiveWorkbook.Path
ThisFile = Range("B1").Value
ActiveWorkbook.SaveAs Filename:=ThisPath & ThisFile & ".xlsm", FileFormat:=52, CreateBackup:=False
Else
MsgBox "Cancelled."
End If
End If
End Sub
I have a password protected workbook (Test.xlsm" that is strictly for data entry. When the user opens the workbook as read only, enters the data, and then exits the workbook/template, I want the SaveAs dialog box that automatically pops up to have the contents of A1 of Sheet1 to be the "Suggested" file name that is autofilled in the SaveAs box.
I thought if I snagged the BeforeSave function that I could declare this path/filename but alas, it does not work. The autofill box displays "Copy of Test.xlsm". I do not even think it sees the above code.
How can I accomplish autofilling this box with the desired name. Thanks.
------------Update------------------
Rewrote code to below but it still is not snagging the default dialog box on save. Maybe I am misunderstanding the Workbook_BeforeSave function. I thought it was automatically called whenever the file is getting saved. I never want the user to save the file with the name Test.xltm (I changed file to a template to see if that made a difference) but to suggest the user rename file to the value in B1 for standardization reasons. The code is not getting called automatically. I was able to get similar code to work if I call it by executing a macro from the quick access toolbar for example, but cannot seem to get it to execute automatically whenever the user selects "Close", "Save" or "Save As" from the "File" pull down menu.
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim saveDialog As FileDialog
Debug.Print "Hello"
Set saveDialog = Application.FileDialog(msoFileDialogSaveAs)
If ThisWorkbook.Name = "Test.xltm" Then
Application.EnableEvents = False
Debug.Print "Save as"
Set saveDialog = Application.FileDialog(msoFileDialogSaveAs)
With saveDialog
.InitialFileName = "foo.xlsm"
.Show
End With
Application.EnableEvents = True
Else
Debug.Print "Cancel"
End If
End Sub
The template is password protected and opened as read only by user so SaveAs dialog box should always open upon exit/save/save as. Correct? And shouldn't the Workbook_BeforeSave always get called under this circumstance?
Example:
Sub saveDialogTest()
Dim saveDialog As FileDialog
Set saveDialog = Application.FileDialog(msoFileDialogSaveAs)
With saveDialog
.InitialFileName = "Foo.xlsx"
.Show
End With
End Sub
If you use the FileDialog like this, it will allow you to change the suggested file name.

ActiveX control Blocks Me.Saved=True

Previously I have used this:
Private Sub Document_Close()
Me.Saved = True
End Sub
to disable the save prompt when exiting a Word document and changes have been made, however I have added a "Combo Box (ActiveX Control)", and now Word is prompting to save again. Is there a way around this?
I've tried writing code to just delete the Combo Box when the document closes, but the box deletes itself after being used (not my code, it just does), then when the document closes the box doesn't exist and causes an error there. I could just do an if exist/error control, but I feel like that is just getting sloppy instead of finding the root problem... can someone help?
If had the same issue, and found the solution with Bing:
http://answers.microsoft.com/en-us/office/forum/office_2007-customize/activex-control-blocks-ms-word-vba/71eca664-8e43-4e4f-84c5-59154661ee5a
The following code helped me to get around this issue:
Dim WithEvents App As Application
Private Sub App_DocumentBeforeClose(ByVal Doc As Document, Cancel As Boolean)
'Did the user saved our file?
If Not ThisDocument.Saved Then
'Close our file?
If Doc.Name = ThisDocument.Name Then
'Cancel the dialog always!
Cancel = True
'Set the save state
ThisDocument.Saved = True
'Close the document after this event
Application.OnTime Now + TimeSerial(0, 0, 1), Me.CodeName & ".Document_AfterClose"
End If
End If
End Sub
Sub Document_AfterClose()
'Close the document without saving
ThisDocument.Close False
End Sub
Private Sub cboEquip_Change()
'Let the document know that a change is made
ThisDocument.Saved = False
End Sub
Private Sub Document_Open()
Set App = Application
End Sub