2010-09-03 44 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循环之后实现你的代码。