PDA

View Full Version : Solved: AutoNew FileName Update



TripleT
05-04-2005, 04:46 PM
Hi

I'm not very good at VBA, but I do try to give it my best shot.

What I'd like to do is update the filename field, which is contained within the first page footer. I've tried to contain the field within a bookmark in the hope that the update could be easier, unfortunately that also proved to be problematic, and I shouldn't use bookmarks (for some reason they conflict with other templates???).

I have the code below, which does what I want, however the screen "flickers" as it flips between the view panes.


Sub AutoNew()
ActiveDocument.Bookmarks("FileName").Select
Selection.Fields.Update

ActiveWindow.ActivePane.Close

With ActiveWindow.View
.Type = wdPrintView
End With
End Sub



Is there any way that I can update this field cleanly so people using the template don't really know what's going on?

Cheers, Triple T

Killian
05-04-2005, 05:13 PM
Hi and welcome to VBAX :hi:

unless it causes some other issue, you might just find it easier to update all the fields and not bother with the bookmark.
Also you can turn screenupdating off to minimize the flicker Application.ScreenUpdating = False
ActiveDocument.Fields.Update
'do some other stuff?
Application.ScreenUpdating = True

TripleT
05-04-2005, 05:19 PM
Hi - Thanks for the quick reply :)

My original code updated all fields

Sub oldAutoNew()
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
End Sub


But this caused problems for a lot of people because they didn't want certain fields updated. So that's why I just need the filename field updated *sigh*

I think I might have found something that works okay:


Sub AutoNew()
'http://www.visualbasicforum.com/showthread.php?p=718255#post718255
Dim Story As Variant
Dim rngNext As Word.Range

' Iterate through all story types
For Each Story In ActiveDocument.StoryRanges
' Only update fields in a footer
If Story.StoryType = wdPrimaryFooterStory Or _
Story.StoryType = wdFirstPageFooterStory Or _
Story.StoryType = wdEvenPagesFooterStory Then
' Update fields in this footer
Story.Fields.Update
' There may be linked footers so update them as well
Set rngNext = Story.NextStoryRange
Do Until rngNext Is Nothing
' Update fields in this footer
'conduct a find/replace, etc.
rngNext.Fields.Update
' Link to next story (if any)
Set rngNext = rngNext.NextStoryRange
Loop
End If
Next

End Sub


However even with Application.ScreenUpdating = True & False, I still get the "flicker". Any ideas?

Cheers

Killian
05-04-2005, 06:19 PM
that seems to do the job.
Are you turning off screenupdating at the start?Dim rngNext As Word.Range
Application.ScreenUpdating = False
' Iterate through all story types
For Each Story In ActiveDocument.StoryRanges
' Only... and switch back on (True) at the end. the refresh should then just be one 'flicker' at the end.

Actually, it wouldn't surprise me if updating fields cancelled/ignored the screenupdating setting... this is the kind of undocumented "feature" that makes word vba so much fun :doh:

TripleT
05-04-2005, 08:00 PM
Hi

I think that I've settled on the following code:

Sub AutoOpen()

Dim Story As Variant
Dim rngNext As Word.Range

Application.ScreenUpdating = False

' Iterate through all story types
For Each Story In ActiveDocument.StoryRanges

' Only update fields in a footer
If Story.StoryType = wdPrimaryFooterStory Or _
Story.StoryType = wdFirstPageFooterStory Or _
Story.StoryType = wdEvenPagesFooterStory Then

' Update fields in this footer
Story.Fields.Update

' There may be linked footers so update them as well
Set rngNext = Story.NextStoryRange
Do Until rngNext Is Nothing

' Update fields in this footer
'conduct a find/replace, etc.
rngNext.Fields.Update

' Link to next story (if any)
Set rngNext = rngNext.NextStoryRange
Loop
End If
Next

Application.ScreenUpdating = True

End Sub



I still get the flicker, but I guess it will just be one of those special "features".

Thanks for all your help.
Cheers

MOS MASTER
05-05-2005, 05:37 AM
Hi, :D

Your sub targets to many story's! (That's why your chances on screen flickering are bigger)

If you're using firstpage is different (Page set-up) you can just use:
Sub UpDateFileNameField()
Dim oField As Word.Field
Dim oRange As Word.Range
Set oRange = ActiveDocument.Sections(1).Footers(2).Range

If oRange.Fields.Count >= 1 Then
For Each oField In oRange.Fields
If oField.Type = wdFieldFileName Then
oField.Update
End If
Next
End If
End Sub

This Sub will only update fields that are of type wdFieldFileName (Filepath)

It also only targets the firstpage footer only

If you are using another footer type you can change the 2 to 1 or 3 to get the other footer story's.

You can add the Screenupdating is False to adjust for any left over flickering

Enjoy! :whistle:

fumei
05-05-2005, 11:23 AM
I am having a stupid day, so pardon me for asking...but if this is AutoNew that seems to me to imply that this is a new document created from a template.

If so, AutoNew fires on the creation of a new document. In which case, the document does not have a filename. How can you update the field sucessfully. On AutoNew it has not be saved yet.

What am I missing?

MOS MASTER
05-05-2005, 11:35 AM
I am having a stupid day, so pardon me for asking...but if this is AutoNew that seems to me to imply that this is a new document created from a template.

If so, AutoNew fires on the creation of a new document. In which case, the document does not have a filename. How can you update the field sucessfully. On AutoNew it has not be saved yet.

What am I missing?
Doesn't seam to be a stupid question Gerry! You're off course right on this! :thumb

I had the same thought when I posted my code but when I looked at the OP's last entry, he renamed his code to AutoOpen...(So I assumed in an auto open my add would work)

TripleT
05-05-2005, 01:15 PM
Hi

The templates I have created all have the filename field as standard in the first page footer. I'm using the same code for AutoNew and AutoOpen.

I know this seems particularly strange. Without the code, the filename field doesn't update and retains the "template" location path. People that use the template/s don't like that. I've tried explaining that this field can be manually updated, or when they re-open it updates, or if they print it will update - but they don't like that either.

The users seem happier to see a filename like "Doc2" than the template path. Rather bizarre :)

So I understand where you are coming from - I think it's rather odd too :)

Thanks to everyone for your help - I really appreciated it.

Cheers, Triple T

fumei
05-05-2005, 01:31 PM
I still think you may be using template files improperly. However, here is a possible alternative for getting the filename into the footer.
Private Sub Document_New()
Selection.Sections(1).Footers(wdHeaderFooterPrimary).Range.Text = _
ActiveDocument.FullName
End Sub

Private Sub Document_Open()
With Selection
.Sections(1).Footers(wdHeaderFooterPrimary).Range.Text = _
""
.Sections(1).Footers(wdHeaderFooterPrimary).Range.Text = _
ActiveDocument.FullName
End With
End Sub
This is in the .DOT file, that is, the template.

So, using a template file properly, cloning the .dot file, NOT opening it...

Document_New - which creates the new clone will, in fact, put

"Document1" in the footer. Or whatever is the current document index number. NO path is included because technically there IS no path.

The cloned file is saved. When the file is opened, Document_Open fires and deletes the old footer, and inserts the new footer with the document path and name. Everytime the file is opened it will do this. Therefore even if you make changes and do a SaveAs to a different name, it will still delete and rewrite the current file fullname into the footer when it is opned.

MOS MASTER
05-05-2005, 01:39 PM
Hi, :D

I'm confused now...
Just for me to comprehend what you want to save?

Is it always the path to the template?
Or the path of the new file that's based on the template? (So the path that get's created when a user saves that new file)

:whistle:

fumei
05-05-2005, 01:46 PM
Who are you confused with? Me or TripleT?

I am a little confused myself, because I am not sure if the template file is being opened. I am not even totally sure if it IS a .DOT file.

MOS MASTER
05-05-2005, 01:51 PM
Who are you confused with? Me or TripleT?
So sorry Gerry...I'm confused with TripleT



I am a little confused myself, because I am not sure if the template file is being opened. I am not even totally sure if it IS a .DOT file.
Well if that AutoNew macro off him worked than we can assume it's a *dot file

But I just really would like to now when and what should be saved because it's starting to be a headspinner...:bug:

TripleT
05-05-2005, 01:54 PM
Hi

A bit of background to this whole thing follows... (Apologies for it being a little long winded).

The organisation in which I work has just upgraded from Office 97 to Office 2003. In the past organisational templates (which were pretty substandard) were provided on an intranet, and employees were required to save them to a personalised location (whatever that meant to them - eg a c:, g:, or h: drive).

With upgrading to Office 2003 we had an opportunity to improve how the organisational templates were distributed, and of course "corporatise" the normal.dot. The normal.dot was modified to reflect corporate styles, fonts, spacing, etc. Each user profile receives a copy of this normal.dot which is saved in their own h: drive. Corporate templates (eg memo, fax, letterhead, etc) are saved as read only templates in a j: drive. All users have a shortcut in their h: to j: so that these templates are accessible. The centralisation of these templates means that users can access them quickly, and that the branding department can also make sure that the current look and feel is immediately available to users.

Normal.dot, and other corporate templates all have the file name in the footer. This was an organisational requirement. Users can modify normal.dot, but not the other templates.

