例如
当我们在vb的IDE环境下键入private sub text,后然回车时
IDE环境会自动把相关的字母转为大写,text后加上括号,并在这句话的后面加上End sub
当我们在VB的IDE环境下双击一个控件后,IDE环境会自动在Code界面以控件名为参数增加一个private Sub。。等等。
请问这些事件在Add_in里如何响应
17 个解决方案
#1
没有看懂
不过强烈关注
帮你顶一下~~~
不过强烈关注
帮你顶一下~~~
#2
关注
#3
Add_in?
当我们在vb的IDE环境下键入private sub text,后然回车时
IDE环境会自动把相关的字母转为大写,text后加上括号,并在这句话的后面加上End sub
当我们在VB的IDE环境下双击一个控件后,IDE环境会自动在Code界面以控件名为参数增加一个private Sub。。等等。
//你可以自己用RICHTEXT编写啊
当我们在vb的IDE环境下键入private sub text,后然回车时
IDE环境会自动把相关的字母转为大写,text后加上括号,并在这句话的后面加上End sub
当我们在VB的IDE环境下双击一个控件后,IDE环境会自动在Code界面以控件名为参数增加一个private Sub。。等等。
//你可以自己用RICHTEXT编写啊
#4
vb的Add_in功能是一种类似于word宏的功能,可以在Add_in中对整个VB环境进行操作,就我目前找到的功能来说,可以对VB菜单事件,打开新工程事件,关闭新工程事件等进行响应,并可以把VB环境当成一个对象进行操作,功能比较强大,大家可以在msdn中找到一个名叫taborder的用于更改VB控件tab属性的Add_in例子。
但我除了以上功能之外还想找到响应IDE环境中双击控件和回车的事件,在网上找了很久,没见到相应的例子,所以请求大家帮忙
但我除了以上功能之外还想找到响应IDE环境中双击控件和回车的事件,在网上找了很久,没见到相应的例子,所以请求大家帮忙
#5
re ryuginka(ryuginka)
我的目的是做一个Add_in 接管VBIDE环境的这种自动生成。
比如说在双击一个控件后,我要求除了自动增加private sub和end sub外,还能自动生成相应的注释。
我的目的是做一个Add_in 接管VBIDE环境的这种自动生成。
比如说在双击一个控件后,我要求除了自动增加private sub和end sub外,还能自动生成相应的注释。
#6
有没高手可以解答啊?分不够可以接着加
#7
'code in connect.Dsr
Option Explicit
Public FormDisplayed As Boolean
Public VBInstance As VBIDE.VBE
Dim mcbMenuCommandBar As Office.CommandBarControl
Dim mfrmAddIn As New frmAddIn
Public WithEvents MenuHandler As CommandBarEvents 'command bar event handler
Sub Hide()
On Error Resume Next
FormDisplayed = False
mfrmAddIn.Hide
End Sub
Sub Show()
On Error Resume Next
If mfrmAddIn Is Nothing Then
Set mfrmAddIn = New frmAddIn
End If
Set mfrmAddIn.VBInstance = VBInstance
Set mfrmAddIn.Connect = Me
FormDisplayed = True
mfrmAddIn.Show
End Sub
'------------------------------------------------------
'this method adds the Add-In to VB
'------------------------------------------------------
Private Sub AddinInstance_OnConnection(ByVal Application As Object, ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, ByVal AddInInst As Object, custom() As Variant)
On Error GoTo error_handler
'save the vb instance
Set VBInstance = Application
'this is a good place to set a breakpoint and
'test various addin objects, properties and methods
Debug.Print VBInstance.FullName
If ConnectMode = ext_cm_External Then
'Used by the wizard toolbar to start this wizard
Me.Show
Else
Set mcbMenuCommandBar = AddToAddInCommandBar("My AddIn")
'sink the event
Set Me.MenuHandler = VBInstance.Events.CommandBarEvents(mcbMenuCommandBar)
End If
If ConnectMode = ext_cm_AfterStartup Then
If GetSetting(App.Title, "Settings", "DisplayOnConnect", "0") = "1" Then
'set this to display the form on connect
Me.Show
End If
End If
Exit Sub
error_handler:
MsgBox Err.Description
End Sub
'------------------------------------------------------
'this method removes the Add-In from VB
'------------------------------------------------------
Private Sub AddinInstance_OnDisconnection(ByVal RemoveMode As AddInDesignerObjects.ext_DisconnectMode, custom() As Variant)
On Error Resume Next
'delete the command bar entry
mcbMenuCommandBar.Delete
'shut down the Add-In
If FormDisplayed Then
SaveSetting App.Title, "Settings", "DisplayOnConnect", "1"
FormDisplayed = False
Else
SaveSetting App.Title, "Settings", "DisplayOnConnect", "0"
End If
Unload mfrmAddIn
Set mfrmAddIn = Nothing
End Sub
Private Sub IDTExtensibility_OnStartupComplete(custom() As Variant)
If GetSetting(App.Title, "Settings", "DisplayOnConnect", "0") = "1" Then
'set this to display the form on connect
Me.Show
End If
End Sub
'this event fires when the menu is clicked in the IDE
Private Sub MenuHandler_Click(ByVal CommandBarControl As Object, handled As Boolean, CancelDefault As Boolean)
Me.Show
End Sub
Function AddToAddInCommandBar(sCaption As String) As Office.CommandBarControl
Dim cbMenuCommandBar As Office.CommandBarControl 'command bar object
Dim cbMenu As Object
On Error GoTo AddToAddInCommandBarErr
'see if we can find the Add-Ins menu
Set cbMenu = VBInstance.CommandBars("Add-Ins")
If cbMenu Is Nothing Then
'not available so we fail
Exit Function
End If
'add it to the command bar
Set cbMenuCommandBar = cbMenu.Controls.Add(1)
'set the caption
cbMenuCommandBar.Caption = sCaption
Set AddToAddInCommandBar = cbMenuCommandBar
Exit Function
AddToAddInCommandBarErr:
End Function
Option Explicit
Public FormDisplayed As Boolean
Public VBInstance As VBIDE.VBE
Dim mcbMenuCommandBar As Office.CommandBarControl
Dim mfrmAddIn As New frmAddIn
Public WithEvents MenuHandler As CommandBarEvents 'command bar event handler
Sub Hide()
On Error Resume Next
FormDisplayed = False
mfrmAddIn.Hide
End Sub
Sub Show()
On Error Resume Next
If mfrmAddIn Is Nothing Then
Set mfrmAddIn = New frmAddIn
End If
Set mfrmAddIn.VBInstance = VBInstance
Set mfrmAddIn.Connect = Me
FormDisplayed = True
mfrmAddIn.Show
End Sub
'------------------------------------------------------
'this method adds the Add-In to VB
'------------------------------------------------------
Private Sub AddinInstance_OnConnection(ByVal Application As Object, ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, ByVal AddInInst As Object, custom() As Variant)
On Error GoTo error_handler
'save the vb instance
Set VBInstance = Application
'this is a good place to set a breakpoint and
'test various addin objects, properties and methods
Debug.Print VBInstance.FullName
If ConnectMode = ext_cm_External Then
'Used by the wizard toolbar to start this wizard
Me.Show
Else
Set mcbMenuCommandBar = AddToAddInCommandBar("My AddIn")
'sink the event
Set Me.MenuHandler = VBInstance.Events.CommandBarEvents(mcbMenuCommandBar)
End If
If ConnectMode = ext_cm_AfterStartup Then
If GetSetting(App.Title, "Settings", "DisplayOnConnect", "0") = "1" Then
'set this to display the form on connect
Me.Show
End If
End If
Exit Sub
error_handler:
MsgBox Err.Description
End Sub
'------------------------------------------------------
'this method removes the Add-In from VB
'------------------------------------------------------
Private Sub AddinInstance_OnDisconnection(ByVal RemoveMode As AddInDesignerObjects.ext_DisconnectMode, custom() As Variant)
On Error Resume Next
'delete the command bar entry
mcbMenuCommandBar.Delete
'shut down the Add-In
If FormDisplayed Then
SaveSetting App.Title, "Settings", "DisplayOnConnect", "1"
FormDisplayed = False
Else
SaveSetting App.Title, "Settings", "DisplayOnConnect", "0"
End If
Unload mfrmAddIn
Set mfrmAddIn = Nothing
End Sub
Private Sub IDTExtensibility_OnStartupComplete(custom() As Variant)
If GetSetting(App.Title, "Settings", "DisplayOnConnect", "0") = "1" Then
'set this to display the form on connect
Me.Show
End If
End Sub
'this event fires when the menu is clicked in the IDE
Private Sub MenuHandler_Click(ByVal CommandBarControl As Object, handled As Boolean, CancelDefault As Boolean)
Me.Show
End Sub
Function AddToAddInCommandBar(sCaption As String) As Office.CommandBarControl
Dim cbMenuCommandBar As Office.CommandBarControl 'command bar object
Dim cbMenu As Object
On Error GoTo AddToAddInCommandBarErr
'see if we can find the Add-Ins menu
Set cbMenu = VBInstance.CommandBars("Add-Ins")
If cbMenu Is Nothing Then
'not available so we fail
Exit Function
End If
'add it to the command bar
Set cbMenuCommandBar = cbMenu.Controls.Add(1)
'set the caption
cbMenuCommandBar.Caption = sCaption
Set AddToAddInCommandBar = cbMenuCommandBar
Exit Function
AddToAddInCommandBarErr:
End Function
#8
'add a listbox named list1,two commandbutton named OKButton/CancelButton
'put the code into the frmAddIn
Option Explicit
Public VBInstance As VBIDE.VBE
Public Connect As Connect
Private Sub CancelButton_Click()
Connect.Hide
End Sub
Private Sub Form_Load()
Dim mCop As Object
'獲得當前啟動工程中的所有對象
For Each mCop In VBInstance.VBProjects.StartProject.VBComponents
'如果對象是窗体類型就將其添加到ListBox中
If mCop.Type = vbext_ct_VBForm Then
List1.AddItem mCop.Name
End If
Next
If List1.ListCount < 1 Then
MsgBox "工程中沒有添加控件的窗体"
Connect.Hide
Else
List1.ListIndex = 0
End If
End Sub
Private Sub OKButton_Click()
Dim xComp As VBComponent
Dim xModule As VBComponent
Dim xForm As VBForm
Dim xControl As VBControl
Dim xCode As CodeModule
MsgBox "AddIn operation on: " & VBInstance.FullName
'獲得用戶選擇的窗体對象
Set xComp = VBInstance.VBProjects.StartProject.VBComponents(List1.List(List1.ListIndex))
'獲得窗体設計器對象
Set xForm = xComp.Designer
'添加一個CommandButton到窗体上
Set xControl = xForm.VBControls.Add("VB.CommandButton")
'設定控件的名稱
xControl.Properties("Name") = "cmdButton"
'添加控件的Click事件代碼
xComp.CodeModule.CreateEventProc "Click", "cmdButton"
'添加一個新模塊到工程中
Set xModule = VBInstance.VBProjects.StartProject.VBComponents.Add(vbext_ct_StdModule)
'設定模塊名稱
xModule.Properties("Name") = "ModulTemp"
'獲得對象的代碼對象
Set xCode = xModule.CodeModule
Dim astr As String
'添加mClick子程序到新模當中
astr = "'******************************************************************************" & vbLf & _
"'Function: Test " & vbLf & _
"' " & vbLf & _
"'Desctiption: 執行cmdButton_Click事件 " & vbLf & _
"' " & vbLf & _
"'Parameters: None " & vbLf & _
"' " & vbLf & _
"'Return Value: None " & vbLf & _
"'============================================================================ " & vbLf & _
"' DATE NAME DESCRIPTION " & vbLf & _
"' ----------- ---------------- ----------------- " & vbLf & _
"' 29-04-2004 You Created " & vbLf & _
"'******************************************************************************" & vbLf
astr = astr & "Public Sub Test()" & Chr(13) & Chr(10) & _
Chr(vbKeyTab) & "MsgBox ""You click a button!""" & Chr(13) & Chr(10) & _
"End Sub"
xCode.AddFromString astr
Dim lCount As Long
'在cmdButton的Click事件中添加執行mClick子程序
lCount = xComp.CodeModule.ProcBodyLine("cmdButton_Click", vbext_pk_Proc)
If lCount <> 0 Then
xComp.CodeModule.InsertLines lCount + 1, "mClick"
End If
Me.Hide
End Sub
'put the code into the frmAddIn
Option Explicit
Public VBInstance As VBIDE.VBE
Public Connect As Connect
Private Sub CancelButton_Click()
Connect.Hide
End Sub
Private Sub Form_Load()
Dim mCop As Object
'獲得當前啟動工程中的所有對象
For Each mCop In VBInstance.VBProjects.StartProject.VBComponents
'如果對象是窗体類型就將其添加到ListBox中
If mCop.Type = vbext_ct_VBForm Then
List1.AddItem mCop.Name
End If
Next
If List1.ListCount < 1 Then
MsgBox "工程中沒有添加控件的窗体"
Connect.Hide
Else
List1.ListIndex = 0
End If
End Sub
Private Sub OKButton_Click()
Dim xComp As VBComponent
Dim xModule As VBComponent
Dim xForm As VBForm
Dim xControl As VBControl
Dim xCode As CodeModule
MsgBox "AddIn operation on: " & VBInstance.FullName
'獲得用戶選擇的窗体對象
Set xComp = VBInstance.VBProjects.StartProject.VBComponents(List1.List(List1.ListIndex))
'獲得窗体設計器對象
Set xForm = xComp.Designer
'添加一個CommandButton到窗体上
Set xControl = xForm.VBControls.Add("VB.CommandButton")
'設定控件的名稱
xControl.Properties("Name") = "cmdButton"
'添加控件的Click事件代碼
xComp.CodeModule.CreateEventProc "Click", "cmdButton"
'添加一個新模塊到工程中
Set xModule = VBInstance.VBProjects.StartProject.VBComponents.Add(vbext_ct_StdModule)
'設定模塊名稱
xModule.Properties("Name") = "ModulTemp"
'獲得對象的代碼對象
Set xCode = xModule.CodeModule
Dim astr As String
'添加mClick子程序到新模當中
astr = "'******************************************************************************" & vbLf & _
"'Function: Test " & vbLf & _
"' " & vbLf & _
"'Desctiption: 執行cmdButton_Click事件 " & vbLf & _
"' " & vbLf & _
"'Parameters: None " & vbLf & _
"' " & vbLf & _
"'Return Value: None " & vbLf & _
"'============================================================================ " & vbLf & _
"' DATE NAME DESCRIPTION " & vbLf & _
"' ----------- ---------------- ----------------- " & vbLf & _
"' 29-04-2004 You Created " & vbLf & _
"'******************************************************************************" & vbLf
astr = astr & "Public Sub Test()" & Chr(13) & Chr(10) & _
Chr(vbKeyTab) & "MsgBox ""You click a button!""" & Chr(13) & Chr(10) & _
"End Sub"
xCode.AddFromString astr
Dim lCount As Long
'在cmdButton的Click事件中添加執行mClick子程序
lCount = xComp.CodeModule.ProcBodyLine("cmdButton_Click", vbext_pk_Proc)
If lCount <> 0 Then
xComp.CodeModule.InsertLines lCount + 1, "mClick"
End If
Me.Hide
End Sub
#9
请看清我的要求,这段代码响应的是menu_click事件,我刚才已经说了,menu_click和增删工程这类事件我早就找到了,我要找的是双击控件和回车等事件。
怎么现在cdsn里全成了Ctrl C+Ctrl V大军了?
怎么现在cdsn里全成了Ctrl C+Ctrl V大军了?
#10
'將project保存為MyAddIn.vbp --> 運行 --> 再打開VB新增一個project -->
單擊Add-Ins裡的MyAddIn --> OK --> 確定 .
單擊Add-Ins裡的MyAddIn --> OK --> 確定 .
#11
ctrlC+CtrlV也没错,可是请先看清楚题目,顺便说一句,我在此提问前,已经在google上找了七八个相关样例,全没有能达到我所说功能的。基本都是直接响应对Add_in的工程里的事件,而不是通过对IDE的事件响应来激发事件。
#12
你的意思是雙擊Button?Sorry,那我也無能為力了......
#13
sorry,leolan(史留香) 刚才我口气不好。不过还是非常感谢你。
我希望达到的目标是每当IDE中产生新的Function或Sub时,能自动加上相关的说明和注释,这样光是靠Add_in本身的事件响应是无法做到的。实在不行的话我可能就只能在Add_in里采用timer循环,如果发现新的function或submit后再进行增加,但这样肯定对资源占用比较大,能有其他方案的话,我会尽量不这样做。
我希望达到的目标是每当IDE中产生新的Function或Sub时,能自动加上相关的说明和注释,这样光是靠Add_in本身的事件响应是无法做到的。实在不行的话我可能就只能在Add_in里采用timer循环,如果发现新的function或submit后再进行增加,但这样肯定对资源占用比较大,能有其他方案的话,我会尽量不这样做。
#14
你說得對!是我沒理解你的意思.
"我希望达到的目标是每当IDE中产生新的Function或Sub时,能自动加上相关的说明和注释"
That's great!!!!
if you succeed in this , pls share it with us,OK?
"我希望达到的目标是每当IDE中产生新的Function或Sub时,能自动加上相关的说明和注释"
That's great!!!!
if you succeed in this , pls share it with us,OK?
#15
还没人能帮助解答吗?
#16
我子类狂来说两句理论,呵呵
是否可以尝试获得当前活动代码窗口的句柄,然后进行子类处理
是否可以尝试获得当前活动代码窗口的句柄,然后进行子类处理
#17
难以理解,你们都是高手我只能download了。
#1
没有看懂
不过强烈关注
帮你顶一下~~~
不过强烈关注
帮你顶一下~~~
#2
关注
#3
Add_in?
当我们在vb的IDE环境下键入private sub text,后然回车时
IDE环境会自动把相关的字母转为大写,text后加上括号,并在这句话的后面加上End sub
当我们在VB的IDE环境下双击一个控件后,IDE环境会自动在Code界面以控件名为参数增加一个private Sub。。等等。
//你可以自己用RICHTEXT编写啊
当我们在vb的IDE环境下键入private sub text,后然回车时
IDE环境会自动把相关的字母转为大写,text后加上括号,并在这句话的后面加上End sub
当我们在VB的IDE环境下双击一个控件后,IDE环境会自动在Code界面以控件名为参数增加一个private Sub。。等等。
//你可以自己用RICHTEXT编写啊
#4
vb的Add_in功能是一种类似于word宏的功能,可以在Add_in中对整个VB环境进行操作,就我目前找到的功能来说,可以对VB菜单事件,打开新工程事件,关闭新工程事件等进行响应,并可以把VB环境当成一个对象进行操作,功能比较强大,大家可以在msdn中找到一个名叫taborder的用于更改VB控件tab属性的Add_in例子。
但我除了以上功能之外还想找到响应IDE环境中双击控件和回车的事件,在网上找了很久,没见到相应的例子,所以请求大家帮忙
但我除了以上功能之外还想找到响应IDE环境中双击控件和回车的事件,在网上找了很久,没见到相应的例子,所以请求大家帮忙
#5
re ryuginka(ryuginka)
我的目的是做一个Add_in 接管VBIDE环境的这种自动生成。
比如说在双击一个控件后,我要求除了自动增加private sub和end sub外,还能自动生成相应的注释。
我的目的是做一个Add_in 接管VBIDE环境的这种自动生成。
比如说在双击一个控件后,我要求除了自动增加private sub和end sub外,还能自动生成相应的注释。
#6
有没高手可以解答啊?分不够可以接着加
#7
'code in connect.Dsr
Option Explicit
Public FormDisplayed As Boolean
Public VBInstance As VBIDE.VBE
Dim mcbMenuCommandBar As Office.CommandBarControl
Dim mfrmAddIn As New frmAddIn
Public WithEvents MenuHandler As CommandBarEvents 'command bar event handler
Sub Hide()
On Error Resume Next
FormDisplayed = False
mfrmAddIn.Hide
End Sub
Sub Show()
On Error Resume Next
If mfrmAddIn Is Nothing Then
Set mfrmAddIn = New frmAddIn
End If
Set mfrmAddIn.VBInstance = VBInstance
Set mfrmAddIn.Connect = Me
FormDisplayed = True
mfrmAddIn.Show
End Sub
'------------------------------------------------------
'this method adds the Add-In to VB
'------------------------------------------------------
Private Sub AddinInstance_OnConnection(ByVal Application As Object, ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, ByVal AddInInst As Object, custom() As Variant)
On Error GoTo error_handler
'save the vb instance
Set VBInstance = Application
'this is a good place to set a breakpoint and
'test various addin objects, properties and methods
Debug.Print VBInstance.FullName
If ConnectMode = ext_cm_External Then
'Used by the wizard toolbar to start this wizard
Me.Show
Else
Set mcbMenuCommandBar = AddToAddInCommandBar("My AddIn")
'sink the event
Set Me.MenuHandler = VBInstance.Events.CommandBarEvents(mcbMenuCommandBar)
End If
If ConnectMode = ext_cm_AfterStartup Then
If GetSetting(App.Title, "Settings", "DisplayOnConnect", "0") = "1" Then
'set this to display the form on connect
Me.Show
End If
End If
Exit Sub
error_handler:
MsgBox Err.Description
End Sub
'------------------------------------------------------
'this method removes the Add-In from VB
'------------------------------------------------------
Private Sub AddinInstance_OnDisconnection(ByVal RemoveMode As AddInDesignerObjects.ext_DisconnectMode, custom() As Variant)
On Error Resume Next
'delete the command bar entry
mcbMenuCommandBar.Delete
'shut down the Add-In
If FormDisplayed Then
SaveSetting App.Title, "Settings", "DisplayOnConnect", "1"
FormDisplayed = False
Else
SaveSetting App.Title, "Settings", "DisplayOnConnect", "0"
End If
Unload mfrmAddIn
Set mfrmAddIn = Nothing
End Sub
Private Sub IDTExtensibility_OnStartupComplete(custom() As Variant)
If GetSetting(App.Title, "Settings", "DisplayOnConnect", "0") = "1" Then
'set this to display the form on connect
Me.Show
End If
End Sub
'this event fires when the menu is clicked in the IDE
Private Sub MenuHandler_Click(ByVal CommandBarControl As Object, handled As Boolean, CancelDefault As Boolean)
Me.Show
End Sub
Function AddToAddInCommandBar(sCaption As String) As Office.CommandBarControl
Dim cbMenuCommandBar As Office.CommandBarControl 'command bar object
Dim cbMenu As Object
On Error GoTo AddToAddInCommandBarErr
'see if we can find the Add-Ins menu
Set cbMenu = VBInstance.CommandBars("Add-Ins")
If cbMenu Is Nothing Then
'not available so we fail
Exit Function
End If
'add it to the command bar
Set cbMenuCommandBar = cbMenu.Controls.Add(1)
'set the caption
cbMenuCommandBar.Caption = sCaption
Set AddToAddInCommandBar = cbMenuCommandBar
Exit Function
AddToAddInCommandBarErr:
End Function
Option Explicit
Public FormDisplayed As Boolean
Public VBInstance As VBIDE.VBE
Dim mcbMenuCommandBar As Office.CommandBarControl
Dim mfrmAddIn As New frmAddIn
Public WithEvents MenuHandler As CommandBarEvents 'command bar event handler
Sub Hide()
On Error Resume Next
FormDisplayed = False
mfrmAddIn.Hide
End Sub
Sub Show()
On Error Resume Next
If mfrmAddIn Is Nothing Then
Set mfrmAddIn = New frmAddIn
End If
Set mfrmAddIn.VBInstance = VBInstance
Set mfrmAddIn.Connect = Me
FormDisplayed = True
mfrmAddIn.Show
End Sub
'------------------------------------------------------
'this method adds the Add-In to VB
'------------------------------------------------------
Private Sub AddinInstance_OnConnection(ByVal Application As Object, ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, ByVal AddInInst As Object, custom() As Variant)
On Error GoTo error_handler
'save the vb instance
Set VBInstance = Application
'this is a good place to set a breakpoint and
'test various addin objects, properties and methods
Debug.Print VBInstance.FullName
If ConnectMode = ext_cm_External Then
'Used by the wizard toolbar to start this wizard
Me.Show
Else
Set mcbMenuCommandBar = AddToAddInCommandBar("My AddIn")
'sink the event
Set Me.MenuHandler = VBInstance.Events.CommandBarEvents(mcbMenuCommandBar)
End If
If ConnectMode = ext_cm_AfterStartup Then
If GetSetting(App.Title, "Settings", "DisplayOnConnect", "0") = "1" Then
'set this to display the form on connect
Me.Show
End If
End If
Exit Sub
error_handler:
MsgBox Err.Description
End Sub
'------------------------------------------------------
'this method removes the Add-In from VB
'------------------------------------------------------
Private Sub AddinInstance_OnDisconnection(ByVal RemoveMode As AddInDesignerObjects.ext_DisconnectMode, custom() As Variant)
On Error Resume Next
'delete the command bar entry
mcbMenuCommandBar.Delete
'shut down the Add-In
If FormDisplayed Then
SaveSetting App.Title, "Settings", "DisplayOnConnect", "1"
FormDisplayed = False
Else
SaveSetting App.Title, "Settings", "DisplayOnConnect", "0"
End If
Unload mfrmAddIn
Set mfrmAddIn = Nothing
End Sub
Private Sub IDTExtensibility_OnStartupComplete(custom() As Variant)
If GetSetting(App.Title, "Settings", "DisplayOnConnect", "0") = "1" Then
'set this to display the form on connect
Me.Show
End If
End Sub
'this event fires when the menu is clicked in the IDE
Private Sub MenuHandler_Click(ByVal CommandBarControl As Object, handled As Boolean, CancelDefault As Boolean)
Me.Show
End Sub
Function AddToAddInCommandBar(sCaption As String) As Office.CommandBarControl
Dim cbMenuCommandBar As Office.CommandBarControl 'command bar object
Dim cbMenu As Object
On Error GoTo AddToAddInCommandBarErr
'see if we can find the Add-Ins menu
Set cbMenu = VBInstance.CommandBars("Add-Ins")
If cbMenu Is Nothing Then
'not available so we fail
Exit Function
End If
'add it to the command bar
Set cbMenuCommandBar = cbMenu.Controls.Add(1)
'set the caption
cbMenuCommandBar.Caption = sCaption
Set AddToAddInCommandBar = cbMenuCommandBar
Exit Function
AddToAddInCommandBarErr:
End Function
#8
'add a listbox named list1,two commandbutton named OKButton/CancelButton
'put the code into the frmAddIn
Option Explicit
Public VBInstance As VBIDE.VBE
Public Connect As Connect
Private Sub CancelButton_Click()
Connect.Hide
End Sub
Private Sub Form_Load()
Dim mCop As Object
'獲得當前啟動工程中的所有對象
For Each mCop In VBInstance.VBProjects.StartProject.VBComponents
'如果對象是窗体類型就將其添加到ListBox中
If mCop.Type = vbext_ct_VBForm Then
List1.AddItem mCop.Name
End If
Next
If List1.ListCount < 1 Then
MsgBox "工程中沒有添加控件的窗体"
Connect.Hide
Else
List1.ListIndex = 0
End If
End Sub
Private Sub OKButton_Click()
Dim xComp As VBComponent
Dim xModule As VBComponent
Dim xForm As VBForm
Dim xControl As VBControl
Dim xCode As CodeModule
MsgBox "AddIn operation on: " & VBInstance.FullName
'獲得用戶選擇的窗体對象
Set xComp = VBInstance.VBProjects.StartProject.VBComponents(List1.List(List1.ListIndex))
'獲得窗体設計器對象
Set xForm = xComp.Designer
'添加一個CommandButton到窗体上
Set xControl = xForm.VBControls.Add("VB.CommandButton")
'設定控件的名稱
xControl.Properties("Name") = "cmdButton"
'添加控件的Click事件代碼
xComp.CodeModule.CreateEventProc "Click", "cmdButton"
'添加一個新模塊到工程中
Set xModule = VBInstance.VBProjects.StartProject.VBComponents.Add(vbext_ct_StdModule)
'設定模塊名稱
xModule.Properties("Name") = "ModulTemp"
'獲得對象的代碼對象
Set xCode = xModule.CodeModule
Dim astr As String
'添加mClick子程序到新模當中
astr = "'******************************************************************************" & vbLf & _
"'Function: Test " & vbLf & _
"' " & vbLf & _
"'Desctiption: 執行cmdButton_Click事件 " & vbLf & _
"' " & vbLf & _
"'Parameters: None " & vbLf & _
"' " & vbLf & _
"'Return Value: None " & vbLf & _
"'============================================================================ " & vbLf & _
"' DATE NAME DESCRIPTION " & vbLf & _
"' ----------- ---------------- ----------------- " & vbLf & _
"' 29-04-2004 You Created " & vbLf & _
"'******************************************************************************" & vbLf
astr = astr & "Public Sub Test()" & Chr(13) & Chr(10) & _
Chr(vbKeyTab) & "MsgBox ""You click a button!""" & Chr(13) & Chr(10) & _
"End Sub"
xCode.AddFromString astr
Dim lCount As Long
'在cmdButton的Click事件中添加執行mClick子程序
lCount = xComp.CodeModule.ProcBodyLine("cmdButton_Click", vbext_pk_Proc)
If lCount <> 0 Then
xComp.CodeModule.InsertLines lCount + 1, "mClick"
End If
Me.Hide
End Sub
'put the code into the frmAddIn
Option Explicit
Public VBInstance As VBIDE.VBE
Public Connect As Connect
Private Sub CancelButton_Click()
Connect.Hide
End Sub
Private Sub Form_Load()
Dim mCop As Object
'獲得當前啟動工程中的所有對象
For Each mCop In VBInstance.VBProjects.StartProject.VBComponents
'如果對象是窗体類型就將其添加到ListBox中
If mCop.Type = vbext_ct_VBForm Then
List1.AddItem mCop.Name
End If
Next
If List1.ListCount < 1 Then
MsgBox "工程中沒有添加控件的窗体"
Connect.Hide
Else
List1.ListIndex = 0
End If
End Sub
Private Sub OKButton_Click()
Dim xComp As VBComponent
Dim xModule As VBComponent
Dim xForm As VBForm
Dim xControl As VBControl
Dim xCode As CodeModule
MsgBox "AddIn operation on: " & VBInstance.FullName
'獲得用戶選擇的窗体對象
Set xComp = VBInstance.VBProjects.StartProject.VBComponents(List1.List(List1.ListIndex))
'獲得窗体設計器對象
Set xForm = xComp.Designer
'添加一個CommandButton到窗体上
Set xControl = xForm.VBControls.Add("VB.CommandButton")
'設定控件的名稱
xControl.Properties("Name") = "cmdButton"
'添加控件的Click事件代碼
xComp.CodeModule.CreateEventProc "Click", "cmdButton"
'添加一個新模塊到工程中
Set xModule = VBInstance.VBProjects.StartProject.VBComponents.Add(vbext_ct_StdModule)
'設定模塊名稱
xModule.Properties("Name") = "ModulTemp"
'獲得對象的代碼對象
Set xCode = xModule.CodeModule
Dim astr As String
'添加mClick子程序到新模當中
astr = "'******************************************************************************" & vbLf & _
"'Function: Test " & vbLf & _
"' " & vbLf & _
"'Desctiption: 執行cmdButton_Click事件 " & vbLf & _
"' " & vbLf & _
"'Parameters: None " & vbLf & _
"' " & vbLf & _
"'Return Value: None " & vbLf & _
"'============================================================================ " & vbLf & _
"' DATE NAME DESCRIPTION " & vbLf & _
"' ----------- ---------------- ----------------- " & vbLf & _
"' 29-04-2004 You Created " & vbLf & _
"'******************************************************************************" & vbLf
astr = astr & "Public Sub Test()" & Chr(13) & Chr(10) & _
Chr(vbKeyTab) & "MsgBox ""You click a button!""" & Chr(13) & Chr(10) & _
"End Sub"
xCode.AddFromString astr
Dim lCount As Long
'在cmdButton的Click事件中添加執行mClick子程序
lCount = xComp.CodeModule.ProcBodyLine("cmdButton_Click", vbext_pk_Proc)
If lCount <> 0 Then
xComp.CodeModule.InsertLines lCount + 1, "mClick"
End If
Me.Hide
End Sub
#9
请看清我的要求,这段代码响应的是menu_click事件,我刚才已经说了,menu_click和增删工程这类事件我早就找到了,我要找的是双击控件和回车等事件。
怎么现在cdsn里全成了Ctrl C+Ctrl V大军了?
怎么现在cdsn里全成了Ctrl C+Ctrl V大军了?
#10
'將project保存為MyAddIn.vbp --> 運行 --> 再打開VB新增一個project -->
單擊Add-Ins裡的MyAddIn --> OK --> 確定 .
單擊Add-Ins裡的MyAddIn --> OK --> 確定 .
#11
ctrlC+CtrlV也没错,可是请先看清楚题目,顺便说一句,我在此提问前,已经在google上找了七八个相关样例,全没有能达到我所说功能的。基本都是直接响应对Add_in的工程里的事件,而不是通过对IDE的事件响应来激发事件。
#12
你的意思是雙擊Button?Sorry,那我也無能為力了......
#13
sorry,leolan(史留香) 刚才我口气不好。不过还是非常感谢你。
我希望达到的目标是每当IDE中产生新的Function或Sub时,能自动加上相关的说明和注释,这样光是靠Add_in本身的事件响应是无法做到的。实在不行的话我可能就只能在Add_in里采用timer循环,如果发现新的function或submit后再进行增加,但这样肯定对资源占用比较大,能有其他方案的话,我会尽量不这样做。
我希望达到的目标是每当IDE中产生新的Function或Sub时,能自动加上相关的说明和注释,这样光是靠Add_in本身的事件响应是无法做到的。实在不行的话我可能就只能在Add_in里采用timer循环,如果发现新的function或submit后再进行增加,但这样肯定对资源占用比较大,能有其他方案的话,我会尽量不这样做。
#14
你說得對!是我沒理解你的意思.
"我希望达到的目标是每当IDE中产生新的Function或Sub时,能自动加上相关的说明和注释"
That's great!!!!
if you succeed in this , pls share it with us,OK?
"我希望达到的目标是每当IDE中产生新的Function或Sub时,能自动加上相关的说明和注释"
That's great!!!!
if you succeed in this , pls share it with us,OK?
#15
还没人能帮助解答吗?
#16
我子类狂来说两句理论,呵呵
是否可以尝试获得当前活动代码窗口的句柄,然后进行子类处理
是否可以尝试获得当前活动代码窗口的句柄,然后进行子类处理
#17
难以理解,你们都是高手我只能download了。