Get variables from .xlam to a .xlsb - excel

Recently I discovered about .xlam files to reuse functions that I use in several files. I'm not a programmer so this is maybe something easy but I've been hours into this and I can't find the way to solve it.
I've just created a project with some of the functions that I used most and I saved the file in a Sharepoint site. After that I created a Reference and everything seems to be working well. I can Call functions just with a
call FunctionName()
And the functions are running well. But I haven't found the way to return global variables from the .xlam file to my .xlsb file
I'm defining a Global variable just like this in a Module in the .xlam
Global CRegion As String
In the same module I have this easy macro:
Function GetRegion()
CRegion = Range("B2").Value
End Function
And then in my .xlsb file Im testing something like this after calling the GetRegion Function.
Msgbox CRegion
The result is an empty Message Box. I've tried to define a global variable with the same name in my .xlsb file and it still does not work.
¿Any clue to solve this?

Have not tested it, but try:
To make a public variable available to other workbooks, from a new workbook select the workbook containing the public variable in the Available References box of the References dialog box (from a module sheet, click References on the Tools menu).
Source: MSDN
http://support.microsoft.com/kb/141693

Related

Name conflict error pops when creating reference at runtime, but not at design time

I'm having this staggering problem in creating a reference at runtime.
In short: I have an addin referenced in another addins, all created by myself and all working fine (except for some annoying popup which appears on loading Excel). As a workaround to this annoyance, I removed the existing reference to create it later, at runtime. But when I try creating the reference, it returns an "Name conflicts with existing module, project, or object library" error popup, and the reference is not created--which makes no sense, because if I create the reference at design time it works perfectly. There is no module or project with repeated name.
Now explaining a little further.
I have four kinda-complex Excel applications I'd had developed for my job. Each one is a VBA project, distributed as Excel Addins (.xlam).
Additionally, I have a fifth project with functions common to the other four. This fifth project is referenced in the other ones (via Tools -> References). So now, all I have to do is to call them as any API outside referenced function (commonProject.Function (arg1, arg2, arg3 etc)).
Unfortunately, I had some problems with a popup message (here) and the workaround seems to be referencing the fifth project in the other ones at runtime.
In order to do this, I uninstalled three of the other four Addins, remaining only one, and used the following code in its Workbook_Open event:
Private Sub Workbook_Open()
Dim strNamePath As String
Dim bolAddinIsRefered as Boolean
Dim oRef As Variant
strNamePath = "c:\etc\etc\etc\Filename.xlam"
For Each oRef In ThisWorkbook.VBProject.References
If oRef.FullPath = strNamePath Then bolAddinIsRefered = True
Next oRef
If bolAddinIsRefered = False Then ThisWorkbook.VBProject.References.AddFromFile strNamePath
End Sub
The problem is, when I used to create the reference at design time by hand, it worked mostly fine (except for the annoying popup I'm now trying to circumvent). But when I try creating the Reference at runtime, it returns an "Name conflicts with existing module, project, or object library" error popup, and the reference is not created.
I have verified the module and worksheet names and there is no duplicated one.
Does anyone have any idea how to prevent this error?
it seems after one and a half day struggling with a problem, when you ask the question on StackOverflow the solution just pops in your mind.
I'd already tried to change both projects names, even internal functions or subs resembling the same name. But I had not looked upon the FILE NAME!
As my solution is called Sisyphus, the filenames of all addins started with "Sisyphus"--"Sisyphus Common Functions.xlam", "Sisyphus DocMerge.xlam" etc.
The problem was VBA was comparing the first word in filename. I removed spaces and it worked well. Now my filenames are "SisyphusCommonFunctions.xlam", "SisyphusDocMerge.xlam" etc. and the referencing in runtime works all right.
Thank you for your time, I'll let this Question and Answer here, because it can be usefull to someone.

Cannot run common module in xlam file as macro is disabled

VBA / Excel - 2007
I want to create one (possibly many) .xlam file(s) to hold common modules accessible across projects. Along the way I have received a number of errors but through the creation of a "mickey-mouse" scenario I have boiled it down to one error condition (as shown in actual result):-
I have seen several problems reported here related to this which in the end have either not been answered or the solution has not quite hit the mark
As a precursor to this I was able to put my common module into another .xlsm project and successfully execute it by using a reference to that called project. However it would be preferable not to create a workbook for the sole purpose of housing common modules
'Caller VBAProject (Caller.xlsm):
Public Sub Caller()
Dim i As Integer
i = 0
'*** Error in line below***
Application.Run "C:\Users\IT\AppData\Roaming\Microsoft\AddIns\Common.xlam!Test", i
End Sub
'Called Test (Common.xlam) -- different project, obviously
Function Test(ByRef i As Integer) As Boolean
If i = 0 Then
Test = False
Else
Test = True
End If
End Function
Actual Result
Run-time error '1004':
Cannot run the macro
'C:\Users\IT\AppData\Roaming\Mirosoft\AddIns\Common.xlam!Test'
The macro may not be available in this workbook or all macros may
be disabled.
Steps Undertaken (in Excel Options)
Trust Center
a) Macro Settings
-- both i) and ii) (at different times)
i) Disabled all macros with notification
ii) Enabled all macros
iii) set Trust access to the VBA project object model
b) Add-ins -- left as default ie no option ticked
c) Trusted Locations -- have added the following
C:\Users\IT\AppData\Roaming\Microsoft\AddIns\
Add-Ins
As an Active Application Add-in I have
C:\Users\IT\AppData\Roaming\Microsoft\AddIns\Common.xlam
Can anybody please tell me what I might have missed?
So just to summarise I don't have a direct answer to my question thus far, that is how to avoid the 1004 error when calling a macro within a .xlam file. However, I do have a more than adequate alternative which is to import the common file into a different module within the same project. What I'm realising is that as I write this it isn't an import at runtime or late binding as Zac implied. I'm not sure I'm too worried though (at least at the moment).

