VBA Excel - Add references programmatically - excel

I built a excel macro using the "Microsoft Outlook 15.0 Object Library" reference, but when I send the file to other people they get this error: "Can’t find project or library".
This file will be used by a lot of people, so I have to add it from VBA Code.
I'm using this code that runs when you open the excel file, which is returning the following error: "Error in loading DLL"
Private Sub Workbook_Open()
Application.VBE.ActiveVBProject.References.AddFromFile "C:\Program Files (x86)\Microsoft Office\Office15\MSOUTL.OLB\"
End Sub
Do you have any idea why this is happening?
Thanks

If you still want to keep the early binding you could use the GUID in order to add the libray, i.e.
Sub AddOL_Ref()
Application.VBE.ActiveVBProject.REFERENCES. _
AddFromGuid "{00062FFF-0000-0000-C000-000000000046}", 1, 0
End Sub
Advantage of using the GUID is that it is constant and does not change with the version of the program.
Here one finds an extened discussion on dis/advantages of early resp. late binding.

You will have to remove the references & use late binding
or you can also try this. Send your code to one of the user having 2010MS.
Goto VBA > Tools > References
Check for any missing references. You may have Microsoft Outlook 15.0 Object Library showing as missing. Un-check this, browse down and select Microsoft Outlook 14.0 Object Library.

Related

Error handling for excel addin that cannot find networking drive

