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

允我心安 提交于 2019-12-23 05:29:17

问题


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!


回答1:


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 e.g.

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.



来源:https://stackoverflow.com/questions/34640262/vba-to-generate-a-default-file-name-when-save-as-is-selected-in-word-2016

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!