简体   繁体   中英

VBA to generate a default file name when Save As is selected in Word 2016

I have VBA that, in Word 2010, will default a file name into the Save As screen when the user selects the Save As file option. Word 2016 (Office 365) has a different interface with a screen to select a save location (OneDrive, This PC,etc) prior to the "standard" Save As dialog box appearing which seems to have broken the code to generate a specified default file name.

The code (with title simplified) is

  Sub FileSaveAs()

  With Dialogs(wdDialogFileSaveAs)
    .Name = "MyTitle"
    .Show
  End With

  End Sub

If I run the code in Developer to Debug, it works fine, with the Save As screen appearing and the file name defaulting but it does not work in the live environment where the file name defaults to the previous document name.

Based on web research, I have tried additional code to change the properties of the document place the name in the title as this is supposed to then default when Save As is selected but that also doesn't work.

Any suggestions or recommendations on how to resolve this would be appreciated.

Thanks!

Update 1/11/16: Have been able to get a little closer by using the following: Application.DocumentBeforeSave Event example here https://msdn.microsoft.com/en-us/library/office/ff838299.aspx tied to Using Events with Application Object example here https://msdn.microsoft.com/en-us/library/office/ff821218.aspx

Had to add

Cancel=True

to the end of the event procedure code or the Save as Dialog box will open twice. Still a bit clunky and seems to be limited to run only before the first user generated save event but almost workable.

Wanted to share what I found in case it helps others but any other suggestions for improvement would be appreciated as it still doesn't seem like the best solution. Thanks again!

Thanks for reporting this. I have a potential workaround that I hope will help.

Could you use the SendKeys method ( https://msdn.microsoft.com/en-us/library/office/gg278655.aspx ) to send the keyboard shortcut (F12 for the Save As dialog, full list: http://aka.ms/Tf6yv0 ) to the application eg

App.SendKeys("{F12}")

when you need the Save As dialog to be displayed?

I hope this helps. Thanks again for bringing this issue to our attention, we will be looking further into the root cause.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM