E
EagleOne
XL 2003, 2007
Using VB Express 2008, I have created, "Signed," GAC and registered a ComVisible DLL called
ExcelTrans.Dll. The Dll "Built" fine meaning there were no warnings or errors.
In VBE, I used Tools>References and added ExcelTrans.Tools
Then I attempted to use one of the embedded (in ExcelTrans.Tools) the Subroutines:
myFormat() and
myUnformat() with the code as follows:
Sub TestDLL()
Dim MF As ExcelTrans.Tools ' this line works
Set MF = New ExcelTrans.Tools ' the "SET" works fine
Call MF.myFormat ' Intellisense "sees" myFormat
and myUnformat meaning that
I can select either in a dropdown
End Sub
Thanks to Chip Pearson, I made alterations to his code on www.cpearson.com
Bottom line, I am not sure how to use the the Subroutines in the DLL with Excel VBA. The following
code is provided as background only.
TIA EagleOne
The code used in VB 2008 Express creates one "Class :"Tools" and two Public Subs() called:
myFormat() and
myUnformat()
'*******************************************************************************************************
'****************************** VB 2008 Express Code *******************************************
'*******************************************************************************************************
Option Explicit On
Option Compare Text
Option Strict Off
Imports XL = Microsoft.Office.Interop.Excel
Imports OFC = Microsoft.Office.Interop
Imports System
Imports System.Runtime.InteropServices
Imports Microsoft.Win32
Imports Microsoft.VisualBasic
Imports Microsoft.Vbe.Interop.vbext_ProcKind
<ClassInterface(ClassInterfaceType.AutoDual), ComVisible(True)> Public Class Tools
Private Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal strClass As String, _
ByVal lpWindow As String) As Long
Public Sub myFormat()
'
Dim myCode As String
Dim modName As String
Dim AppPath As String
Dim XLPath As String
Dim i As Long
Dim j As Long
Dim str As String
Dim blnStringMode As Boolean
Dim blnLineContinue As Boolean
Dim lCount As Long
Dim xlsApp As Microsoft.Office.Interop.Excel.Application
Dim xlsWB As Microsoft.Office.Interop.Excel.Workbook
Dim xlsSheet As Microsoft.Office.Interop.Excel.Worksheet
'Dim xlsCell As Microsoft.Office.Interop.Excel.Range
'***************** Code to access current instance of Excel **************************
Dim nWnd As IntPtr
Dim ceroIntPtr As New IntPtr(0)
Dim Wnd_name As String
Wnd_name = "Excel"
nWnd = FindWindow(Nothing, Wnd_name)
'show the info
If nWnd.Equals(ceroIntPtr) Then
MsgBox("Creating New session of Excel")
xlsApp = New Microsoft.Office.Interop.Excel.Application
Else
MsgBox("Connecting to Current session of Excel")
xlsApp = GetObject(, "Excel.Application")
End If
xlsApp.Visible = True
xlsWB = xlsApp.ActiveSheet
xlsSheet = xlsWB.Worksheets(1)
'***************** End Code to access current instance of Excel ************************
modName = "Tools"
' ........... VBA code to Remove indentations
' ........... VBA code to Remove Removes comments
' ........... VBA code to Removes blank lines
End Sub
Public Sub myUnformat()
'
Dim myCode As String
Dim modName As String
Dim AppPath As String
Dim XLPath As String
Dim xlsApp As Microsoft.Office.Interop.Excel.Application
Dim xlsWB As Microsoft.Office.Interop.Excel.Workbook
Dim xlsSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim nWnd As IntPtr
Dim ceroIntPtr As New IntPtr(0)
Dim Wnd_name As String
Wnd_name = "Excel"
nWnd = FindWindow(Nothing, Wnd_name)
'show the info
If nWnd.Equals(ceroIntPtr) Then
MsgBox("Creating New session of Excel")
xlsApp = New Microsoft.Office.Interop.Excel.Application
Else
MsgBox("Connecting to Current session of Excel")
xlsApp = GetObject(, "Excel.Application")
End If
xlsApp.Visible = True
xlsWB = xlsApp.ActiveSheet
xlsSheet = xlsWB.Worksheets(1)
modName = "myUnformat"
' ........... VBA code to Undo Remove indentations above
' ........... VBA code to Undo Remove Removes comments above
' ........... VBA code to Undo Removes blank lines above
End Sub
<ComRegisterFunctionAttribute()> Public Shared Sub RegisterFunction(ByVal type As Type)
Registry.ClassesRoot.CreateSubKey(GetSubkeyName(type))
End Sub
<ComUnregisterFunctionAttribute()> Public Shared Sub UnregisterFunction(ByVal type As Type)
Registry.ClassesRoot.DeleteSubKey(GetSubkeyName(type), False)
End Sub
Private Shared Function GetSubkeyName(ByVal type As Type) As String
Dim S As New System.Text.StringBuilder()
S.Append("CLSID\{")
S.Append(type.GUID.ToString().ToUpper())
S.Append("}\Programmable")
Return S.ToString()
End Function
End Class
'*******************************************************************************************************
'****************************** End VB 2008 Express Code ***************************************
'*******************************************************************************************************
Using VB Express 2008, I have created, "Signed," GAC and registered a ComVisible DLL called
ExcelTrans.Dll. The Dll "Built" fine meaning there were no warnings or errors.
In VBE, I used Tools>References and added ExcelTrans.Tools
Then I attempted to use one of the embedded (in ExcelTrans.Tools) the Subroutines:
myFormat() and
myUnformat() with the code as follows:
Sub TestDLL()
Dim MF As ExcelTrans.Tools ' this line works
Set MF = New ExcelTrans.Tools ' the "SET" works fine
Call MF.myFormat ' Intellisense "sees" myFormat
and myUnformat meaning that
I can select either in a dropdown
End Sub
Thanks to Chip Pearson, I made alterations to his code on www.cpearson.com
Bottom line, I am not sure how to use the the Subroutines in the DLL with Excel VBA. The following
code is provided as background only.
TIA EagleOne
The code used in VB 2008 Express creates one "Class :"Tools" and two Public Subs() called:
myFormat() and
myUnformat()
'*******************************************************************************************************
'****************************** VB 2008 Express Code *******************************************
'*******************************************************************************************************
Option Explicit On
Option Compare Text
Option Strict Off
Imports XL = Microsoft.Office.Interop.Excel
Imports OFC = Microsoft.Office.Interop
Imports System
Imports System.Runtime.InteropServices
Imports Microsoft.Win32
Imports Microsoft.VisualBasic
Imports Microsoft.Vbe.Interop.vbext_ProcKind
<ClassInterface(ClassInterfaceType.AutoDual), ComVisible(True)> Public Class Tools
Private Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal strClass As String, _
ByVal lpWindow As String) As Long
Public Sub myFormat()
'
Dim myCode As String
Dim modName As String
Dim AppPath As String
Dim XLPath As String
Dim i As Long
Dim j As Long
Dim str As String
Dim blnStringMode As Boolean
Dim blnLineContinue As Boolean
Dim lCount As Long
Dim xlsApp As Microsoft.Office.Interop.Excel.Application
Dim xlsWB As Microsoft.Office.Interop.Excel.Workbook
Dim xlsSheet As Microsoft.Office.Interop.Excel.Worksheet
'Dim xlsCell As Microsoft.Office.Interop.Excel.Range
'***************** Code to access current instance of Excel **************************
Dim nWnd As IntPtr
Dim ceroIntPtr As New IntPtr(0)
Dim Wnd_name As String
Wnd_name = "Excel"
nWnd = FindWindow(Nothing, Wnd_name)
'show the info
If nWnd.Equals(ceroIntPtr) Then
MsgBox("Creating New session of Excel")
xlsApp = New Microsoft.Office.Interop.Excel.Application
Else
MsgBox("Connecting to Current session of Excel")
xlsApp = GetObject(, "Excel.Application")
End If
xlsApp.Visible = True
xlsWB = xlsApp.ActiveSheet
xlsSheet = xlsWB.Worksheets(1)
'***************** End Code to access current instance of Excel ************************
modName = "Tools"
' ........... VBA code to Remove indentations
' ........... VBA code to Remove Removes comments
' ........... VBA code to Removes blank lines
End Sub
Public Sub myUnformat()
'
Dim myCode As String
Dim modName As String
Dim AppPath As String
Dim XLPath As String
Dim xlsApp As Microsoft.Office.Interop.Excel.Application
Dim xlsWB As Microsoft.Office.Interop.Excel.Workbook
Dim xlsSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim nWnd As IntPtr
Dim ceroIntPtr As New IntPtr(0)
Dim Wnd_name As String
Wnd_name = "Excel"
nWnd = FindWindow(Nothing, Wnd_name)
'show the info
If nWnd.Equals(ceroIntPtr) Then
MsgBox("Creating New session of Excel")
xlsApp = New Microsoft.Office.Interop.Excel.Application
Else
MsgBox("Connecting to Current session of Excel")
xlsApp = GetObject(, "Excel.Application")
End If
xlsApp.Visible = True
xlsWB = xlsApp.ActiveSheet
xlsSheet = xlsWB.Worksheets(1)
modName = "myUnformat"
' ........... VBA code to Undo Remove indentations above
' ........... VBA code to Undo Remove Removes comments above
' ........... VBA code to Undo Removes blank lines above
End Sub
<ComRegisterFunctionAttribute()> Public Shared Sub RegisterFunction(ByVal type As Type)
Registry.ClassesRoot.CreateSubKey(GetSubkeyName(type))
End Sub
<ComUnregisterFunctionAttribute()> Public Shared Sub UnregisterFunction(ByVal type As Type)
Registry.ClassesRoot.DeleteSubKey(GetSubkeyName(type), False)
End Sub
Private Shared Function GetSubkeyName(ByVal type As Type) As String
Dim S As New System.Text.StringBuilder()
S.Append("CLSID\{")
S.Append(type.GUID.ToString().ToUpper())
S.Append("}\Programmable")
Return S.ToString()
End Function
End Class
'*******************************************************************************************************
'****************************** End VB 2008 Express Code ***************************************
'*******************************************************************************************************