Log in

View Full Version : Document Revisions



Jfp87
05-25-2015, 10:30 AM
Guys,

My code creates different folders which contain the same documents but have different revisions. To distinguish the revision of the folder I have been using the folder's name; so I might have a folder titled "R1" and another "A1". Another part of my code will compare the folder names and decide which is the latest revision by comparing values I have assigned to each of the possible revisions. Unfortunately, the user may change the folder name, in which case this method breaks down.

My next approach will be to determine the revision of the folder by opening a document within the folder and searching for the value of the cc named "Status", which holds the revision. In this way, the code will store the folder name and revision in arrays. The latest revised folder will be determined using these arrays.

Does this sound like an ok way of doing things? Or is there a more obvious simpler way? I am basically trying to allow the user to rename the folder if necessary, but still have the proper latest revision available each time.

Thanks,
Joe

gmayor
05-26-2015, 01:51 AM
Based on your comments, I think I would be inclined to write the value of the version to a document variable stored in the document whenever the document is saved. That way you can read the variable in the document to ensure it is the latest version. The following functions will help with that



Option Explicit

Public Function ReadVar(strVariableName As String) As String
'Macro by Graham Mayor 26 May 2015
Dim oVar As Variable
Dim bVar As Boolean
For Each oVar In ActiveDocument.Variables
If oVar.name = strVariableName Then
ReadVar = oVar.Value
bVar = True
Exit For
End If
Next oVar
If Not bVar Then
ReadVar = 0
WriteVar strVariableName, "0"
End If
lbl_Exit:
Exit Function
End Function

Public Sub WriteVar(strVariable As String, strValue As String)
'Macro by Graham Mayor 26 May 2015
ActiveDocument.Variables(strVariable).Value = strValue
ActiveDocument.Save
lbl_Exit:
Exit Sub
End Sub

Sub TestRead()
MsgBox ReadVar("varVersionNumber")
End Sub

Jfp87
05-26-2015, 04:03 AM
Thanks very much gmayor, I will give that a try.

gmaxey
05-28-2015, 04:11 PM
Just as an alternative, I sometimes use error handling and explicit call the variable of interest:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
MsgBox ReadVar("Revision", "0")
lbl_Exit:
Exit Sub
End Sub
Public Function ReadVar(strVarName As String, strDefaultVal As String) As String
Dim oVar As Variable
Set oVar = ActiveDocument.Variables(strVarName)
On Error Resume Next
ReadVar = oVar.Value
If Err.Number <> 0 Then
ReadVar = strDefaultVal
WriteVar strVarName, strDefaultVal
End If
lbl_Exit:
Exit Function
End Function

Public Sub WriteVar(strVariable As String, strValue As String)
'Macro by Graham Mayor 26 May 2015
ActiveDocument.Variables(strVariable).Value = strValue
ActiveDocument.Save
lbl_Exit:
Exit Sub
End Sub