Auto name document

Is it possible to have the file name of a document automatically assume it's title + version number from within the document instead of when the file is closed.  In the case of the attached, I would want the file to automatically assume the file name of Procedure Documentation Template 1.1
title-version-number.docx
rtod2Asked:
Who is Participating?
 
dlmilleConnect With a Mentor Commented:
I converted the file to a .docm (probably not ideal, but I need to head home).  Tell a story on how you're managing this and perhaps I can help, re: could this be a template with macro such that when the file is closed, its is named at that point (no longer requiring the macro, thus can be saved as docx?)

Think about the work process - or share it, and I can help you customize.

In the meantime, I put the code in a VBA module and saved the file as a .DOCM.  Open it and test it, when you close it it does the save as the way you want (except I kept it as docm as I'm not sure where you're headed with this, yet).


NOTE - I COULD NOT UPLOAD the DOCM, so I renamed it - after you download it, RENAME to .DOCM before you open it.

Cheers,

Dave
title-version-number-r1.docx
0
 
GrahamSkanRetiredCommented:
The only way to name a document is to save it as the new name. This code reads the text from the header.

Note that the dot is replaced with an underscore.
Sub NameDoc()
Dim strTitle As String
Dim strVersion As String
Dim tbl As Table
Set tbl = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Tables(1)

strTitle = GetCellText(tbl.Range.Cells(1))
strVersion = GetCellText(tbl.Range.Cells(2))
ActiveDocument.SaveAs "C:\MyFolder\" & strTitle & Replace(strVersion, ".", "_") & ".docx"
End Sub

Function GetCellText1(wdCell As Word.Cell) As String
    Dim rng As Word.Range
    
    Set rng = wdCell.Range
    rng.MoveEnd wdCharacter, -1
    GetCellText = rng.Text
End Function

Open in new window

0
 
rtod2Author Commented:
Ok, perfect!  I'd still rather have the dot though.  How do I change that and implement it?
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
GrahamSkanRetiredCommented:
Dots are confusing in file names except to separate the extension from the name. To keep it, just replace the SaveAs line

ActiveDocument.SaveAs "C:\MyFolder\" & strTitle & strVersion & ".docx"
0
 
rtod2Author Commented:
So you are suggesting this if I wanted to save the dot?  What steps do I take to implement the solution?
Sub NameDoc()
Dim strTitle As String
Dim strVersion As String
Dim tbl As Table
Set tbl = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Tables(1)

strTitle = GetCellText(tbl.Range.Cells(1))
strVersion = GetCellText(tbl.Range.Cells(2))
; use dot instead of dash
; ActiveDocument.SaveAs "C:\MyFolder\" & strTitle & Replace(strVersion, ".", "_") & ".docx"
ActiveDocument.SaveAs "C:\MyFolder\" & strTitle & strVersion & ".docx"
End Sub

Function GetCellText1(wdCell As Word.Cell) As String
    Dim rng As Word.Range
    
    Set rng = wdCell.Range
    rng.MoveEnd wdCharacter, -1
    GetCellText = rng.Text
End Function

Open in new window

0
 
dlmilleCommented:
You got it - just eliminate lines 9 and 10 and test it out - you should be good to go.

Dave
0
 
rtod2Author Commented:
It's the test-it-out part I am stuck on :)
0
 
dlmilleCommented:
Ahhh - I see.  

So - when do you want the document to assume its new name?  What event would drive that?  Do you want to run the macro that does that, or do you want it to happen when the document is closed, etc.?

Dave
0
 
rtod2Author Commented:
dlmille,
Thank you for your help, sir!!  I'd probably want it to happen on close or save.  

Action:  
1. Rename the document to whatever is showing for the current title (shown centered on the page), but only IF it as been changed

- and-

2. Add the current version number (shown at the top, beside the word "version"), but only IF it has been changed.  

If neither have changed, nothing should happen.
0
 
dlmilleCommented:
I think what you might need perhaps is an add-in or template with embedded macro solution.  Chat me up on what you're trying/want to do and I'll look at it shortly.

Cheers,

Dave
0
 
rtod2Author Commented:
Dave, thanks

Perhaps our paths crossed.

Thank you for your help, sir!!  I'd probably want it to happen on close or save.  

Action:  
1. Rename the document to whatever is showing for the current title (shown centered on the page), but only IF it as been changed

- and-

2. Add the current version number (shown at the top, beside the word "version"), but only IF it has been changed.  

If neither have changed, nothing should happen.

** The above seems ideal.  Often, there is no need to rename or change version numbers, so if that is the case, closing or saving would have no effect on the name.  Otherwise, the version would change.
0
 
dlmilleCommented:
Ok - test out what I posted - play with it a bit.  Let me know how it works for you.  I'll do a little research on document templates versus addin - I'm thinking add-in now, as once you've saved the template it becomes a doc and would loose capability to change its name after that.

Cheers,

Dave
0
 
rtod2Author Commented:
I sure seems to for the number!!

For the name, it changes to the word next to the version number instead of centered area below the header.  It is that name that I would prefer to keep.  The word Version should not be in the name of the document but should appear next to the version number.
0
 
dlmilleCommented:
Ok - let's be real clear, here.  what is the exact name?
0
 
rtod2Author Commented:
"Procedure Documentation Template"
0
 
dlmilleCommented:
and its always the first line of the entire document, correct?
0
 
rtod2Author Commented:
rethinking...  This is perfect.  Thank you!
0
 
dlmilleCommented:
Wait... we're done?  I'm in the throes of your solution - what's up?
0
 
dlmilleCommented:
Ok - this one gets the "name" from the first line of the active document - if that's always the title, you're good to go.

same exercise - rename to .docm

Cheers,

Dave
title-version-number-r1.docx
0
 
rtod2Author Commented:
Well, I'll tell you.  I like your solution better.  Best to use the title next to the number.  Then I don't need a title on the document.  That works well!!

I am noticing though, that pressing save does not create the new name and version where save does.  Save, on the other hand, updates the original which it should NOT do UNLESS the title or version number has changed.

I am uploading the latest that we should work from at this point.
Procedure-Documentation-Template.docx
0
 
dlmilleCommented:
I'm not sure I wholly understand what you stated.

We were dealing with a CLOSE event.

Let's talk about the SAVE event, now.  When you hit SAVE, it should save the document - the document's name should always be the Header+Version - correct?  Is there ever a case where you want to hit save and save it differently than the header+version?

Lay it out for me - VBA is easy - getting things straight between two people - now, that's just darn tough sometimes :)

Cheers,

Dave

 
0
 
rtod2Author Commented:
That's funny.  You are correct on you first question.  Both events (close, save) should create a new file if the name either the title or version has changed.  If neither of those has changed, then the existing file should simply be updated.  

What I DONT want is for there to be a new file AND an -updated- old file.
0
 
dlmilleCommented:
Good - that makes it easy.  SAVE will always SAVE AS title+version (and if the title/version have not changed, no new instance of the file would be created).

Give me a sec...
0
 
dlmilleCommented:
do you want any space or underscore or anything between title + version?
0
 
rtod2Author Commented:
Dave, Thank you.

A space
0
 
dlmilleCommented:
ok - we have a chat going, lol...

On document close, do you want to be prompted to save or not (just in case you totally messed up the document?)
0
 
rtod2Author Commented:
Yeah, that would be perfect!
0
 
dlmilleCommented:
Ok - test this behavior out and see if you like it.

If you SAVE, and the header/version change, it will save that file, but it doesn't rename the file you have open to that name....  Trying to figure out why that is - a good thing?  don't know as yet.

Check it out - same routine

Dave
Procedure-Documentation-Template.DOCX
0
 
dlmilleCommented:
ok - this is it - I think the behanor is working as I (and hopefully you) would expect it to.

Here are the public modules:
 
Public alreadyClosing 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
    
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 alreadyClosing Then
        DoEvents
        Call saveDoc
        Cancel = True
    End If
    
End Sub

Open in new window


See attached document.  Let me know how it all works out for you!

Enjoy!

Dave
title-version-number-r3.DOCX
0
 
dlmilleCommented:
Sorry - this also code in the ThisDocument code page:
 
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
        alreadyClosing = True
        Call saveDoc
    End If
End Sub

Private Sub Document_Open()
    Call Register_Event_Handler
End Sub

Open in new window


Cheers,

Dave

PS - not sure how making this into an add-in assists in your work process.  More of a smart document, now.  All the code needed to manage its naming is embedded.
0
 
rtod2Author Commented:
I like the "do you want to save before closing".

I don't like it not renaming the file you have open to that name.  I think it 'should' rename the file you have open to the new name.  After all, you wouldn't have renamed it on the inside if you didn't want a new name.

Also, I think the case for both events ('close' and 'save') should always match the case of what is at the top of the document.
0
 
dlmilleCommented:
Try my last post - it does what you think it "should" now.

The only way to rename it differently is to do a File/Save As or rename in Windows Explorer - or, of course, change the header+version

Cheers.

Dave
0
 
rtod2Author Commented:
Holy toledo, can you send back the doc with those changes implemented?
0
 
dlmilleCommented:
You mean this one?
title-version-number-r3.DOCX
0
 
rtod2Author Commented:
That appears to work.  However, still makes an uppercase DOCM that isn't so good.  Lowercase extensions are preferred.
0
 
dlmilleCommented:
go into the VBA Editor and change it.  Its in the module called "Class1".

See if you can find it - it would be good for you to see so you can make minor mods.

Let me know if help is required.

Dave
0
 
dlmilleCommented:
Excuse me, its actually in the Module1 module - see the function called getFName -

just change this:

getFName = ActiveDocument.Path & "\" & getTitle & " " & getVersion & ".DOCM"

to:

getFName = ActiveDocument.Path & "\" & getTitle & " " & getVersion & ".docm"

0
 
rtod2Author Commented:
I'd greatly appreciate the assist if you could do the change.  I'm also seeing that if I have the file closed, and rename it, the title and version aren't updated when I open it.  This should happen as well in order to have a complete solution.  If I need to close and ask that as a new topic, I can.  Thank you for the help here.
0
 
rtod2Author Commented:
That is to say, any name and/or version change on the inside should change the name and/or version on the outside, and vice-versa.
0
 
dlmilleCommented:
It might be a good idea - as this thread is getting long.  I'll post the new solution and code, etc., so E-Ers out there that want to benefit from it will see it right up front.

Be sure to ask a related question.

Attached is that change to lower case

Dave
title-version-number-r5.docx
0
 
rtod2Author Commented:
Dave,
Thank you for all your help!  The new question is here >> http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Word/Q_27417819.html  and I will check back tomorrow.  

Great stuff!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.