PDA

View Full Version : Word 2007 Footer



Dee Bee 19
10-07-2014, 02:53 AM
Hi - I'm a total bozo with VB but trying to learn!

I'm using the following code in Excel to print the Full Path and Date Last Saved in a spreadsheet footer.

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
ActiveSheet.PageSetup.LeftFooter = FullName & Space(40) & "Last saved: " & Format(Date, "dd-mm-yy") & " " & Time

End Sub

Could any kind person translate this so it can be used in a Word 2007 document? I assume it would go into "ThisDocument" pane.

Thanks in advance.

gmaxey
10-07-2014, 06:28 AM
I'm assuming that you have the code to create the application event class.


Private Sub oApp_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
With ActiveDocument
Sections(1).Headers(wdHeaderFooterPrimary).Range.Text = .FullName & Space(40) & "Last saved: " & Format(Date, "dd-mm-yy") & " " & Time
End With
End Sub

Dee Bee 19
10-07-2014, 07:01 AM
I'm assuming that you have the code to create the application event class.


Private Sub oApp_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
With ActiveDocument
Sections(1).Headers(wdHeaderFooterPrimary).Range.Text = .FullName & Space(40) & "Last saved: " & Format(Date, "dd-mm-yy") & " " & Time
End With
End Sub

Thanks for your quick reply. I have copied your code into a MS Word document by going to "Developer" "Visual Basic" and pasting it into Document1 - ThisDocument (Code).

I then saved the document but nothing subsequently showed in either the header or footer. I'm clearly doing something wrong - can you point me in the right direction please!

gmaxey
10-07-2014, 07:25 AM
You are going to have to create an application event class an put the code in the class. As best I can tell Excel doesn't have a built-in DocumentBeforeSave event, so someone must have created a class for you.

In a standard project module add the following code:


Option Explicit
Dim oAppClass As New clsApp
Public Sub AutoExec()
Set oAppClass.oApp = Word.Application
End Sub
Sub AutoNew()
Set oAppClass.oApp = Word.Application
End Sub
Sub AutoOpen()
Set oAppClass.oApp = Word.Application
End Sub


Insert a new class module and rename it clsApp

Insert this code:


Option Explicit
Public WithEvents oApp As Word.Application
Private Sub oApp_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
With ActiveDocument
Sections(1).Headers(wdHeaderFooterPrimary).Range.Text = .FullName & Space(40) & "Last saved: " & Format(Date, "dd-mm-yy") & " " & Time
End With
End Sub


See: http://word.mvps.org/FAQs/MacrosVBA/AppClassEvents.htm

Dee Bee 19
10-07-2014, 08:20 AM
You really are trying to help Greg so many thanks but I am still being a bozo. If you can spare the time could you walk me through the process in Word 2007 'cos I'm not even sure what a standard project module is! Sorry to be a pain. If not don't worry. Regards.

gmaxey
10-07-2014, 10:08 AM
Dee,

That link I provided is pretty much a walkthrough on its own. When you open the VB Editor (ALT+F11) you should see the Project explorer pane on the left side. If not use the View menu to show it. In that explorer pane you will see your Normal project and the project for whatever file you have open. Select your project and then the Insert menu to insert module (that is a standard module) and insert a class module. Rename the class module as indicated and paste in the code as instructed. Run one of the Auto macros to initialize the class.

snb
10-09-2014, 03:27 AM
An alternative:


Private Sub Document_Close()
ActiveWindow.ActivePane.View.SeekView = 4
ActiveDocument.StoryRanges(9).Text = ThisDocument.FullName & Space(40) & "Last saved: " & Now
ActiveWindow.ActivePane.View.SeekView = 0
End Sub

Dee Bee 19
10-10-2014, 06:14 AM
An alternative:


Private Sub Document_Close()
ActiveWindow.ActivePane.View.SeekView = 4
ActiveDocument.StoryRanges(9).Text = ThisDocument.FullName & Space(40) & "Last saved: " & Now
ActiveWindow.ActivePane.View.SeekView = 0
End Sub

Thanks to all who responded. Being new at this game it's the terminology I'm having difficulty with but I'll persevere. Thanks again.