In the past I have used autoopen, and autonew to control various actions.

These templates needed enough flexibility that if users modify the footer information in the resulting document (based on the template) that only the filename would update (if they decided to keep that). There were many disagreements around what type of information should be in the footer, eg version #, full file path, document name, print date, save date, etc. So the decision was made to include the full file path, and give the users flexibility to add other aspects if that is what they wanted.

Hope this all makes sense, and/or provides more clarity to the situation.

Cheers, TripleT

MOS MASTER
05-05-2005, 02:06 PM
Hi, :D

Ah just enjoyed some tea I'm back now...
Yess it does answers some questions but sorry dum Dutchmen over here! :rofl:

Can you please tell me if it's the template path you'd like in the newly created document or the path that the documents get when that documents get saved by the user? (So that the user knows where his saved file is at?)

Sorry for this question! :whistle:

TripleT
05-05-2005, 02:13 PM
Hi :)

Ultimately I would like the full path of the filename that is generated when the document is saved. Obviously when they create a document based on a template there isn't a full filename, so in that instance I am happy with "Document2" (or similar). The existing code does cover these aspects well.

The users do not want to see the full template filename sitting in their document.

The only odd thing - and it could just be a matter of the macro type (eg AutoOpen, AutoNew), is that when Word is first opened (obviously it uses normal.dot) the template name presents itself in the document. However, when they create a new blank document - the resulting document has "Document2" as expected. Should I be using Document_New, or Document_Open as well?

Hope I've still managed to keep things clear. It's one of those mind boggling situations.

Cheers, TripleT

MOS MASTER
05-05-2005, 02:24 PM
Hi, :D

Now I get it! You want the path when the document is saved.

To get this solved you should execute the code when the document get's saved. Or technically after that event When FullName is acctually Full! :rofl:

Get you're Automacro's out of there and put this in there:

Option Explicit
'Runs when save is pressed on the toolbar
Sub FileSave()
On Error GoTo Oops
ActiveDocument.Save

If ActiveDocument.Type = wdTypeDocument Then
UpDateFileNameField
End If
Oops:
End Sub

'Runs when save as is pressed in File menu
Sub FileSaveAs()
On Error GoTo Oops
Dialogs(wdDialogFileSaveAs).Show

If ActiveDocument.Type = wdTypeDocument Then
UpDateFileNameField
End If
Oops:
End Sub

Sub UpDateFileNameField()
Dim oField As Word.Field
Dim oRange As Word.Range
'Change Footers(1) to the footer your using e.g. 1 or 3
Set oRange = ActiveDocument.Sections(1).Footers(2).Range

If oRange.Fields.Count >= 1 Then
For Each oField In oRange.Fields
If oField.Type = wdFieldFileName Then
oField.Update
' oField.Unlink 'Uncomment if you whanna change to text!
End If
Next
End If

If ActiveDocument.Saved = False Then ActiveDocument.Save
End Sub


Now the filename will update if the user presses Save or SaveAs!

Important the macro UpDateFileNameField will only run if the document is a *doc file not a template! (See: If ActiveDocument.Type = wdTypeDocument Then)

If you want it to update the template as well if you alter it than loose the If Statement I just mentioned!

Please read my comments to reference the correct Footer there can be 3 Footers! I'm using Footers(2) That is the one you get if you choose First page different in page Set-up!

That's my 2 cents worth! :whistle:

TripleT
05-05-2005, 02:29 PM
Hi

Thanks for the help - that was absolutely awesome!

Cheers, TripleT

MOS MASTER
05-05-2005, 02:33 PM
Hi

Thanks for the help - that was absolutely awesome!

Cheers, TripleT
You're Welcome! :beerchug:
Don't forget to mark your thread solved?

fumei
05-06-2005, 07:24 AM
Uh.....you seemed to have missed my post.

My code put the document1 in the footer when it is first created...because that it IS the name and path.

When the document is saved, closed then reopened later, the footer will ALWAYS show the current filename and path.

Now, it is true that with my code, it only updated when the document is opened again. The assumption was Save..Close.

But OK. You can, as Joost did, do a rewrite of the FileSave command.

But you can still do it like:

Sub FileSave()
Selection.HomeKey Unit:=wdStory
With Selection
.Sections(1).Footers(wdHeaderFooterPrimary).Range.Text = _
""
.Sections(1).Footers(wdHeaderFooterPrimary).Range.Text = _
ActiveDocument.FullName
End With
End Sub

MOS MASTER
05-07-2005, 11:04 AM
Hi Gerry, :D

You're right both sollution's will work! :whistle: