Page 1 of 2 12 LastLast
Results 1 to 15 of 25
  1. #1
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Adding a reference to an object library in code (VBA for Word 97/2000)

    I've looked through posts, unless I missed one, on these subject, but still can't quite get the syntax down for adding a reference to an object library in code. I need to add a reference at run time to "Microsoft Visual Basic for Applications Extensability 5.3".

    What I have so far is:

    Dim ExtLib As Object
    Set objCurProj = GlobTemp.VBProject
    Set ExtLib = CreateObject(objCurProj.References.????)

    Thanks!!
    Troy

  2. #2
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Adding a reference to an object library in code (VBA for Word 97/2000)

    <img src=/w3timages/blackline.gif width=33% height=2>
    > I need to add a reference at run time to "Microsoft Visual Basic
    <img src=/w3timages/blackline.gif width=33% height=2>
    Why?

    Troy, check out the threads in this <A target="_blank" HREF=http://groups.google.com/groups?q=VBProject+References&num=100&hl=en>search </A>. Looks like VBProject.References.AddFromFile may be what you want, but it sounds dangerous, hence the initial why question. HTH --Sam
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  3. #3
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Adding a reference to an object library in code (VBA for Word 97/2000)

    The following line will add the reference, but you will need to check the path and the actual file name on your system, as it could differ :<pre> ThisDocument.VBProject.References.AddFromFile _
    "C:Program FilesCommon FilesMicrosoft SharedVBAVBEEXT1.OLB"</pre>

    You could search for VBEEXT*, and base your file path on the result.

    Andrew C

  4. #4
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Adding a reference to an object library in code (VBA for Word 97/2000)

    Thanks!! Almost home free!!

    Just one more thing: Is there some vba element that refers to this path that I could use? Since this will be done on multiple PC's, at some point the path probably won't be the same.

    I could probably take it from the location of

    ProgramsProgram FilesCommon FilesMicrosoft Shared

    I just don't know how to find that in the code.

    Thanks again!!
    Troy

  5. #5
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Adding a reference to an object library in code (VBA for Word 97/2000)

    The location of shared files should be available from the registry. The following key has a path value which should provide what you want :

    HKEY_LOCAL_MACHINESOFTWAREMicrosoftShared Toolsvbeext1.olb.

    I get C:Program FilesCommon FilesMicrosoft SharedVBAVBEEXT1.OLB as the content of the path value from that key.

    Andrew C

  6. #6
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Adding a reference to an object library in code (VBA for Word 97/2000)

    Sorry to have to drag this out one more level, but I have one more question.

    I assume I can use the GetSetting function to retrieve this value. Give that, the syntax is:

    GetSetting(appname, section, key[, default])

    In this case I assume:

    section = HKEY_LOCAL_MACHINESOFTWAREMicrosoftShared Tools
    key = vbeext1.olb

    But what exactly do I put for the appname? Is it "Windows" or "Word" or "Application.????"

    Thanks again!! As always, I'm learning a lot!!
    Troy

  7. #7
    New Lounger
    Join Date
    Dec 2001
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Adding a reference to an object library in code (VBA for Word 97/2000)

    try this snippet

    Sub dd()
    Dim s As String
    s = System.PrivateProfileString("", "HKEY_LOCAL_MACHINESOFTWAREMicrosoftShared Toolsvbeext1.olb", "Path")
    MsgBox s
    End Sub

  8. #8
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Adding a reference to an object library in code (VBA for Word 97/2000)

    Troy,

    The VBA functions GetSetting, SaveSetting etc only work on one Registrty key, i.e. HKEY_CURRENT_USERSoftwareMicrosoftVB and VBA Program Settings, and so cannot provide what you want. As you are using Word, VBADUDE's little snippet will work (for Word 2000 anyway, not sure about 97).

    In Excel, and other flavours of VBA and VB itself, you would need an API call to achieve the same result.

    Hope you can sort things now.

    Andrew C

  9. #9
    New Lounger
    Join Date
    Dec 2001
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Adding a reference to an object library in code (VBA for Word 97/2000)

    'API method - cut and paste this code to a standard module in office 97 or 2k
    'blah blah - standard disclaimers about api calls - note fixed length string

    '-------------------------------

    Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, _
    ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long

    Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, _
    ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As Any, lpcbData As Long) As Long

    Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long

    Public Const HKEY_CURRENT_USER = &H80000001
    Public Const HKEY_LOCAL_MACHINE = &H80000002
    Public Const KEY_QUERY_VALUE = &H1
    Public Const ERROR_SUCCESS = 0&
    Public Const READ_CONTROL = &H20000
    Public Const STANDARD_RIGHTS_READ = (READ_CONTROL)
    Public Const KEY_ENUMERATE_SUB_KEYS = &H8
    Public Const KEY_NOTIFY = &H10
    Public Const KEY_CREATE_LINK = &H20
    Public Const SYNCHRONIZE = &H100000
    Public Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or _
    KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
    Public Const KEY_EXECUTE = (KEY_READ)
    Public Const KEY_ALL_ACCESS = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or _
    KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY Or KEY_CREATE_LINK) And (Not SYNCHRONIZE))
    Public Const REG_DWORD = 4
    Public Const REG_SZ = 1
    Public Const REG_BINARY = 3

    'note the fixed length string
    Dim sData As String * 75
    Dim sPath As String

    Sub blah()

    Dim hResult As Long, retVal As Long, sSubKey As String, _
    sSetting As String, lType As Long, lDataLen As Long

    sSubKey = "SOFTWAREMicrosoftShared Toolsvbeext1.olb"
    retVal = RegOpenKeyEx(HKEY_LOCAL_MACHINE, sSubKey, 0&, KEY_QUERY_VALUE, hResult)

    'error_success = 0
    If retVal = ERROR_SUCCESS Then

    sSetting = "Path"
    lDataLen = Len(sData)

    If hResult <> 0 Then
    retVal = RegQueryValueEx(hResult, sSetting, 0&, lType, sData, lDataLen)
    Else
    MsgBox "error opening key"
    End If

    retVal = RegCloseKey(hResult)
    Else
    MsgBox "Unsuccessful returning default Word file directory"
    End If

    sPath = sData

    Do Until Asc(Right(sPath, 1)) <> 0
    sPath = Left(sPath, Len(sPath) - 1)
    Loop

    MsgBox sPath
    End Sub

  10. #10
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Adding a reference to an object library in code (VBA for Word 97/2000)

    Caveat: on my Win 2000/Office 2000 system the file name vbeext1.olb is not found, that's the name used by Win 98/Office 97.

    For Office 2000, it's VBE6EXT.OLB.

  11. #11
    New Lounger
    Join Date
    Dec 2001
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Adding a reference to an object library in code (VBA for Word 97/2000)

    caveats - lots of em'!

    unless you know the configuration you are working with, this can be a never ending game - this code ran reliably since 98' at a particular client's site

    we tried the same thing in an "internet downloadable" office solution - HEADACHE - trying to troubleshoot stuff via email, ARGHHHHHHH

    tread lightly

  12. #12
    New Lounger
    Join Date
    Dec 2001
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Adding a reference to an object library in code (VBA for Word 97/2000)

    FINAL NOTE -

    for a good look at checking out the registry - check out VB6's package and deployment project (SETUP1.VBP) in the PDWizard subdirectory - lots of interesting stuff going on in there

    note - make sure you save it as another project before you start hacking away on it - this is what the package and deployment wizard uses to create setup packages

    note2 - using the pdwizard can make for an easy way to deploy Office based solutions .... especially if they have file dependencies - this way you KNOW the file will be on there machine because you're installing it

  13. #13
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Adding a reference to an object library in code (VBA for Word 97/2000)

    Troy,

    With all the difficulties of using different versions and possible OS differences, probably adding the reference from the library filename causes just too many problems. Luckily though you can add the reference by use the GUID (Globally Unique Identifier), which should be the same for each VBA version. Hopefully the following will resolve any problems you might be having :<pre> Sub AddVBEEXT_Ref()
    MajVer = Val(Left(Application.VBE.Version, 1))
    MinVer = Val(Right(Application.VBE.Version, 1))
    Application.VBE.ActiveVBProject.References. _
    AddFromGuid "{0002E157-0000-0000-C000-000000000046}", _
    MajVer, MinVer
    End Sub</pre>

    It seems to work fine for me for Office 97 and 2000, WindowsME and NT. Perhaps others might try it and report any problems that might arise.

    Andrew C

  14. #14
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Adding a reference to an object library in code (VBA for Word 97/2000)

    Andrew,

    This seems like a great idea, but I get an error message when I try to run it (see attached screensnip).

    Betraying my sketchy knowledge of this stuff: is the GUID universal and assigned to a library file when the file is created (and therefore the GUID for that library will be the same on every installation of that library on every machine), or rather is it the case that the OS on the individual machine assigns the GUID for that library, at the time the library is registered on that machine? Would the error message indicate the latter is the case?

    Gary
    Attached Images Attached Images

  15. #15
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Adding a reference to an object library in code (VBA for Word 97/2000)

    Gary,

    Thanks for the feedback. My own knowledge is certainly not encyclopaedic, but as I understand it, the GUID, it is generated from an algorithm that ensures that the value is unique and can never be created on a different machine. The same value should then mean the same thing to all PCs at all times. So yes the GUID is assigned once and for all to a library file (or any dll or exe and other COM and ActiveX objects).

    What version of VBA are you using ?. You do need to pass the correct Major and Minor version numbers (of the VBA version, not Office version), otherwise the error you are reporting occurs. But the code as posted should get that correct. Unless there are versions of VBA with x.xx as version numbers. Try using the literal value and see what happens.

    Andrew

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •