Adding References through code problem

J

Joel Wiseheart

I tried to create a function to add References through
code. It almost works, but crashes occasionally when
comparing path names, to make sure the reference doesn't
already exist.

The reason is that Sometimes ref.FullPath
returns 'C:\Program Files\Common Files\...' and other
times it returns 'C:\Progra~1\Common~1\...', and it
doesn't see these two strings as being equal. I put >>>>
and <<<< around the line where this problem is occuring.

Here's the code, passing 'cdo.dll' and 'BarTend.exe' as
reference variables:

Public Function AddReference(strRefNameAndExt As String,
Optional strRefPath As String)
'Purpose: Adds reference for new installations.
'Variables: strRefNameAndExt = The name and file extension
of the reference being added.
' strRefPath = Path name to begin
search. "c:\Program Files" is used by default,
' unless otherwise specified.
'On Error GoTo Error_AddReference

Dim strReturnedPath As String
Dim ref As Reference

With Application.FileSearch 'Search for
reference called by function.
.NewSearch
If strRefPath = "" Then
.LookIn = "C:\Program Files"
Else
.LookIn = "strRefPath"
End If
.SearchSubFolders = True
.FileName = strRefNameAndExt
.MatchTextExactly = True
.Execute
strReturnedPath = .FoundFiles.Item(1)
strReturnedPath = .FileName
End With

For Each ref In Application.References'If reference doesn't already exist...
Exit Function
End If
Next

References.AddFromFile
(strReturnedPath) '...then add the reference.

Exit_AddReference:
Exit Function

Error_AddReference:
Call ErrorHandlerCustom
("mdlAddReference", "AddReference")
Resume Exit_AddReference

End Function

If I put a Debug.Print statement in for the ref.FullPath
statement, here's the result:

C:\Program Files\Common Files\Microsoft
Shared\VBA\VBA332.DLL

C:\Program Files\Microsoft Office\Office\MSACC8.OLB

C:\Program Files\Common Files\Microsoft
Shared\DAO\dao350.dll

C:\WINDOWS\System32\mscomct2.ocx

C:\PROGRA~1\MICROS~2\Office10\msoutl.olb <<<<8 char.

C:\PROGRA~1\COMMON~1\System\Mapi\1033\CDO.DLL <<<<8 char.

C:\Program Files\Seagull\BarTender 6.20
\Enterprise\BarTend.exe

Notice the path name for Outlook and MAPI uses the 8-
character abbrviation, but not in the dao350.dll? How do I
get it to not do that?

Thanks!
 
J

Joel Wiseheart

-----Original Message-----
I tried to create a function to add References through
code. It almost works, but crashes occasionally when
comparing path names, to make sure the reference doesn't
already exist.

The reason is that Sometimes ref.FullPath
returns 'C:\Program Files\Common Files\...' and other
times it returns 'C:\Progra~1\Common~1\...', and it
doesn't see these two strings as being equal. I put >>>>
and <<<< around the line where this problem is occuring.

Here's the code, passing 'cdo.dll' and 'BarTend.exe' as
reference variables:

Public Function AddReference(strRefNameAndExt As String,
Optional strRefPath As String)
'Purpose: Adds reference for new installations.
'Variables: strRefNameAndExt = The name and file extension
of the reference being added.
' strRefPath = Path name to begin
search. "c:\Program Files" is used by default,
' unless otherwise specified.
'On Error GoTo Error_AddReference

Dim strReturnedPath As String
Dim ref As Reference

With Application.FileSearch 'Search for
reference called by function.
.NewSearch
If strRefPath = "" Then
.LookIn = "C:\Program Files"
Else
.LookIn = "strRefPath"
End If
.SearchSubFolders = True
.FileName = strRefNameAndExt
.MatchTextExactly = True
.Execute
strReturnedPath = .FoundFiles.Item(1)
########strReturnedPath = .FileName#############
End With

For Each ref In Application.References
'If reference doesn't already exist...
Exit Function
End If
Next

References.AddFromFile
(strReturnedPath) '...then add the reference.

Exit_AddReference:
Exit Function

Error_AddReference:
Call ErrorHandlerCustom
("mdlAddReference", "AddReference")
Resume Exit_AddReference

End Function

If I put a Debug.Print statement in for the ref.FullPath
statement, here's the result:

C:\Program Files\Common Files\Microsoft
Shared\VBA\VBA332.DLL

C:\Program Files\Microsoft Office\Office\MSACC8.OLB

C:\Program Files\Common Files\Microsoft
Shared\DAO\dao350.dll

C:\WINDOWS\System32\mscomct2.ocx

C:\PROGRA~1\MICROS~2\Office10\msoutl.olb <<<<8 char.

C:\PROGRA~1\COMMON~1\System\Mapi\1033\CDO.DLL <<<<8 char.

C:\Program Files\Seagull\BarTender 6.20
\Enterprise\BarTend.exe

Notice the path name for Outlook and MAPI uses the 8-
character abbrviation, but not in the dao350.dll? How do I
get it to not do that?

Thanks!

.
Oops! There was a line I put in during troubleshooting
efforts that doesn't belong! It's marked with a ######.

I also decided to put in a debug.print line for both
variables, to illustrate the problem:

ref.FullPath = C:\Program Files\Common Files\Microsoft
Shared\VBA\VBA332.DLL
strReturnedPath = C:\Program Files\Common
Files\System\Mapi\1033\CDO.DLL


ref.FullPath = C:\Program Files\Microsoft
Office\Office\MSACC8.OLB
strReturnedPath = C:\Program Files\Common
Files\System\Mapi\1033\CDO.DLL


ref.FullPath = C:\Program Files\Common Files\Microsoft
Shared\DAO\dao350.dll
strReturnedPath = C:\Program Files\Common
Files\System\Mapi\1033\CDO.DLL


ref.FullPath = C:\WINDOWS\System32\mscomct2.ocx
strReturnedPath = C:\Program Files\Common
Files\System\Mapi\1033\CDO.DLL


ref.FullPath = C:\PROGRA~1\MICROS~2\Office10\msoutl.olb
strReturnedPath = C:\Program Files\Common
Files\System\Mapi\1033\CDO.DLL


' These should be equal, but since the program sees the
paths as strings, they're not:
ref.FullPath = C:\PROGRA~1\COMMON~1\System\Mapi\1033
\CDO.DLL
strReturnedPath = C:\Program Files\Common
Files\System\Mapi\1033\CDO.DLL


ref.FullPath = C:\Program Files\Seagull\BarTender 6.20
\Enterprise\BarTend.exe
strReturnedPath = C:\Program Files\Common
Files\System\Mapi\1033\CDO.DLL
 
J

Joel Wiseheart

It worked fantastic! Thank you!
-----Original Message-----
last line in the function better this other way

GetLongPath = Left(LongPath, InStr(LongPath, Chr(0)) - 1)

;-)

--
Saludos desde Barcelona
Juan M. Afan de Ribera
<MVP Ms Access>


"Juan M. Afan de Ribera" <[email protected]> escribió en el mensaje
call it like this,
ie:


.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top