View Single Post
 
Old 09-23-2019, 08:29 PM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,138
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

You should also declare all your variables and update the fields when the processing is completed.

If you have fields that are not in the body of the document you will need to ensure that they are updated also.
Code:
Private Sub cmdOK_Click()
Dim i As Long
Dim StrOut As String
Dim oVars As Variables
    Set oVars = ActiveDocument.Variables
    Hide
    oVars("var1").value = Surname.value
    For i = 0 To Services.ListCount - 1
        If Services.Selected(i) Then
            StrOut = StrOut & Services.List(i) & vbCr
        End If
    Next i
    oVars("var3").value = StrOut
    UpdateAllFields
    Set oVars = Nothing
    Unload Me
End Sub

Private Sub UpdateAllFields()
Dim oStory As Range
    For Each oStory In ActiveDocument.StoryRanges
        oStory.Fields.Update
        If oStory.StoryType <> wdMainTextStory Then
            While Not (oStory.NextStoryRange Is Nothing)
                Set oStory = oStory.NextStoryRange
                oStory.Fields.Update
            Wend
        End If
    Next oStory
    Set oStory = Nothing
lbl_Exit:
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote