[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Smart file name

Posted on 2011-10-26
12
Medium Priority
?
493 Views
Last Modified: 2012-05-12
The document contained below has a smart file name feature built-in.  When either the Title or the Version Number in document's header are changed, the document gets renamed automatically.  This takes place when the document is either saved or closed.  What I need now is for the reverse to also happen.  When the document is manually renamed, the header on the inside of the document should automatically reflect that information when opened.

It would also be nice to have the 'Word Part' in the header that currently says 'Title' be changed to say "Smart File Name' since that is really what it is.  I have attached a version that illustrates what I am saying.  It is named docx but is actually a docm.
Smart-file-name-2.1.docx
0
Comment
Question by:rtod2
  • 6
  • 6
12 Comments
 
LVL 42

Expert Comment

by:dlmille
ID: 37036321
I was able to work in parallel on your new request and am already ready to post...

ok - this is it - I think the behavior is working as I (and hopefully you) would expect it to.  Including the new "save/rename" process on open, in case the file name needs modification from the start.

Here are the public modules:
Public alreadyHandling As Boolean
Function getTitle() As String
Dim tbl As Table
    Set tbl = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Tables(1)
    getTitle = GetCellText(tbl.Range.Cells(1))
End Function
Function getVersion() As String
Dim tbl As Table
    Set tbl = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Tables(1)
    getVersion = GetCellText(tbl.Range.Cells(2))
End Function
Function getFName() As String
    getFName = ActiveDocument.Path & "\" & getTitle & " " & getVersion & ".docm"
End Function
Function saveDoc()

    ActiveDocument.SaveAs2 FileName:=getFName, FileFormat:=wdFormatXMLDocumentMacroEnabled
    
End Function
Function GetCellText(wdCell As Word.Cell) As String
    Dim rng As Word.Range
    
    Set rng = wdCell.Range
    rng.MoveEnd wdCharacter, -1
    GetCellText = rng.Text
End Function
Sub tellMeName()
    MsgBox "The Path\Name of the Document would be: " & getFName, vbOKOnly, "Name of the document on close/save"
End Sub

Open in new window

Another public module and a class module, courtesy source: Rollin's post at http://forums.techguy.org/business-applications/536573-solved-ms-word-macro-before.html
PUBLIC MODULE:  BEFORE SAVE EVENT
Dim X As New Class1
Public Sub Register_Event_Handler()
    Set X.App = Word.Application
End Sub

Open in new window

CLASS MODULE: BEFORE SAVE EVENT
Public WithEvents App As Word.Application

Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)

    If Not alreadyHandling Then
        DoEvents
        Call saveDoc
        Cancel = True
    End If
    
End Sub

Open in new window

And, finally, in the ThisDocument codepage:
Private Sub Document_Close()
Dim xMsg As Long

    xMsg = MsgBox("Do you want to save before closing?", vbYesNo, "Hit NO to close without saving")
    If xMsg = vbYes Then
        alreadyHandling = True
        Call saveDoc
    End If
End Sub

Private Sub Document_Open()
    Call Register_Event_Handler
    alreadyHandling = True
    Call saveDoc
    alreadyHandling = False
End Sub

Open in new window


SmartDocument complete!  It turned out to be easier than I thought to rename the document on open - just had to tweak the global variable "alreadyHandling" kind of like one would with Excel's EnableEvents, so on open, we can do the SaveAs routine right at the start.

See attached.

Cheers,

Dave


title-version-number-r7.docx
0
 
LVL 42

Expert Comment

by:dlmille
ID: 37036324
Hang on - I get it (now ;).  You want the header and version number to change based on the filename?  tricky...

Let me take a better look at this.  I thought you wanted it the other way around - to save the file based on header+version on open, in case the file had been renamed...
Dave
0
 

Author Comment

by:rtod2
ID: 37036342
Not quite on that one.  Here is the video  >> http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Word/Q_27417819.html and the part that was missed.

It would also be nice to have the 'Word Part' in the header that currently says 'Title' be changed to say "Smart File Name' since that is really what it is.  I have attached a version that illustrates what I am saying.  It is named docx but is actually a docm.

See you tomorrow.  Thank you Dave!

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 42

Expert Comment

by:dlmille
ID: 37036349
I get it.  You'll get an error if there's no space in the filename.  I'll assume the last space separates title from version and stick it in on open.

Cheers,

Dave
0
 

Author Comment

by:rtod2
ID: 37036350
Dave, I wanted it both ways.  Re-read top post.  It's stated pretty well.

1. Change header reflected in file name
2. Change in file name reflected in header

3. Header Word Part should say "Smart File Name"
0
 
LVL 42

Expert Comment

by:dlmille
ID: 37036362
Yes - I read your post.  I thought my response was clear as well.  However, if I can say it back to you and you agree - then we're both aligned - agreed?

You can't have it both ways, simultaneously.

When the document is initally opened - the header/version will be updated based on the filename at the time of open

When the document is saved/closed -the document will be saved based on the header/version in the document.

Is this a correct reflection of your post?
0
 

Author Comment

by:rtod2
ID: 37036366
Dave thank you, yes.

We had posts crossing in the wind.
0
 
LVL 42

Accepted Solution

by:
dlmille earned 2000 total points
ID: 37036389
We just need a routine added on open that calls this:

 
Function updateHeaderVersion()
Dim tbl As Table
Dim thisTitle As String
Dim thisVersion As String

    If InStr(ActiveDocument, " ") <> 0 Then
        thisTitle = Left(ActiveDocument.Name, Len(ActiveDocument.Name) - InStr(StrReverse(ActiveDocument.Name), " "))
        thisVersion = Left(Right(ActiveDocument.Name, Len(ActiveDocument.Name) - Len(thisTitle) - 1), Len(Right(ActiveDocument.Name, Len(ActiveDocument.Name) - Len(thisTitle) - 1)) - 5)
        
        Set tbl = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Tables(1)
        PutCellText tbl.Range.Cells(1), thisTitle
        PutCellText tbl.Range.Cells(2), thisVersion
        
    Else
        MsgBox "Cannot rename file because there's no space separating title from version", vbOKOnly, "Continuing on anyway..."
    End If
    
End Function

Open in new window


Note - there must be at least ONE space in the file name.  The process ASSUMES that the LAST space in the file name separates the TITLE from the VERSION.  Otherwise, you'll get a popup error message and can rectify from there.

See attached.

Enjoy!

Dave
Smart-file-name-2.2.docx
0
 

Author Comment

by:rtod2
ID: 37036406
Hmm, no way to put a space if there isn't one there automatically, then process?  I'll check tomorrow.  Thank you again, Dave.  You're awesome!
0
 
LVL 42

Expert Comment

by:dlmille
ID: 37036409
You make the rules.  Think about it - what separates the title from the version number.  It currently is a space.  It could be a numeric value, but then, etc., etc.,etc.,  Need a hard and fast rule - think about the work process and what works best - so its simple to remember, implement, and support.

Tomorrow.

Dave
0
 

Author Comment

by:rtod2
ID: 37036424
Sounds good.  Whatever you think is best.  I'll go with that.
0
 

Author Closing Comment

by:rtod2
ID: 37036434
Damn Fine Work!
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Here are 5 resources that could help you become better in business writing.
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month11 days, 4 hours left to enroll

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question