PDA

View Full Version : [SOLVED:] Force an update to the filename & path



sts023
11-05-2013, 09:42 AM
Hi guys....
Anyone got any idea how to force an update to Word's Quickpart/Field/ Document Information - Filename field using VBA? I tried to record a Macro, but you can't right-mouse-click a field when recording a macro (normally rmc then 'Update Field' forces an update).
thanks in anticipation....

fumei
11-05-2013, 03:02 PM
When recording macros, and you can not seem to use a right-click, try using Shift-F10. This is the standard Windows keyboard equivalent to a right-click.

macropod
11-05-2013, 03:20 PM
Anyone got any idea how to force an update to Word's Quickpart/Field/ Document Information - Filename field using VBA?
Simply toggling 'Print Preview', either manually or by code, is sufficient.

sts023
11-06-2013, 02:59 AM
fumei - Thanks - that got me to where I wanted to be!

Macropod - The reason I wanted to look at the macro is that I'm fumbling about in Save/Save As/Save All, and the Field is the Filename and Path. If you haven't yet saved the document, not surprisingly the Filename and Path is just "Document1".
Curiously, even after saving as AnotherName.docx, the Filename and path Field doesn't seem to show the new details in print Preview or on the document unless the field is manually refreshed. Possibly yet another MS Word "feature"???

macropod
11-06-2013, 04:46 AM
That a print preview on its own doesn't work suggests your field is in the body of the document, rather than in the header/footer area. A fairly simple macro, which should update the field wherever it is, is:

Sub UpdateFields()
Application.ScreenUpdating = False
With ActiveDocument
.Fields.Update
.PrintPreview
.ClosePrintPreview
End With
Application.ScreenUpdating = True
End Sub

sts023
11-06-2013, 05:07 AM
Paul....

The field is, in fact, in the Footer!

I've noticed oddities before with field updates - for what it's worth, the MS Office version is 2010, and is an OEM version. The OS is Windows Vista Business 2007 (SP2).

I'll play around with installing your sample code (thanks!) in an appropriate place - looks like a "double whammy" routine - execute Fields.Update, then force and close a Preview ("to be sure, to be sure"!)...

macropod
11-06-2013, 02:12 PM
Hi Steve,

.Fields.Update only updates fields in the body of the document.
.PrintPreview and .ClosePrintPreview toggles the Print Preview, the same as you can do manually. Thus, if the code's working for a field in the footer, so too would toggling Print Preview manually...

sts023
11-07-2013, 05:57 AM
Hi again Paul (and any other interested parties)....

By inspection, I've discovered that:-

If you insert Filename & Path into a Footer in a Template, then open the Template in a mailmerge, fairly reasoably at the end of the mailmerge the document, never having been saved, cannot update the path.

On being saved (via the Save command or the Save Icon) the filename & path is not updated!
(Inspecting the document shows that the details in the footer have not been updated.)

However, on issuing a Print command after the Save, in the Preview (Word 2010), the filename & path HAS been updated. The new values show on the Print Preview, and also in the document.

If at this point you cancel the print and close the document, then open the saved document, the filename & path show the original (i.e. un-updated) value (usually "Document1").

To get round this, I've used Alt+F8 when viewing the document to set up code in the Template as follows. Note that the created Module name is "NewMacros":-

Option Explicit
Sub FileSave()
'
' FileSave Macro
' Saves the active document or template
'
ActiveDocument.Save
Call ForcePreviewAndResave
End Sub
Sub FileSaveAs()
'
' FileSaveAs Macro
' Saves a copy of the document in a separate file
'
Dialogs(wdDialogFileSaveAs).Show
Call ForcePreviewAndResave
End Sub
Public Sub ForcePreviewAndResave()
Application.ScreenUpdating = False
With ActiveDocument
.Fields.Update
.PrintPreview
.ClosePrintPreview
End With
Application.ScreenUpdating = True
ActiveDocument.Save
End Sub 'ForcePreviewAndResave
This is a very slightly modified version of the suggested code (thanks again, Paul), and seems to work OK.

All it is really doing is getting round my perceived problem by forcing a Print Preview then a second save following either a Save or Save As command (I'll think about the Save All later, as it's highly unlikely to happen in the current scenario).

Although this is irritating and potentially time consuming, it's the only pragmatic way I have found to solve the problem.

Unless, of course, you can think of a better way....

sts023
11-07-2013, 06:30 AM
Incidentally, because it can get very confusing when saving the Template during VBA development, I've changed the ForcePreviewAndResave subroutine to ignore Templates.

Public Sub ForcePreviewAndResave()
Dim intPtr As Integer
Dim strExt As String
intPtr = InStr(1, ActiveDocument.Name, ".")
strExt = Mid(ActiveDocument.Name, intPtr + 1)
If strExt = "dotm" Then Exit Sub
Application.ScreenUpdating = False
With ActiveDocument
.Fields.Update
.PrintPreview
.ClosePrintPreview
End With
Application.ScreenUpdating = True
ActiveDocument.Save
End Sub 'ForcePreviewAndResave
Incidentally, I know you could rewrite the additional code without the Dims, using

If Mid(ActiveDocument.Name, InStr(1, ActiveDocument.Name, ".") + 1) = "dotm" Then Exit Sub

but that makes my brain hurt, and I was always in favour of the KISS acronym, especially during development!