Consulting

Page 2 of 2 FirstFirst 1 2
Results 21 to 28 of 28

Thread: Macro Help

  1. #21
    VBAX Regular
    Joined
    May 2024
    Posts
    15
    Location
    i knew i forgot something!

    Sub AutoOpen()    ' Update all fields in the document
        Dim aStory As Range
        Dim aField As Field
        For Each aStory In ActiveDocument.StoryRanges
            For Each aField In aStory.Fields
                aField.Update
            Next aField
        Next aStory
        
        ' Process checkboxes based on custom properties
        CheckBoxesByTag
    End Sub
    
    
    Sub CheckBoxesByTag()
        Dim lngProp As Long
        Dim arrProperties() As String
        Dim varValue As Variant
        Dim oCC As ContentControl
        
        ' Define the names of the custom properties to check
        arrProperties = Split("DCR Type|Change Classification", "|")
        
        ' Loop through each property name in the array
        For lngProp = 0 To UBound(arrProperties)
            ' Get the value of the custom property
            varValue = ActiveDocument.CustomDocumentProperties(arrProperties(lngProp)).Value
            
            ' If the value is not empty, find the corresponding content control by tag and check it
            If Not IsEmpty(varValue) Then
                ' Get the specific content control associated with that value e.g., tag = value
                Set oCC = ActiveDocument.SelectContentControlsByTag(varValue).Item(1)
                oCC.LockContents = False
                oCC.Checked = True
                oCC.LockContents = True
            End If
        Next lngProp
    lbl_Exit:
      Exit Sub
    End Sub

  2. #22
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,357
    Location
    Tony,

    Just for the sake of completeness, while your AutoOpen() macros may be updating all of the fields you care about, it is not a comprehensive update. You might consider:

    Sub AutoOpen()
      UpdateAllFields
      CheckBoxesByTag
    lbl_Exit:
      Exit Sub
    End Sub
    
    Sub UpdateAllFields()
    Dim oRngStory As Word.Range
    Dim lngJunk As Long
    Dim oShp As Shape, oCanShp As Shape
    Dim oToc As TableOfContents, oTOA As TableOfAuthorities, oTOF As TableOfFigures
    lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType
      For Each oRngStory In ActiveDocument.StoryRanges
        'Iterate through all linked stories
        Do
          On Error Resume Next
          oRngStory.Fields.Update
          Select Case oRngStory.StoryType
            Case 6, 7, 8, 9, 10, 11
              If oRngStory.ShapeRange.Count > 0 Then
                For Each oShp In oRngStory.ShapeRange
                  If oShp.TextFrame.HasText Then
                     oShp.TextFrame.TextRange.Fields.Update
                  End If
                  If oShp.Type = msoCanvas Then
                    For Each oCanShp In oShp.CanvasItems
                      If oCanShp.TextFrame.HasText Then
                        oCanShp.TextFrame.TextRange.Fields.Update
                      End If
                    Next oCanShp
                  End If
                Next
              End If
            Case Else
              'Do Nothing
          End Select
          On Error GoTo 0
          'Get next linked story (if any)
          Set oRngStory = oRngStory.NextStoryRange
        Loop Until oRngStory Is Nothing
      Next oRngStory
      'Special Cases
      For Each oToc In ActiveDocument.TablesOfContents
        oToc.Update
      Next oToc
      For Each oTOA In ActiveDocument.TablesOfAuthorities
        oTOA.Update
      Next
      For Each oTOF In ActiveDocument.TablesOfFigures
        oTOF.Update
      Next
    lbl_Exit:
      Exit Sub
    End Sub
    Sub CheckBoxesByTag()
    Dim lngProp As Long
    Dim arrProperties() As String
    Dim varValue As Variant
    Dim oCC As ContentControl
      'Define the names of the custom properties to check
      arrProperties = Split("DCR Type|Change Classification", "|")
      'Loop through each property name in the array
      For lngProp = 0 To UBound(arrProperties)
        'Get the value of the custom property
        varValue = ActiveDocument.CustomDocumentProperties(arrProperties(lngProp)).Value
        'If the value is not empty, find the corresponding content control by tag and check it
        If Not IsEmpty(varValue) Then
          'Get the specific content control associated with that value e.g., tag = value
          Set oCC = ActiveDocument.SelectContentControlsByTag(varValue).Item(1)
          With oCC
            .LockContents = False
            .Checked = True
            .LockContents = True
          End With
        End If
      Next lngProp
    lbl_Exit:
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  3. #23
    VBAX Regular
    Joined
    May 2024
    Posts
    15
    Location
    Greg,

    Thanks for the latest update!

    Of course now that i have this working, management has decided that the line here:

    ' Define the names of the custom properties to check
        arrProperties = Split("DCR Type|Change Classification", "|")
    doesnt need an array check. there is now just Change Classification property to check. could you modify so this is the case? Thanks in advance

  4. #24
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,357
    Location
    You can just modify that line to:

    arrProperties = Split("Change Classification", "|")

    arrProperties will then just contain one element "Change Classification"

    If your management changes their mind later you can revise that line to suit e.g.,:
    arrProperties = Split("Change Classification|PropB|PropC|Prop whatever", "|")
    Greg

    Visit my website: http://gregmaxey.com

  5. #25
    VBAX Regular
    Joined
    May 2024
    Posts
    15
    Location
    Perfect! thank you very much.

    Also could you suggest a good resource for self learning this VBA stuff. I have a feeling that we'll be using this more in the future.

  6. #26
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,357
    Location
    Well for starters, I would avoid the AI idiots. In my opinion they will give you half-baked solutions at best. My website has a lot of code examples as does Graham Mayor, Suzanne Barnhill and Charles Kenyon. Reading and trying to answer posts in these forums with the position that "a solution is there, I just have to find it" is where I learned most of what little I know.
    Greg

    Visit my website: http://gregmaxey.com

  7. #27
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,106
    Location
    Quote Originally Posted by gmaxey View Post
    ....is where I learned most of what little I know.
    . Even for us Aussies, that's far too modest!.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  8. #28
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,357
    Location
    "We are all apprentices in a craft where no one ever becomes a master." ... Hemingway (I think). Thanks Ted!
    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
  •