2010-09-03 73 views
6

我的MS Word 2007中的模板與在它的文件名頁腳。用戶將打開模板並執行「另存爲...」以創建文檔。觸發MS Word宏保存事件之後

我想在頁腳顯示的文件名立即更新至新的文件名。

是否有AfterSaveEvent或東西,我可以作爲一個鉤子用來啓動我的VBA腳本,並更新?

或者是有一個更簡單的方法?

回答

4

只需創建這樣一個宏(我相信,如果包括它工作更好地在Normal.dot)

Sub FileSaveAs() 
' 
' FileSaveAs Macro 
' Saves a copy of the document in a separate file 
' 
Dialogs(wdDialogFileSaveAs).Show 

'returns the name including the .doc extension 
ChosenFileNameAndExtension = ActiveDocument.Name 'Or use .FullName 

' Your code here 

End Sub 

將觸發只要用戶選擇「文件另存爲」

HTH!

+0

感謝您的回答。我的IT專家正在對它進行一些測試......如果它能正常工作,它將被視爲已被接受。 – blokeley 2010-09-08 09:30:12

+0

@blokeley請發表評論和結果。祝你好運! – 2010-09-08 12:24:30

+0

優秀,它第一次工作。我將在下面發佈其他人的完整解決方案。 – blokeley 2010-09-10 15:17:13

1

這個工作基礎上@belisarius的回答:

Sub UpdateAll() 
    Dim oStory As Object 
    Dim oToc As Object 

    'Exit if no document is open 
    If Documents.Count = 0 Then Exit Sub 
    Application.ScreenUpdating = False 

    For Each oStory In ActiveDocument.StoryRanges 
     oStory.Fields.Update 'Update fields in all stories 
    Next oStory 

    For Each oToc In ActiveDocument.TablesOfContents 
     oToc.Update 'Update table of contents 
    Next oToc 

    Application.ScreenUpdating = True 
End Sub 

Sub FileSaveAs() 
' 
' FileSaveAs Macro 
' Saves a copy of the document in a separate file 
' 
Dialogs(wdDialogFileSaveAs).Show 

UpdateAll 


End Sub 
+0

不錯!感謝您發佈它 – 2010-09-10 15:28:42

0

我們知道Aftersave事件不適用於Microsoft Word。但Word允許我們使用BeforeSave事件。我已經實現了這個解決方案,它工作正常。

首先我們要實現在Word BeforeSave事件Application.onTime方法如下

Private Sub mobjWord_DocumentBeforeSave(ByVal Doc As Word.Document, SaveAsUI As Boolean, Cancel As Boolean) 
    Word.Application.OnTime Now + TimeValue("00:00:02"), "AfterSave" 
End Sub 

此方法將調用2秒後叫AfterSave的方法。

Public Sub AfterSave() 
    While Word.ActiveDocument.Application.BackgroundSavingStatus <> 0 
     DoEvents 
    Wend 
'Implement your code here 
End Sub 

在這種方法中,while循環將循環直到文檔保存過程完成。所以你可以在while循環之後實現你的代碼。