Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-19-2018, 11:53 PM
swenniels swenniels is offline Mac OS X Office 2016 for Mac
Novice
 
Join Date: Mar 2018
Posts: 6
swenniels is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 03-23-2018, 11:29 AM
slaycock slaycock is offline Windows 7 64bit Office 2016
Expert
 
Join Date: Sep 2013
Posts: 256
slaycock is on a distinguished road
Default

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.
Reply With Quote
  #3  
Old 03-23-2018, 07:23 PM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2013
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #4  
Old 03-23-2018, 10:09 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by Guessed View Post
Is Document_Close run before the document closes or does it run after?
Assuming the Document_Close macro is in the 'ThisDocument' code module of the document or its template, it runs before the document closes - indeed, it runs before the save prompt (if applicable). If it's not where its supposed to be, it won't run at all. We can probably discount that, however, since the OP has already told us he's confirmed the correct implementation via a Message Box.

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]
Reply With Quote
  #5  
Old 03-24-2018, 01:40 AM
swenniels swenniels is offline Mac OS X Office 2016 for Mac
Novice
 
Join Date: Mar 2018
Posts: 6
swenniels is on a distinguished road
Default

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
Reply With Quote
  #6  
Old 03-24-2018, 03:18 AM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by swenniels View Post
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
Your problem here has nothing to do with "varMerk42", since your 'Macro part 1' never interacts with that variable - all it ever looks at is "varMerk2" - and your Document_Close macro only ever interacts with "varMerk42".

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]
Reply With Quote
  #7  
Old 03-24-2018, 07:01 AM
swenniels swenniels is offline Mac OS X Office 2016 for Mac
Novice
 
Join Date: Mar 2018
Posts: 6
swenniels is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
Your problem here has nothing to do with "varMerk42", since your 'Macro part 1' never interacts with that variable - all it ever looks at is "varMerk2" - and your Document_Close macro only ever interacts with "varMerk42".
But Macro part 2 interacts with "varMerk42"... and macro part 3 should delete this variable.
Reply With Quote
  #8  
Old 03-24-2018, 11:53 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by eduzs View Post
Also can you test with MsgBox ActiveDocument.name, because in the path there may be other documents?
eduzs: This has already been addressed numerous times. Please pay attention to what has already been posted.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 03-25-2018, 12:11 AM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by swenniels View Post
But Macro part 2 interacts with "varMerk42"... and macro part 3 should delete this variable.
But what has that got to do with what's going on in your part 1 macro, which is where you said the problem manifests?
Quote:
Originally Posted by swenniels View Post
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
To test, add the following code to the 'ThisDocument' code module of the document or its template
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]
Reply With Quote
  #10  
Old 03-25-2018, 01:13 PM
swenniels swenniels is offline Mac OS X Office 2016 for Mac
Novice
 
Join Date: Mar 2018
Posts: 6
swenniels is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
To test, add the following code to the 'ThisDocument' code module of the document or its template[/CODE]
I did so. result: msx box saying: VarMerk42 has been deleted.
Reply With Quote
  #11  
Old 03-25-2018, 01:20 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #12  
Old 03-25-2018, 02:30 PM
swenniels swenniels is offline Mac OS X Office 2016 for Mac
Novice
 
Join Date: Mar 2018
Posts: 6
swenniels is on a distinguished road
Default

Thanks, I try to find that out and I´ll post here again if I need help...

Cheers!

swenniels
Reply With Quote
Reply



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

Other Forums: Access Forums

All times are GMT -7. The time now is 11:06 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft