VBA and file reference

5 Posts
3 Users
0 Likes
56 Views
(@admin_doc72)
Posts: 493
Member Admin
Topic starter
 

Hi

When adding a file reference VBA for instance on Microsoft Excel Library it's work fine on a first PC where Excel is installed on C:

The same application on a second PC where Excel is installed on D: does not work because the file reference is not good.

Does anyone know a workaround ?

 
Posted : 22/08/2018 12:24 pm
ED
 ED
(@e-duvalarcinfo-com)
Posts: 138
Estimable Member
 

Hi Nico
I am not usre I understand the quesiton correclly but here is something you must have come accross but just in case...

Check or Add an Object Library Reference

If you use the objects in other applications as part of your Visual Basic application, you may want to establish a reference to the object libraries of those applications. Before you can do that, you must first be sure that the application provides an object library.

1 - To see if an application provides an object library

From the Tools menu, choose References to display the References dialog box.

The References dialog box shows all object libraries registered with the operating system. Scroll through the list for the application whose object library you want to reference. If the application isn't listed, you can use the Browse button to search for object libraries (*.olb and .tlb) orexecutable files (.exe and *.dll on Windows). References whose check boxes are checked are used by yourproject; those that aren't checked are not used, but can be added.

2 - To add a object library reference to your project

Select the object library reference in the Available References box in the References dialog box and click OK. Your Visual Basic project now has a reference to the application's object library. If you open theObject Browser (press F2) and select the application's library, it displays the objects provided by the selected object library, as well as each object'smethods andproperties. In the Object Browser, you can select aclass in the Classes box and select a method or property in the Members box. Use copy and paste to add the syntax to your code.

 
Posted : 30/08/2018 7:28 pm
(@admin_doc72)
Posts: 493
Member Admin
Topic starter
 

Thank you Manu

I'am OK with all you have written....

I will try to be more clear

I create an application with my PC and choose ta add a reference to Excel library. On my PC Excel is installed on drive E:..PcVue add a reference on a file "E:...Excel...dll"

I send you this application, you have the same Excel but installed on your drive C:

My VBA code will not work because you wil have te re-select the good path with Excel dll in ordre to make it work : on your PC "C:...Excel...dll".

 
Posted : 30/08/2018 8:18 pm
(@admin_doc72)
Posts: 493
Member Admin
Topic starter
 

Hi

Working ten minutes with E Duval, here is the trick :
-adding the reference by code to the correct file (using a parameter or PcVue variable)

Sub AddReference()

Dim BoolExists As Boolean

Set vbProj = ThisProject.VBProject

'~~> Check if "Microsoft Excel 16.0 Object Library" is already added
For Each chkRef In vbProj.References
If chkRef.Name = "Microsoft Excel 16.0 Object Library" Then
BoolExists = True
GoTo CleanUp
End If
Next

'-->Location has to be a parameter
vbProj.References.AddFromFile "C:Program Files (x86)Microsoft OfficerootOffice16Excel.exe"

CleanUp:
If BoolExists = True Then
MsgBox "Reference already exists"
Else
MsgBox "Reference Added Successfully"
End If

Set vbProj = Nothing

End Sub

You can also use vbProj.References.AddFromGUID ( http://www.vbaexpress.com/kb/getarticle.php?kb_id=267 : not sure it's easier !

 
Posted : 30/08/2018 9:11 pm
KASI
 KASI
(@k-simanjalamarcinfo-com)
Posts: 134
Estimable Member
 

Hello Nico,

Thank you so much for the article. After 3 years it is relevant for my case today, and it was for Excel too 🙂

Just some correction on the code :

Microsoft Excel 16.0 Object Library is the description of the reference. The reference name is Excel, so the code should be like below.

Sub AddReference()
   
      Dim BoolExists As Boolean
   
      Set vbProj = ThisProject.VBProject
   
      '~~> Check if "Microsoft Excel 16.0 Object Library" is already added
      For Each chkRef In vbProj.References
          If chkRef.Name = "Excel" Then
              BoolExists = True
              GoTo CleanUp
          End If
      Next
   
      '-->Location has to be a parameter
      vbProj.References.AddFromFile "C:Program FilesMicrosoft OfficerootOffice16Excel.exe"
   
   CleanUp:
      If BoolExists = True Then
          MsgBox "Reference already exists"
      Else
          MsgBox "Reference Added Successfully"
      End If
   
      Set vbProj = Nothing
   
   End Sub
 
Posted : 16/11/2021 2:33 pm