问题
I have created a Word document using VBA in Access by creating a new Word.Application, importing a document with Documents.Add and then making the desired changes. Once the changes are made the document is activated and Word is given focus.
I'm trying to change the suggested name that appears when a user first tries to click "Save As" in this document, but without opening the dialog immediately.
I have set a title using
.ActiveDocument.BuiltInDocumentProperties(wdPropertyTitle) = "Title"
once the document has been activated, but am experiencing some strange behaviour.
The first time I click "Save As" on the document, an automatically suggested name based on the contents of the document appears. However, when I close this dialog and click "Save As" for a second time, the correct name appears.
It appears that another user has experienced this issue here.
Is there an explanation/fix for this behaviour, and if not is there an alternative approach I could use?
And help would be much appreciated.
回答1:
This is what I came up with some years ago. It also sets .BuiltInDocumentProperties(wdPropertyTitle)
.
The key is to use, but not show, oWord.Dialogs(wdDialogFileSummaryInfo)
.
' Window title
sWindowTitle = "Document for " & sName
' http://wordmvp.com/FAQs/MacrosVBA/SetDefFilename.htm
With oWord.Dialogs(wdDialogFileSummaryInfo)
' Several characters will cause Word to truncate the default file name when saving
sClean = sWindowTitle
sClean = Replace(sClean , ".", "") ' Dr. John Smith
sClean = Replace(sClean , "-", " ") ' Anne-Marie Jones
' ... expand as needed ...
.Title = sClean
.Execute
End With
With oDoc
.ActiveWindow.Caption = sWindowTitle
End With
来源:https://stackoverflow.com/questions/64212093/vba-set-default-save-as-name-without-opening-dialog