#1
|
|||
|
|||
Declaring Public variables and accessing in other modules
In the "ThisDocument" object I declared Code:
Public filePath As String 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. |
#2
|
|||
|
|||
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 Last edited by Charles Kenyon; 11-24-2020 at 05:09 PM. |
#3
|
|||
|
|||
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 |
#4
|
|||
|
|||
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 Code:
Let ThisDocument.Variables("FilePath").Value = "C:\MyDocs\" Public Function strFilePath() as String Let strFilePath = ThisDocument.Variables("FilePath") End Function |
#5
|
|||
|
|||
|
#6
|
||||
|
||||
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 |
#7
|
|||
|
|||
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 |
#8
|
|||
|
|||
Quote:
Code:
Option Explicit Public projectPath As String Sub setFilePath() Let projectPath = "This is my filepath" End Sub Sub ShowprojectPath() MsgBox "projectPath = " & projectPath End Sub |
#9
|
|||
|
|||
Quote:
Code:
Public Const projectPath As String = "myURL" For what it's worth, I also tried the two methods for Document variable, but they too ddi not seem to set/persist. |
#10
|
|||
|
|||
Document variables persist if the document in which they are created is saved.
Code:
ActiveDocument.Save '(or) ThisDocument.Save 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. |
#11
|
|||
|
|||
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 Code:
Sub Test() MsgBox p_strPublicTest End Sub Code:
Option Explicit Sub Test() MsgBox ThisDocument.p_strPublicTest 'This is the public property of the ThisDocument Class End Sub Code:
Option Explicit Public Const PublicTest = "Test Constant" Public p_strPublicTest As String Sub AutoOpen() p_strPublicTest = "Testing one, two, three ..." End Sub Code:
Sub test() MsgBox PublicTest MsgBox p_strPublicTest End Sub Last edited by gmaxey; 11-23-2020 at 12:18 PM. |
#12
|
|||
|
|||
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. |
#13
|
|||
|
|||
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.
|
#14
|
|||
|
|||
Quote:
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 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. |
#15
|
|||
|
|||
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 Using Document Variables is likely easier in global templates (as set forth earlier). That is what I've done in the past. |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Combining 3 Modules into 1 | Jude24Joy | Word VBA | 5 | 02-01-2017 04:20 PM |
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 |
Empty Modules | Greg S. | Excel Programming | 2 | 07-30-2013 01:38 PM |