Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-22-2020, 09:39 PM
rekent rekent is offline Declaring Public variables and accessing in other modules Windows 10 Declaring Public variables and accessing in other modules Office 2016
Novice
Declaring Public variables and accessing in other modules
 
Join Date: May 2014
Posts: 22
rekent is on a distinguished road
Default Declaring Public variables and accessing in other modules

In the "ThisDocument" object I declared


Code:
Public filePath As String
and the created a sub immediately following it in the "ThisDocument" object that assigns a url to that variable. My understanding is that I should then be able to access that variable from any other module in my project. However, the variable is empty.

Is my understanding about of Public variables incorrect? My intent is to declare and define a URL path as a variable in one location and then reference that variable in multiple different modules within the same project.
Reply With Quote
  #2  
Old 11-22-2020, 10:35 PM
Charles Kenyon Charles Kenyon is offline Declaring Public variables and accessing in other modules Windows 10 Declaring Public variables and accessing in other modules Office 2019
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,082
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

I believe you are correct as to the scope of a public variable declared outside of a procedure. See Understanding scope and visibility (VBA) | Microsoft Docs. I suspect that "FilePath" is not the best name for your variable. I would suggest:
Code:
Public strFilePath as String
[Edit for future readers of this thread: See my later responses about document variables and Greg Maxey's about class modules (like ThisDocument) later in this thread. How to use such a variable depends on what kind of template/document it is being used in.]

Last edited by Charles Kenyon; 11-24-2020 at 05:09 PM.
Reply With Quote
  #3  
Old 11-22-2020, 10:49 PM
rekent rekent is offline Declaring Public variables and accessing in other modules Windows 10 Declaring Public variables and accessing in other modules Office 2016
Novice
Declaring Public variables and accessing in other modules
 
Join Date: May 2014
Posts: 22
rekent is on a distinguished road
Default

This is the code in my "ThisDocument" Microsoft Word Object, but apparently the projectPath variable is never getting set. Any ideas on what is going wrong to prevent the variable from being set?

Code:
Option Explicit
Public projectPath As String
Sub setProjectPath()
  projectPath = "serverURL\projectFolder"
End Sub
Sub OpenForm(control As IRibbonControl)
  ProjectName.Show
End Sub
Reply With Quote
  #4  
Old 11-22-2020, 10:55 PM
Charles Kenyon Charles Kenyon is offline Declaring Public variables and accessing in other modules Windows 10 Declaring Public variables and accessing in other modules Office 2019
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,082
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

It is set, but not persistent. The value only lasts as long as the procedure setting it. If you use it in a function or another sub while your procedure is still running, it will have the value.


You might make them persistent in the document by using Document Variable instead.
Code:
Let ActiveDocument.Variables("FilePath").Value = "C:\MyDocs\"

Public Function strFilePath() as String
   Let strFilePath = ActiveDocument.Variables("FilePath")
End Function
or store in the template holding the code...

Code:
Let ThisDocument.Variables("FilePath").Value = "C:\MyDocs\"

Public Function strFilePath() as String
   Let strFilePath = ThisDocument.Variables("FilePath")
End Function
Reply With Quote
  #5  
Old 11-22-2020, 11:01 PM
Charles Kenyon Charles Kenyon is offline Declaring Public variables and accessing in other modules Windows 10 Declaring Public variables and accessing in other modules Office 2019
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,082
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

See Understanding the lifetime of variables (VBA) | Microsoft Docs
Reply With Quote
  #6  
Old 11-22-2020, 11:06 PM
Guessed's Avatar
Guessed Guessed is offline Declaring Public variables and accessing in other modules Windows 10 Declaring Public variables and accessing in other modules Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
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

As a public variable, its value will persist but you do need to run the macro to give it a value first. So you should rename the setProjectPath macro to Document_Open or Document_New as these will autorun with either opening an existing document or creating a new document respectively.

Alternatively, you could declare it as a constant.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #7  
Old 11-22-2020, 11:13 PM
Charles Kenyon Charles Kenyon is offline Declaring Public variables and accessing in other modules Windows 10 Declaring Public variables and accessing in other modules Office 2019
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,082
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

Code:
Option Explicit
Public projectPath As String

Static Sub setFilePath()
    Let projectPath = "This is my filepath"
End Sub

Sub ShowprojectPath()
    MsgBox "projectPath = " & projectPath
End Sub
The value of projectPath will disappear when the template/document with the code is closed. It should be available to any other procedure/function in the module. Perhaps it will be available to any other procedure/function in the project; I am uncertain and have not tested.
Reply With Quote
  #8  
Old 11-22-2020, 11:20 PM
Charles Kenyon Charles Kenyon is offline Declaring Public variables and accessing in other modules Windows 10 Declaring Public variables and accessing in other modules Office 2019
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,082
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

Quote:
Originally Posted by Guessed View Post
As a public variable, its value will persist but you do need to run the macro to give it a value first. So you should rename the setProjectPath macro to Document_Open or Document_New as these will autorun with either opening an existing document or creating a new document respectively.

Alternatively, you could declare it as a constant.
My experience, tonight, disagrees. Perhaps I am just tired. I tried setting the value of a public variable declared outside a procedure and it blanked when I ran the second procedure trying to access that value.

Code:
Option Explicit 

Public projectPath As String 

Sub setFilePath() 
  Let projectPath = "This is my filepath" 
End Sub 

Sub ShowprojectPath() 
  MsgBox "projectPath = " & projectPath 
End Sub
Andrew, I admit that I am out of my comfort zone here. I initially thought the same. You have more experience with this than I do.
Reply With Quote
  #9  
Old 11-22-2020, 11:20 PM
rekent rekent is offline Declaring Public variables and accessing in other modules Windows 10 Declaring Public variables and accessing in other modules Office 2016
Novice
Declaring Public variables and accessing in other modules
 
Join Date: May 2014
Posts: 22
rekent is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
As a public variable, its value will persist but you do need to run the macro to give it a value first. So you should rename the setProjectPath macro to Document_Open or Document_New as these will autorun with either opening an existing document or creating a new document respectively.

Alternatively, you could declare it as a constant.
I tried the constant route, adding
Code:
Public Const projectPath As String = "myURL"
but I receive an error that says "Constants, fixed-length strings, arrays, user-defined types, and Declare statements not allowed as Public members of object modules."

For what it's worth, I also tried the two methods for Document variable, but they too ddi not seem to set/persist.
Reply With Quote
  #10  
Old 11-22-2020, 11:24 PM
Charles Kenyon Charles Kenyon is offline Declaring Public variables and accessing in other modules Windows 10 Declaring Public variables and accessing in other modules Office 2019
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,082
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

Document variables persist if the document in which they are created is saved.
Code:
ActiveDocument.Save
'(or)
ThisDocument.Save
They become a part of the document, although not displayed in the document. They are strings and if set to a null value are deleted. (If changed or deleted, the document must again be saved for that change to persist.)

Document variables are available as long as the document/template is open, attached or loaded as an Add-In. I do not know that they can be accessed on a closed document/template. They are available in any module and for that matter any project. For a project other than the one holding the variables, I would probably use a public function in the template holding the variables. It would be a lot easier for me to write.
Reply With Quote
  #11  
Old 11-23-2020, 09:34 AM
gmaxey gmaxey is offline Declaring Public variables and accessing in other modules Windows 10 Declaring Public variables and accessing in other modules Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,422
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Rekent/Charles,

I'm confused as to what you fellows are trying/actually tried to do:
"My experience, tonight, disagrees. Perhaps I am just tired. I tried setting the value of a public variable declared outside a procedure and it blanked when I ran the second procedure trying to access that value."


Remember the ThisDocument.Module of the VB Project is a Special Class Object module (not a standard module).

If you have this code in a VB Project ThisDocument Module:
Code:
Option Explicit
Public p_strPublicTest As String
Sub Document_Open()
  p_strPublicTest = "Testing one, two, theee ..."
End Sub
You can test it in the ThisDocument module using:
Code:
Sub Test()
  MsgBox p_strPublicTest
End Sub
and in a standard module using:

Code:
Option Explicit
Sub Test()
  MsgBox ThisDocument.p_strPublicTest  'This is the public property of the ThisDocument Class
End Sub
If you have this code in as Standard module:

Code:
Option Explicit
Public Const PublicTest = "Test Constant"
Public p_strPublicTest As String

Sub AutoOpen()
  p_strPublicTest = "Testing one, two, three ..."
End Sub
You can test it in that module, another standard module, a userform class module, the this document module, or any other class associated with the project using:

Code:
Sub test()
  MsgBox PublicTest
  MsgBox p_strPublicTest
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/

Last edited by gmaxey; 11-23-2020 at 12:18 PM.
Reply With Quote
  #12  
Old 11-24-2020, 07:27 AM
Charles Kenyon Charles Kenyon is offline Declaring Public variables and accessing in other modules Windows 10 Declaring Public variables and accessing in other modules Office 2019
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,082
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

Thanks for the clarification Greg. I wish I could say that "I knew that!" As it is, I'm glad to keep learning.

@Rekent - the key is placing the line setting the value in the Document_Open procedure in the ThisDocument module.

It might also be in a Document_New procedure in a document template.

For a global template, I do not know of a procedure to parallel AutoExec. You might need a Static AutoExec in a standard module.
Reply With Quote
  #13  
Old 11-24-2020, 07:39 AM
gmaxey gmaxey is offline Declaring Public variables and accessing in other modules Windows 10 Declaring Public variables and accessing in other modules Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,422
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

The public variable needs to be a standard module. You can set it use Document_Open in the ThisDocument module or you could set it using AutoOpen in the same standard module. You don't need Static anywhere.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #14  
Old 11-24-2020, 01:09 PM
Charles Kenyon Charles Kenyon is offline Declaring Public variables and accessing in other modules Windows 10 Declaring Public variables and accessing in other modules Office 2019
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,082
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

Quote:
Originally Posted by gmaxey View Post
The public variable needs to be a standard module. You can set it use Document_Open in the ThisDocument module or you could set it using AutoOpen in the same standard module. You don't need Static anywhere.

If I want this in a global template, what sets the variable's value? That is where I was going with Static AutoExec. Perhaps the AutoExec will do it without the Static so long as the variable is set as public in the class module.



With

Code:
Public strTest as String
in the ThisDocument Module
The following macro in a standard module in the same template generates a Variable not defined error. This is true even though the editor will correct the case of the variable name if typed in all lower case.
Code:
Sub AutoExec()
    Let strTest = "This is a test"
    MsgBox strTest
 End Sub

This also generated an error in hidden module when I attempted to load it as an Add-In.
Reply With Quote
  #15  
Old 11-24-2020, 01:15 PM
Charles Kenyon Charles Kenyon is offline Declaring Public variables and accessing in other modules Windows 10 Declaring Public variables and accessing in other modules Office 2019
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,082
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

In a Global Template, the following works and the variable value persists so long as the template is loaded. The code is in a standard module.

Code:
Option Explicit
Public strTest As String

Static Sub AutoExec()
    Let strTest = "This is a test"
End Sub

Sub TestStringVar()
    MsgBox strTest
End Sub
Here is a link to the template: Public Variable Test.dotm


Using Document Variables is likely easier in global templates (as set forth earlier). That is what I've done in the past.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Declaring Public variables and accessing in other modules Combining 3 Modules into 1 Jude24Joy Word VBA 5 02-01-2017 04:20 PM
Declaring Public variables and accessing in other modules Declaring a variable that is known across modules Officer_Bierschnitt Excel Programming 5 11-18-2015 12:28 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
Declaring Public variables and accessing in other modules Empty Modules Greg S. Excel Programming 2 07-30-2013 01:38 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:15 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