Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-01-2019, 08:00 AM
inlanoche inlanoche is offline Global variables Windows 10 Global variables Office 2016
Novice
Global variables
 
Join Date: Apr 2019
Posts: 14
inlanoche is on a distinguished road
Default Global variables

I am still working on a QC Control VBA add in to word to better manage the QC process to our company standards.



I've got it to about 90% complete, but I'm finding that to dummy proof the scripts, I may need to use some global variables. as testing I added a global var to my module, but it seems that as I run each script in my module (one to set your role, another to create the custom styled comment based on your role) it appears that my global var resets to "".

I looked into a few things and found write up on Document variables. the ActiveDocument.Variables looks promising, but I need a variable that clears when the document is closed. So basically, it has to reset on opening the document. Is there something in Word VBA that can act like that?
Reply With Quote
  #2  
Old 05-01-2019, 08:53 AM
inlanoche inlanoche is offline Global variables Windows 10 Global variables Office 2016
Novice
Global variables
 
Join Date: Apr 2019
Posts: 14
inlanoche is on a distinguished road
Default

This gets slightly better. It seems that sometimes the Global Var (public userRole as String) holds a value, and sometimes resets....
If I could better understand that, it may be usable for what I need.
Reply With Quote
  #3  
Old 05-01-2019, 10:36 AM
eduzs eduzs is offline Global variables Windows 10 Global variables Office 2010 32bit
Expert
 
Join Date: May 2017
Posts: 262
eduzs is on a distinguished road
Default

Did you try to set a specific name for the global variable? as Name1234A ??
Did you try using PUBLIC instead of GLOBAL?
Public or Global must be in top of the module, before any sub/function
__________________
Backup your original file before doing any modification.
Reply With Quote
  #4  
Old 05-01-2019, 11:17 AM
inlanoche inlanoche is offline Global variables Windows 10 Global variables Office 2016
Novice
Global variables
 
Join Date: Apr 2019
Posts: 14
inlanoche is on a distinguished road
Default

Quote:
Originally Posted by eduzs View Post
Did you try to set a specific name for the global variable? as Name1234A ??
Did you try using PUBLIC instead of GLOBAL?
Public or Global must be in top of the module, before any sub/function
Yes. At the top of the module, before any other code (besides a comment indicating the use of the Global var) I have this:
Code:
Public userRole As String
Do I need to use GLOBAL or PUBLIC? what is the difference? Documentation I have seen before says to use Public.
Reply With Quote
  #5  
Old 05-01-2019, 11:53 AM
eduzs eduzs is offline Global variables Windows 10 Global variables Office 2010 32bit
Expert
 
Join Date: May 2017
Posts: 262
eduzs is on a distinguished road
Default

I will just use PUBLIC if I not know exactly why need to use GLOBAL which is for complex coding using variables outside word I suppose.

Last edited by eduzs; 05-02-2019 at 02:28 AM.
Reply With Quote
  #6  
Old 05-03-2019, 08:58 AM
inlanoche inlanoche is offline Global variables Windows 10 Global variables Office 2016
Novice
Global variables
 
Join Date: Apr 2019
Posts: 14
inlanoche is on a distinguished road
Default

Well, it seems that I can no longer re-create the fail on the public var not keeping the info, so I guess for now it's solved?

I've been trying for the past few days and can't seem to make it happen again, even with multiple documents open, and other oddities that may occur as people use this. Thanks of all whom replied back.
Reply With Quote
  #7  
Old 05-03-2019, 04:24 PM
eduzs eduzs is offline Global variables Windows 10 Global variables Office 2010 32bit
Expert
 
Join Date: May 2017
Posts: 262
eduzs is on a distinguished road
Default

I am not a pro, but I suppose that nobody can answer your question without analyzing the entire code which is impratical.
The only thing you can do is to test exhaustively with most possible scenarios.
__________________
Backup your original file before doing any modification.
Reply With Quote
  #8  
Old 05-03-2019, 05:02 PM
macropod's Avatar
macropod macropod is offline Global variables Windows 7 64bit Global variables Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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 inlanoche View Post
It seems that sometimes the Global Var (public userRole as String) holds a value, and sometimes resets....
If I could better understand that, it may be usable for what I need.
Simply declaring a variable as public or global doesn't prevent it being reset. For example, with:
Public userRole as String
userRole can be cleared from any sub or function with code like:
userRole = ""
If you want to prevent userRole being reset use code like:
Const userRole As String = "Manager"

Note also that having:
Option Explicit
at the top of your code module will protect you from having something like:
Public userRole as String
but then referencing it as:
useRole
in your code - which quite understandably would come back as a null string.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 05-06-2019, 06:34 AM
inlanoche inlanoche is offline Global variables Windows 10 Global variables Office 2016
Novice
Global variables
 
Join Date: Apr 2019
Posts: 14
inlanoche is on a distinguished road
Default

Thanks for the input on this.

It cannot be a constant, as it would be set by a user selection. This selection needs to be kept as long as the document is open (or another selection changes it). This initial value should be set as the beginning of the use of these scripts. A null value is expected to error check if other parts of the code is used out of turn, which direct the user to proper usage.

So a global var is the only thing I can think of. Other parts of the process 'stamp' a value at the end of the document in a protected area, but as the role can be changed during the use of the document, and these stamps are there from the first usage of the code, I cannot rely on that as a reference point. It needs to be something set only after the code is started, and should have a null value before that.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
How does the Enterprise Global and local Global an User Regitry work together? Lupus74 Project 0 08-31-2018 12:18 PM
Array to iterate through variables and trap blank variables Marrick13 Word VBA 5 08-04-2015 06:19 AM
can word: make variables, find appropriate pages, fill out pages with variables, print only those 20GT Word VBA 1 10-15-2014 09:48 PM
Global variables Global Settings lefttoday Word 3 02-03-2011 03:34 PM
Global variables Global template ryalls3857 Word 1 12-27-2010 05:45 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:02 PM.


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