|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
Scriptstep delete variable doesn´t work on Word 2016 as it used on word 2011
HI,
one more problem since I changed from WOrd 2011 to WOrd 2016: I had a macro running that delete a variable called "varMerk42" when closing any word document. I put it in Normal => this document: Code:
Private Sub Document_Close() On Error Resume Next ActiveDocument.Variables("varMerk42").Delete End Sub It worked perfectly on WOrd 2011. But now it doesn´t work any more: The macro runs (I tested that putting a line with a msg box) but it simply doesn´t delete the variable as wished. Any ideas? Thanks, swenniels |
#2
|
|||
|
|||
The macro will run only if the attached template for your document is the Normal Template.
The fact that you are deleting a document variable, which can only be set from VBA, suggests that Normal isn't the template being used by your document. |
#3
|
||||
|
||||
I would question where does the document get saved? If you make a change to a document and neglect to save it, why would you expect that change to be in the document when you open it.
I'm not sure when the autocode is initiated to run. Is Document_Close run before the document closes or does it run after?
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#4
|
||||
|
||||
Quote:
In my testing, the code works correctly. The question arises therefore as to how the OP is establishing that the variable hasn't been deleted from the document. The mere fact that the result in a DOCVARIABLE field referencing the deleted variable might remain unchanged, for example, isn't evidence; refreshing that field would soon delete its result (but not the field itself).
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
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 Thanks for your help swenniels |
#6
|
||||
|
||||
Quote:
PS: When posting code, please use the code tags, indicated by the # button on the posting menu. Without them, your code loses much of whatever structure it had. I've added the tags to the relevant posts in this thread.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
But Macro part 2 interacts with "varMerk42"... and macro part 3 should delete this variable.
|
#8
|
||||
|
||||
eduzs: This has already been addressed numerous times. Please pay attention to what has already been posted.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#9
|
||||
|
||||
Quote:
Quote:
Code:
Private Sub Document_Open() Const MyVar As String = "varMerk42" On Error GoTo NoVariable MsgBox MyVar & " has the value: " & ActiveDocument.Variables(MyVar).Value, vbOKOnly Exit Sub NoVariable: MsgBox MyVar & " has been deleted.", vbOKOnly End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#10
|
|||
|
|||
I did so. result: msx box saying: VarMerk42 has been deleted.
|
#11
|
||||
|
||||
In that case, your macros in parts 1 & 3 are working correctly. You need to go back to first principles and work out what it is you want the code to do overall, then modify the code (presumably in part 2) accordingly.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#12
|
|||
|
|||
Thanks, I try to find that out and I´ll post here again if I need help...
Cheers! swenniels |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
[Word 2016 for Mac] SKIPIF condition doesn't work | rschnell | Mail Merge | 3 | 10-19-2017 02:01 PM |
Word 2016 - Delete all Tab stops | avonw | Word | 6 | 10-07-2017 01:22 PM |
Slicer built in 2016 doesn't work in 2013 | cdoody | Excel | 0 | 10-31-2016 10:25 AM |
upgrading from word 2011 to word 2016 | classicdining | Word | 1 | 07-01-2016 04:37 PM |
Word 2007 Font Color Doesn't Work | nnicko | Word | 2 | 04-16-2010 01:21 PM |