I'm trying to automate some report generation where Excel VBA is doing all the work. My employer has a standardized set of templates of which all documents are supposed to be generated from. I need to populate one of these templates from Excel VBA. The Word templates utilize VBA extensively.
This is (some of) my Excel VBA code:
Sub GenerateReport() ' (Tables, InputDataObj)
' code generating the WordApp object (works!)
WordApp.Documents.Add Template:="Brev.dot"
' Getting user information from Utilities.Userinfo macro in Document
Call WordApp.Run("Autoexec") ' generating a public variable
Call WordApp.Run("Utilities.UserInfo")
' more code
End sub
In the Word VBA Autoexec
module, a public variable named user
is defined and declared. The Userinfo
sub from the Utilities
module populates user
. Both these routines are run without any complaints from VBA. I would then like to be able to access the user
variable in my Excel VBA, but I get the following error
Compile Error: Variable not yet created in this context.
How can I access the Word VBA variable in Excel VBA? I thought it more or less was the same?
EDIT: the user
variable is a user defined Type
with only String
attributes. Copying the Word VBA functions that populate the user
variable is absolutely doable, just more work than I though was necessary...
UserInfo
into a function and give it a return value that can be assigned to a variable in Excel – SierraOscarUserInfo
is built into the templates and is not something I'm able to tamper with... – Holene