?
Solved

Auto name document

Posted on 2011-10-24
43
Medium Priority
?
465 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:rtod2
  • 21
  • 18
  • 2
41 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 37023879
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
 

Author Comment

by:rtod2
ID: 37024885
Ok, perfect!  I'd still rather have the dot though.  How do I change that and implement it?
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 37024990
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:rtod2
ID: 37025144
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
 
LVL 42

Expert Comment

by:dlmille
ID: 37035736
You got it - just eliminate lines 9 and 10 and test it out - you should be good to go.

Dave
0
 

Author Comment

by:rtod2
ID: 37035758
It's the test-it-out part I am stuck on :)
0
 
LVL 42

Expert Comment

by:dlmille
ID: 37035779
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
 

Author Comment

by:rtod2
ID: 37035813
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
 
LVL 42

Accepted Solution

by:
dlmille earned 2000 total points
ID: 37035820
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
 
LVL 42

Expert Comment

by:dlmille
ID: 37035824
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
 

Author Comment

by:rtod2
ID: 37035834
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
 
LVL 42

Expert Comment

by:dlmille
ID: 37035938
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
 

Author Comment

by:rtod2
ID: 37035944
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
 
LVL 42

Expert Comment

by:dlmille
ID: 37036019
Ok - let's be real clear, here.  what is the exact name?
0
 

Author Comment

by:rtod2
ID: 37036026
"Procedure Documentation Template"
0
 
LVL 42

Expert Comment

by:dlmille
ID: 37036030
and its always the first line of the entire document, correct?
0
 

Author Closing Comment

by:rtod2
ID: 37036040
rethinking...  This is perfect.  Thank you!
0
 
LVL 42

Expert Comment

by:dlmille
ID: 37036042
Wait... we're done?  I'm in the throes of your solution - what's up?
0
 
LVL 42

Expert Comment

by:dlmille
ID: 37036050
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
 

Author Comment

by:rtod2
ID: 37036053
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
 
LVL 42

Expert Comment

by:dlmille
ID: 37036061
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
 

Author Comment

by:rtod2
ID: 37036076
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
 
LVL 42

Expert Comment

by:dlmille
ID: 37036079
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
 
LVL 42

Expert Comment

by:dlmille
ID: 37036083
do you want any space or underscore or anything between title + version?
0
 

Author Comment

by:rtod2
ID: 37036084
Dave, Thank you.

A space
0
 
LVL 42

Expert Comment

by:dlmille
ID: 37036096
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
 

Author Comment

by:rtod2
ID: 37036099
Yeah, that would be perfect!
0
 
LVL 42

Expert Comment

by:dlmille
ID: 37036132
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
 
LVL 42

Expert Comment

by:dlmille
ID: 37036183
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
 
LVL 42

Expert Comment

by:dlmille
ID: 37036187
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
 

Author Comment

by:rtod2
ID: 37036189
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
 
LVL 42

Expert Comment

by:dlmille
ID: 37036197
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
 

Author Comment

by:rtod2
ID: 37036202
Holy toledo, can you send back the doc with those changes implemented?
0
 
LVL 42

Expert Comment

by:dlmille
ID: 37036207
You mean this one?
title-version-number-r3.DOCX
0
 

Author Comment

by:rtod2
ID: 37036213
That appears to work.  However, still makes an uppercase DOCM that isn't so good.  Lowercase extensions are preferred.
0
 
LVL 42

Expert Comment

by:dlmille
ID: 37036229
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
 
LVL 42

Expert Comment

by:dlmille
ID: 37036236
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
 

Author Comment

by:rtod2
ID: 37036241
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
 

Author Comment

by:rtod2
ID: 37036243
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
 
LVL 42

Expert Comment

by:dlmille
ID: 37036271
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
 

Author Comment

by:rtod2
ID: 37036323
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …
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 Month13 days, 15 hours left to enroll

809 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