I have made an addin and placed it on a shared networking drive.
The addin contains a seperat sub that calls the addin whenever an excel document is opened, so the addin will be available in excel all the time.
The addin works fine when i am in my office and when i am connected, however if i for example work from home, and opens excel, then excel gives me the following error:
"Sorry we couldn't find . Is it possible it was moved, renamed or deleted?"
This is because i am not connected and excel cannot retrieve the path i am trying to call the addin from.
Is there a way to handle this error, so the message dont show up? I have tried to ignore the error so the message dont show up but that does not work. How can i exit the sub before the error appears? or an even better solution is to call custom made addins without using a specific networking drive as location.
I have tried the following code to ignore the error.
Sub Open_up ()
Application.DisplayAlerts = False
On Error Resume Next
Call 'name of the addin
End Sub ```
Hope it makes sense
It looks like the add-in was not loaded and not accessible from the shared drive. I'd suggest creating an installer for the add-in, so it could be installed on the end user machines. See Deploy an Office solution by using Windows Installer for more information.

How to make this code run through entirely in one click?

I found this code from spreadsheetguru. The purpose of the code is to activate the Excel 16.0 Library. I want to be able to add this before my code so that it automatically adds the Library and then runs the rest of the code that requires it. Right now I have it in two subs. One is the Private Sub that I have here, and the next is a Public Sub with the rest of my macro. Right now when I run the macro it runs the code psoted here, and then stops. Then If I click again it will run the next sub. Is there any way for me to make this all just go in one click? Maybe by making it all one sub, or some other Private/Public Sub combination I'm not aware of?
Thanks!
Private Sub ActivateReferenceLibrary()
'PURPOSE: Activate Specific Object Libraries
'Error Handler in Case Reference is Already Activated
On Error Resume Next
'Activate PowerPoint Library (version 2.9)
Application.VBE.ActiveVBProject.References.AddFromGuid _
GUID:="{00020813-0000-0000-C000-000000000046}", _
Major:=0, Minor:=0
'Reset Error Handler
On Error GoTo 0
End Sub
In the article spreadsheetguru it is given as Sub ActivateReferenceLibrary() so you need to change your private sub to just sub. Then in your other Sub, i think called averageScoreRelay, at the start you would write this line:
Call ActivateReferenceLibrary.
Make sure both Sub routines are in the same workbook and in standard modules (you can place them in the same standard module).
Bear in mind, some library references are default and cannot be removed. Though you would get a different error message. If you are using Excel 2016 for example, the Excel 16.0 object library will automatically be present by default. You can't add this in to earlier version or remove from 2016 (i believe).
Failing that please post more of your code so we can see how your other Subroutine is working.
Some other solutions, such as late binding where you can remove certain reference dependencies, are discussed here:
VBA reference libraries

EXCEL VBA: Compile error in hidden module only on MacOS

I have a problem with running a VBA in a Excel spreadsheet on a MacOS (Sierra). On Windows everything works like expected. I always get the "Compile error in hidden module: Interpolation ...
I tried to remove the whole content of the module and this is the leftover:
Function interpolate(x As Double, ran As Range) As String
interpolate1 = 1
interpolate = interpolate1
End Function
The VBA code is password protected and I don't use any ActiveX controls.
The references I use are as follows:
Visual Basic For Applications
Microsoft Excel 16.0 Object Library
OLE Automation
Microsoft Office 16.0 Object Library
Microsoft Forms 2.0 Object Library
RefEdit Control
If anyone has any suggestions on how to go about this problem, that would be great.
Thanks,
Tro
The RefEdit reference was missing on MacOS. I removed it and now it seems to work.

Error using application.filedialog to open a file VBA7.0

I have recently installed visio 2010. it has VBA 7.0.
I am trying to write a code to open a file dialog, choose an excel file and open it.
I used this code
Set fd = Application.FileDialog(msoFileDialogFilePicker)
but i get the following error:
Run-time error '438':
Object doesn't support this property or method
i already have the Microsoft Office 14. Object Library as reference.
Do you have an idea? what i should do?
If you use this code in Visio
Set fd = Application.FileDialog(msoFileDialogFilePicker)
the word Application refers to Visio and as you can see in Object Browser, Visio.Application class has no such method as FileDialog. Access and Excel applications have it, but not Visio.
In order to use it in Visio you need to add reference to either Microsoft Excel Object Library or Microsoft Access Object Library. Then you need to use the code below to create an instance of FileDialog (select a proper version depending on what reference did you add to your project - Excel or Access)
'If you have reference to Microsoft Excel Object Library
Set fd = Excel.Application.FileDialog(msoFileDialogFilePicker)
or
'If you have reference to Microsoft Access Object Library
Set fd = Access.Application.FileDialog(msoFileDialogFilePicker)
Like explained by #mielk, Visio does not have the Application.FileDialog. You could use the method of Excel (in this case you'll have to have Excel installed, and started to invoke its methods) or use pure WinAPI from VBA (i.e. GetOpenFileName function). This option is explained here for example:
http://visguy.com/vgforum/index.php?topic=738.0

Excel 2003 on 64-bit Windows 7 automatically changes reference to SysWOW64\MSCOMCTL.OCX so 32-bit Excel complains

In an Excel 2003 VBA project I am using controls from MSCOMCTL.OCX. That is the VBA project has a reference to System32\MSCOMCTL.OCX.
When I open that project in Excel 2003 on my 64-bit Windows 7 system, Excel automatically changes the reference to SysWOW64\MSCOMCTL.OCX (which is the correct location).
However, when I send that project to my client who is using 32-bit Windows XP, the project complains during opening because SysWOW64\MSCOMCTL.OCX does not exist on his system.
Here are the (unsatisfactory) solutions I came up with so far:
Instruct the client to manually change the reference back to the correct location on his system (System32\MSCOMCTL.OCX).
This does not really work for the following reasons:
When Excel 2003 32-bit opens the sheet and it cannot find the reference to MSCOMCTL, it removes all the controls that came from the library (e. g. TreeCtrl) from the forms :-(
Client is struggling with the procedure and it is quite cumbersome for him.
Automatically correct the reference using VBA's VBProject.References.AddFromFile/AddFromGuid.
Same problem as above: When compilation of VBA during opening of workbook fails, Excel will remove all controls that it could not find from the forms.
Automatically add the reference (as in 2.) and use dynamic binding to add all the relevant controls during runtime.
This could actually work, however currently I am struggling with binding the event handlers to the controls (but that will be separate question ;-)
Approaches 1. and 2. do not really solve anything and solution 3 is a lot of work.
Any ideas would be greatly appreciated.
What if you automatically turned the reference off when the workbook closed? that way the reference wouldn't be 'broken' when the workbook is opened, and all your control should still be good.
i.e. :
Private Sub Workbook_Open()
'use environ variable for folder locs
If os = "64bit" Then
Me.VBProject.References.AddFromFile ("C:\WINDOWS\SysWOW64\MSCOMCTL.OCX")
Else
Me.VBProject.References.AddFromFile ("C:\WINDOWS\system32\MSCOMCTL.OCX")
End If
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
For Each ref In Me.VBProject.References
If ref.Name = "MSComctlLib" Then
Me.VBProject.References.Remove ref
End If
Next ref
End Sub
I did a quick test with the ADODB dll and it seemed to work,but I am not sure how you're using that DLL specifically; let me know if that works, though! Sure a lot better than option 3!

Resources