Word VBA - How to set the default save path? - vba

When the following code is executed it changes the directory of the save as dialog and shows the Save dialog,
With Dialogs(wdDialogFileSaveAs)
.Name = "c:\newfolder\"
.Show
End With
And the following code changes the default directory of the open dialog and shows the Open dialog,
ChangeFileOpenDirectory "c:\newfolder\"
Options.DefaultFilePath(wdDocumentsPath) = CurDir
With Dialogs(wdDialogFileOpen)
.Show
End With
However, what I want to achieve is when the user clicks the Save or Save As icon on MS-Word it always needs to show the specific directory ("c:\newfolder\"). The above codes does not get executed when the Save or Save As icon clicked. I tried adding the code to the DocumentBeforeSave event but that shows the Save As dialog twice.
So, how to make MS-Word always so specific folder in Save dialog when the Save icon is clicked?

You can add the code to the DocumentBeforeSave event. It has ref parameter Cancel. After you display your dialog, you set
Cancel = True
This will cancel the following events so only your dialog is displayed.

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

How to handle and prompt file name during file save (Word 2016, VBA)

I establish filename base on file content (first paragraph).
When user click on ribbon file>Save As I want to prompt proper file name (even there is other name). How to handle and put in file name in window on attached screen?
My SaveAs backstage window
Try:
Sub FileSaveAs()
With Application.Dialogs(wdDialogFileSaveAs)
.Name = Split(ActiveDocument.Paragraphs(1).Range.Text, vbCr)(0)
.Show
End With
End Sub
The above code intercepts the Save As dialogue.

xlDialogSaveAs makes Excel crash

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

Outlook VBA insert line of text

I'm trying to write a piece of code (vba) that inserts a single line in the email i'm composing (open e-mail item). The code below is a first step i took
What works
if I run the code directly from the module (development window), the text is inserted.
What doesn't work
If I add the a macro (vba) in my ribbon and try to run it, nothing happens. The code only seems to work directly form the module (Play button).
What I want
Run macro (vba) from ribbon in active/open item;
Solution = A Module name can't contain a Macro of the same name.
Set font color to e.g. green;
Nice to have: insert text at bottom of page.
Code:
Sub InsertText()
Dim sText As String
sText = "Text to insert"
On Error GoTo ErrHandler
If TypeName(ActiveWindow) = "Inspector" Then
If ActiveInspector.IsWordMail And ActiveInspector.EditorType = olEditorWord Then
ActiveInspector.WordEditor.Application.Selection.TypeText sText
End If
End If
Exit Sub
ErrHandler:
Beep
End Sub
When you are adding it to the ribbon, are you adding it to the mail item ribbon or outlook ribbon? This gets confusing so I'm going to use mostly pictures to describe it.
This is it added to the mail item ribbon and it worked fine: -
If I place the button on the Outlook bar pushing the button does not work: -
This is because as soon as I push the button on the Outlook ribbon, the mail item is no longer the active window.
For confirmation, to add the button to to the mail item window, right click on the ribbon of a mail item and choose 'Customize the Ribbon...'
Press the 'New Tab' button in the lower right, change 'Choose commands from:' to 'Macros' and click on the macro in question from the list below it. Finally, click the 'Add > >' and then 'OK'.

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.