TO make it clearer to you I explain, what my variable does (part 1+2 works on Word 2011+2016; part 3 only on word 2011) - by the way: I´m not good at VBA, someone in forum made these codes:
PART 1:
With this code I attach a highlighted part (= symptom of a patient) of a word document "in the background" (with the current date) - I have that code run several times (with each symptom), so I have many attached symptoms at the end:
Code:
Sub VARIABLE_fortlaufende_setzen_und_markieren2()
'
' VARIABLE_fortlaufende_setzen _und_markieren Makro
'
'
Dim doA As Document: Set doA = ActiveDocument
Dim nm As String
Dim rngSel As Range
On Error Resume Next
doA.Variables.Add Name:="varMerk2"
On Error GoTo 0
Set rngSel = Selection.Range
nm = " " & rngSel.Text
doA.Variables("varMerk2").Value = doA.Variables("varMerk2").Value & "|" & Date & ":" & nm & "? " 'das _ fuegt er unten dann beim Einfuegen an
rngSel.HighlightColorIndex = wdTurquoise 'markiert hier einfacher
Set doA = Nothing
Selection.MoveRight Unit:=wdCharacter, Count:=1
End Sub
PART 2:
Weeks later I open the same file and I have one symptom after the other come up with a second macro (when the last symptom has shown up and I want the next to show up which doesn´t exist any more, a msg box comes telling me, that that was the last variable and that the variable "varMerk42" is put to 0 again, so that it starts with the first symptom whenever I open the file the next time):
Code:
Sub VARIABLE_der_reihe_nach_abrufen2()
'
' VARIABLE_der_reihe_nach_abrufen Makro
'
'
Static booA As Byte
Dim doA As Document: Set doA = ActiveDocument
Dim aufn() As String, u As Long, rngS As Range, strUhrz As String, lngOp As Long, strIns As String
On Error Resume Next
strK = doA.Variables("varMerk2").Value
If InStrRev(strK, "|") = Len(strK) Then
doA.Variables("varMerk2").Value = Left(doA.Variables("varMerk2").Value, Len(strK) - 1)
End If
aufn = Split(doA.Variables("varMerk2").Value, "|")
lngz = UBound(aufn)
doA.Variables.Add Name:="varMerk42", Value:="1"
On Error GoTo 0
lngOp = CLng(doA.Variables("varMerk42").Value)
If lngOp <= lngz Then
u = CLng(doA.Variables("varMerk42").Value) + 1
doA.Variables("varMerk42").Value = u
Else
Selection.MoveLeft Unit:=wdCharacter, Count:=1
booA = MsgBox("Es gibt keine weiteren Verlaufsparameter" & vbCrLf & "soll die Zählvariabble auf null gesetzt werden?", vbYesNo)
If booA = 6 Then
doA.Variables("varMerk42").Delete
End If
Set doA = Nothing
Exit Sub
End If
strUhrz = " (" & Format(Time, "hh:mm") & ")" & "(" & u - 1 & "/" & lngz & ")"
strIns = Trim(aufn(u - 1)) & strUhrz & Chr(11)
With Selection
.InsertAfter strIns 'hier fügt er die Uhrzeit etc. ein
.Extend
.Font.Color = -603937025 'färbt hellgrau; 8181936 fürbt hellgrün; -603937025 hellgrau; 14701127 hellblau; -654262273 hellrot; 13927639 violett
.Font.Italic = True
.Collapse (wdCollapseEnd)
.Font.ColorIndex = wdBlack
.Font.Italic = False
End With
Set rngS = doA.Range(Selection.Range.End - Len(strUhrz), Selection.Range.End - 1) 'bestimmt den Bereich der Uhrzeit zum Schwärzen
With rngS
.Font.Color = black
.Font.Italic = False
End With
Set doA = Nothing
Selection.Collapse (wdCollapseEnd)
ActiveDocument.Save
End Sub
PART 3 (This one is in Normal => This document; the other macros are in the module):
this macro runs when closing the document (that´s the one the post is about) to make sure the counter of the variable "varMerk42" is put back to 0 in case I closed the document before arriving at the last symptom:
Code:
Private Sub Document_Close()
' On Error Resume Next
ActiveDocument.Variables("varMerk42").Delete
MsgBox ActiveDocument.Path
End Sub
Probably that makes you understand better. THis last part doesn´t work in WOrd 2016. Hwo do I know? FOr example I have 12 symptoms attached: with macro part 2 I get 5 of them, I then close the document, re open it and get the next symptom with Macro part 1, but then symptom number 6 comes up instead of number 1, because the variable "varMerk42" has not been put to 0
Thanks for your help
swenniels