PDA

View Full Version : Increment FileName with Version And Revision



hamaka
02-27-2013, 08:34 AM
Hello everyone,

I'm new to VBA and need to set up a change control for word document.
The idea is to be able to add and increment after the ActiveFileName a Version number and a Revision Number (MyDocument-Version##-Revision###).

Every change would be a Revision, MyDocument-Version00-Revision001 with Revision incrementing to 002, 003 and so on. Once Revisions are accepted Version is incremented and Revision is set to 000, MyDocument-Version01-Revision000.

I found some macro models to get increment of Revision as MyDocument-Revision001, MyDocument-Revision002, MyDocumentRevision003, etc. Can anyone please help me to find a way to set up the two increment, version and revision?

Sorry if that does not make any sense.

Thanks

Hamaka

macropod
02-28-2013, 12:02 AM
How about posting the code you've got, or at least a link to it. No-one here really wants to reinvent the wheel.

Also, regarding your version numbering, what is supposed to happen if whoever determines the acceptance/rejection of changes accepts/rejects some, but leaves others unprocessed? In other words, how is Word suppoed to know a new version, as opposed to a new revision, has been created?

hamaka
02-28-2013, 03:18 AM
Hello,

Thanks for your reply and apologies for lack of details.

I found an incrementing macro on goo.gl/Lk4aF (and changed it to remove the Date).

----------------------------------------------------------
Sub saveMacro()
Dim WSHShell, RegKey, rkeyWord, Result
Set WSHShell = CreateObject("WScript.Shell")
Dim intCount As Integer
Dim strPath As String
Dim strFile As String
Dim strFileType As WdDocumentType
Dim strRevisionName As String
Dim intPos As Integer
Dim sExt As String
sExt = ".docx"
strFileType = wdFormatDocument
With ActiveDocument
On Error GoTo CancelledByUser
If Len(.Path) = 0 Then
.Save
End If
strPath = .Path
strFile = .Name
End With
intPos = InStr(strFile, " - ")
If intPos = 0 Then
intPos = InStrRev(strFile, ".docx")
End If
strFile = Left(strFile, intPos - 1)
Start:
RegKey = "HKEY_CURRENT_USER\Software\Microsoft\Office\Word\Settings\"
On Error Resume Next
rkeyWord = WSHShell.RegRead(RegKey & strFile)
If rkeyWord = "" Then
WSHShell.regwrite RegKey & strFile, 0
GoTo Start:
End If
intCount = Val(rkeyWord) + 1
WSHShell.regwrite RegKey & strFile, intCount
strRevisionName = strPath & "\" & strFile & " - Revision " & Format(intCount, "00#") & sExt
ActiveDocument.SaveAs strRevisionName
Exit Sub
CancelledByUser:
MsgBox "Cancelled By User", , "Save Cancelled."
End Sub
------------------------------------------------------------

The idea to avoid a revision being saved as a version would be to password protect the increment version part of the macro. Found this on

goo.gl/Vy6jN

-----------------------------------------------------------
Sub RunMacro()
Dim strPassTry As String
Dim strPassword As String
Dim lTries As Long
Dim bSuccess As Boolean

strPassword = "Secret"
For lTries = 1 To 3
strPassTry = InputBox("Enter Password please", "RUN MACRO")
If strPassTry = vbNullString Then Exit Sub
bSuccess = strPassword = strPassTry
If bSuccess = True Then Exit For
MsgBox "Password incorrect"
Next lTries

If bSuccess = True Then Run "MyMacro"
End Sub
--------------------------------------------------------------------------

Now next step is to get my first macro with version and revision (still strugling with that, will post it when done...) and afterward password protect the version part, if that makes senses.

Cheers

hamaka
02-28-2013, 03:38 AM
Hello,

Thanks for your reply and apologies for lack of details.

I found an incrementing macro on goo.gl/Lk4aF (and changed it to remove the Date).



Sub saveMacro()
Dim WSHShell, RegKey, rkeyWord, Result
Set WSHShell = CreateObject("WScript.Shell")
Dim intCount As Integer
Dim strPath As String
Dim strFile As String
Dim strFileType As WdDocumentType
Dim strRevisionName As String
Dim intPos As Integer
Dim sExt As String
sExt = ".docx"
strFileType = wdFormatDocument
With ActiveDocument
On Error GoTo CancelledByUser
If Len(.Path) = 0 Then
.Save
End If
strPath = .Path
strFile = .Name
End With
intPos = InStr(strFile, " - ")
If intPos = 0 Then
intPos = InStrRev(strFile, ".docx")
End If
strFile = Left(strFile, intPos - 1)
Start:
RegKey = "HKEY_CURRENT_USER\Software\Microsoft\Office\Word\Settings\"
On Error Resume Next
rkeyWord = WSHShell.RegRead(RegKey & strFile)
If rkeyWord = "" Then
WSHShell.regwrite RegKey & strFile, 0
GoTo Start:
End If
intCount = Val(rkeyWord) + 1
WSHShell.regwrite RegKey & strFile, intCount
strRevisionName = strPath & "\" & strFile & " - Revision " & Format(intCount, "00#") & sExt
ActiveDocument.SaveAs strRevisionName
Exit Sub
CancelledByUser:
MsgBox "Cancelled By User", , "Save Cancelled."
End Sub

The idea to avoid a revision being saved as a version would be to password protect the increment version part of the macro. Found this on

goo.gl/Vy6jN



Sub RunMacro()
Dim strPassTry As String
Dim strPassword As String
Dim lTries As Long
Dim bSuccess As Boolean

strPassword = "Secret"
For lTries = 1 To 3
strPassTry = InputBox("Enter Password please", "RUN MACRO")
If strPassTry = vbNullString Then Exit Sub
bSuccess = strPassword = strPassTry
If bSuccess = True Then Exit For
MsgBox "Password incorrect"
Next lTries

If bSuccess = True Then Run "MyMacro"
End Sub


Now next step is to get my first macro with version and revision (still strugling with that, will post it when done...) and afterward password protect the version part, if that makes senses.

Cheers

macropod
02-28-2013, 05:48 AM
You can't password-protect part of a filename - anyone could change it via File>SaveAs, or (without even opening the document) via Windows Explorer.

Bearing in mind that limitation, the following macro intercepts the File>Save process to automatically update the version & revision numbers. Revision numbers are updated whenever a change in the tracked change numbers or dates occurs. Version numbers are updated whenever all tracked changes have been accepted/rejected.
Private Sub FileSave()
Dim StrName As String, strVal As String, StrExt As String, StrLastRev As String, lFmt As Long
With ActiveDocument
lFmt = .SaveFormat
StrName = .FullName
StrExt = Right(StrName, Len(StrName) - InStrRev(StrName, ".") + 1)
If .Revisions.Count = 0 Then
StrLastRev = 0
Else
StrLastRev = Format(.Revisions(.Revisions.Count).Date, "YYYYMMDDhhmmss") & .Revisions.Count
End If
Exit Sub
If StrLastRev = 0 Then
.CustomDocumentProperties("LastRevision") = 0
strVal = Format(Split(Split(StrName, "Version")(1), "-")(0) + 1, "00")
StrName = Split(StrName, "Version")(0) & "Version" & strVal & "-Revision000" & StrExt
.SaveAs2 FileName:=StrName, Fileformat:=lFmt
ElseIf StrLastRev <> .CustomDocumentProperties("LastRevision") & .Revisions.Count Then
.CustomDocumentProperties("LastRevision") = StrLastRev
strVal = Format(Split(Split(StrName, "Revision")(1), ".")(0) + 1, "000")
StrName = Split(StrName, "Revision")(0) & "Revision" & strVal & StrExt
.SaveAs2 FileName:=StrName, Fileformat:=lFmt
Else
.Save
End If
End With
End Sub
To be able to use the macro, you need to add it to the document's 'ThisDocument' module. You also need to add a Custom Document Property named "LastRevision" to the document.

You really shouldn't need to password-protect any part of the operation, as you should always have the most recent revisions/versions available. Even if the person responsible for releasing the next version saves the document during the tracked changes acceptance/rejection process, the worst that will happen is that you'll get a revision # that doesn't get released.

hamaka
02-28-2013, 07:20 AM
Thanks a lot Paul ! Will try that and keep you posted.

Cheers