Consulting

Results 1 to 4 of 4

Thread: Document Revisions

  1. #1
    VBAX Regular
    Joined
    Jul 2014
    Posts
    79
    Location

    Document Revisions

    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

  2. #2
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Regular
    Joined
    Jul 2014
    Posts
    79
    Location
    Thanks very much gmayor, I will give that a try.

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •