
因此,由於 VBA 沒有onSave
orafterSave
事件,我很難找到儲存文件並更新文件位置(儲存後)的方法。
因此,本文檔最常見的使用方式是:
- 從範本建立新文檔
- 編輯文檔
- 將文件另存為(網路中的某個位置)
我的老闆對他的所有文件感到困惑,很難再次找到它們。
所以他想看看這些文件保存在哪裡。
我在頁腳中新增了文檔位置字段,並在 VBA 專案中新增了此程式碼。
Sub AutoOpen()
'
' AutoOpen Macro
'
'
Dim aStory As Range
Dim aField As Field
For Each aStory In ActiveDocument.StoryRanges
For Each aField In aStory.Fields
aField.Update
Next aField
Next aStory
' set document as unchanged (prevents save dialog popping up when closing)
' further edits will set this back to false and restore
' the save dialog on close
ActiveDocument.Saved = True
End Sub
Private Sub wdApp_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
ActiveWindow.ActivePane.View.Type = wdPrintView
Application.ScreenUpdating = True
Selection.WholeStory
ActiveDocument.Fields.Update
ActiveDocument.Save
End Sub
一切正常,直到 activeDocument.Save 點,之前必須至少保存一次。
這也有點明顯,因為我假設 beforeSave 事件是在正確保存之前,所以沒有位置路徑......我能做些什麼來讓這件事正確?
答案1
該欄位將透過列印預覽進行更新。所以我使用的技巧是在 AutoOpen 巨集中建立列印預覽。
Sub AutoOpen()
Application.Run MacroName:="MathTypeCommands.UIWrappers.FilePrintPreview"
ActiveDocument.ClosePrintPreview
End Sub