How can i define the file path when opening workbook?

I have a macro that opens a workbook (each time a different one) and imports data from it. But when I activate it, the standard path is to my documents. How can i define a file path to a specific folder?
If you're looking for a way to change the default directory for choosing a file you would use:
ChDir (ThisWorkbook.Path)
However, if you're looking for some way to open a specific filepath, you would have to use a FilePickerDialog, assign a String to that user input, then use something like:
[StringVarName] = Application.GetOpenFilename
If you post your code with your attempt at fixing the issue, we would be able to better assist.

Injecting module into excel and run it

In VBScript I'm injecting a module into excel files and then I want to run them. The injection goes fine but when I run it says it can't find it. I've put the location in the trust center so it should trust it just fine. The module has a public sub named Run as well.
Dim XL
Set XL = CreateObject("Excel.Application")
Dim book
Set book = XL.Workbooks.Open(wkpath + "\" + wkname, 0, false)
book.VBProject.VBComponents.Import "C:\MyModule.bas"
XL.Application.Run("'" + wkname + "'!Run")
The paths and names all work out. Am I doing something wrong with this? What are my next debugging steps here.
[EDIT]
Actually it looks like some references aren't being selected now so it's getting an error about user-defined type not defined, but that's not the error that I get from VBScript. I had to do what is happening manually and then I saw that error.
So I was missing a reference but the error was pretty bogus. I figured this out by stopping my process once the excel file was open and adding in the module manually and trying to run the function. So I added the following in vbscript and it worked:
book.VBProject.References.AddFromFile "path to my xlam that has the type I use"

Finding the name of excel file from xla

I am using an xla file to run my vba-code in excel. I have many templates, which use this xla file. Is there any way to find which template runs(opens) the xla file from xla.
I have used:
ThisWorkbook.FullName
but it returns the name of the xla file and not the name of the template
I don't think there is a straightforward way for a Function or a Sub to find out who's calling it (because the call/stack tracing is not available programatically to VBA).
You could do your own tracing, by implementing some push and pop operations on a custom-made stack globally accessible in your .xla, for example. When a function is called, it pushes its filename+module+name to the stack, and when it ends it pops that information from the stack.
But then again, it depends on the "benevolence" of the calling function (e.g. the one from the file that references your .xla file) to use your tracing convention. Also, it might make your code more brittle. And also, it could make it worse actually, if you don't handle every exception that might break the execution of your functions/subs.
See also this (last section): http://www.cpearson.com/excel/DebuggingVBA.aspx

Resources