0

I have a problem as below.

A program is written using early binding in VBA and it only works with all the references connected.

but there are some systems where the references are not connected and when the program is ran on those systems there is a compile error.

so I thought maybe I can use late binding to connect the references but it still shows compile error.

condition is that I cant convert the variables in code to late binding.

Public oPart As Part 

Sub CATMain()

Dim refname()
Dim reffullPath()
Dim refGUID()

ReDim refname(5)
refname() = Array("INFITF,MECMOD", "ProductStructureTypeLib")

ReDim reffullPath(5)
reffullPath() = Array("D:\opt\ds\catia\B28_VWGROUP\win_b64\code\bin\MecModTypeLib.tlb", "D:\opt\ds\catia\B28_VWGROUP\win_b64\code\bin\PSTypeLib.tlb")

ReDim refGUID(5)
refGUID() = Array("{0D90A5C9-3B08-11D1-A26C-0000F87546FD}", "{5065F8B6-61BB-11D1-9D85-0000F8759F82}")

CheckAndAddReference refname(), reffullPath(), refGUID()

End Sub

Sub CheckAndAddReference(refname() As Variant, refLocation() As Variant, refGUID() As Variant)

Set VBAEditorx = CreateObject("MSAPC.Apc").VBE  'Application.VBE
Set vbProj = VBAEditorx.ActiveVBProject  'ActiveWorkbook.VBProject

For j = 0 To UBound(refname)
    For i = 1 To vbProj.References.Count
        RefCon = False
        If vbProj.References.Item(i).Name = refname(j) Then
            RefCon = True
            Exit For
        End If
    Next
    If RefCon = False Then
        vbProj.References.AddFromFile refLocation(j)
    End If
Next

End Sub

the first line "Public oPart As Part" , I cant make it as Object because there are many of these in original program.

the above line requires a reference called "INFITF,MECMOD" which is not connected in some systems.

when I try to late bind it, it is showing the error as

Compile error: user type not defined

so I wanted to ask weather i can late bind the reference while the line "Public oPart As Part" remains same in the code without showing an error.

or I need to make all the early bound objects into "As Object".

tls
  • 11
  • 2
  • Can you show an example code for your late binding where you get an error? – Shrotter Sep 27 '22 at 09:11
  • Theoretically, yes. Except the case of MAC OS, which does not accept ActiveX. As required above, can you show some such code lines raising compile errors? And how you declared the variables... – FaneDuru Sep 27 '22 at 09:27
  • Unless it is version problems it it unlikely this would help (and if it was you would fix them). Don't ask us why your solution doesn't work but what the problem is. – Lundt Sep 27 '22 at 09:27
  • 1
    Please provide enough code so others can better understand or reproduce the problem. – Community Sep 27 '22 at 09:33
  • Please show a [mcve] of the code you converted to *late binding* and tell us in which line the error occurs. – Pᴇʜ Sep 27 '22 at 09:41
  • Hi everyone, please look at the attached image, i know its crude but its a very huge code and adding all is a bit of a problem. sorry about this – tls Sep 27 '22 at 09:55
  • @Pᴇʜ i will add the minimal reproducible example in a bit of time – tls Sep 27 '22 at 09:57
  • [An image of your code is not helpful](http://idownvotedbecau.se/imageofcode) – Pᴇʜ Sep 27 '22 at 11:07
  • For *late binding* you cannot use the types of the library like `As Part`. Instead you need to use `As Variant` or if it is an object better use `As Object`. – Pᴇʜ Sep 27 '22 at 11:09
  • Hello all, please check the question again i have made edits to it – tls Sep 27 '22 at 11:56
  • *All* early-bound declarations of object types not defined in the host library or your VBA Project need to be declared "As Object" I don't follow what you mean by "I can't make it as Object because there are many of these in original program" – Tim Williams Sep 27 '22 at 18:45

0 Answers0