如何获取VBE中的, 鼠标选中文本?

遇到了问题?请先进行搜索(中文和英文),然后在此提问

Moderators: tmplinshi, arcticir

cgx5871
Posts: 315
Joined: 26 Jul 2018, 14:02

如何获取VBE中的, 鼠标选中文本?

26 Jul 2018, 14:12

看了半天Comvar()
还是没搞懂.
有两个需求:
1.我想获取VBA编辑器中, 鼠标选中的代码内容
2. 清空 "立即窗口"
求指导.
aamii
Posts: 47
Joined: 23 May 2014, 03:50

Re: 如何获取VBE中的, 鼠标选中文本?

06 Aug 2018, 00:04

第一个问题:
^c
第二个问题:
F9::
ControlFocus ,VbaWindow3,ahk_class wndclass_desked_gsk
ControlSend ,VbaWindow3,{CtrlDown}a{CtrlUp}{Delete},ahk_class wndclass_desked_gsk
return
cgx5871
Posts: 315
Joined: 26 Jul 2018, 14:02

Re: 如何获取VBE中的, 鼠标选中文本?

06 Aug 2018, 09:10

aamii wrote:第一个问题:
^c
第二个问题:
F9::
ControlFocus ,VbaWindow3,ahk_class wndclass_desked_gsk
ControlSend ,VbaWindow3,{CtrlDown}a{CtrlUp}{Delete},ahk_class wndclass_desked_gsk
return
这两个的方法都太土了.
还容易误操作.
找到一个曲线救国的.
VBcode=
(
Sub myFunction()
Worksheets(1).Range("C1").Value = 99
End Sub
)

Excel_Run("myFunction", VBcode) ; Changed: pass VBA code text to the function
return

Excel_Run(sFunction, VBcode){
;~ FilePath := C:\Users\KostasK\Desktop\Plano.xlsx
FilePath := A_ScriptDir "\Plano.xlsx" ; Changed for testing
oExcel := ComObjCreate("Excel.Application")
oExcel.Visible := true ; Changed for testing
oWorkbook := oExcel.Workbooks.Open(FilePath)

; We already have a reference to the Excel Application and Workbook objects. Don't need Excel_Get
Excel_ImportCode(VBcode, oWorkbook) ; Changed: Pass the workbook to use to Excel_ImportCode
oExcel.Run(sFunction) ; Changed

;~ oWorkbook.Save ; Changed for testing
;~ oExcel.Quit ; Changed for testing
}

Excel_ImportCode(VBcode, WorkbookObj){
if fileexist(A_ScriptDir . "\tempvbcode.txt")
FileDelete, %A_ScriptDir%\tempvbcode.txt
FileAppend, %VBcode%, %A_ScriptDir%\tempvbcode.txt
WorkbookObj.VBProject.VBComponents.Import(A_ScriptDir . "\tempvbcode.txt")
}
cgx5871
Posts: 315
Joined: 26 Jul 2018, 14:02

Re: 如何获取VBE中的, 鼠标选中文本?

06 Aug 2018, 09:11

Sub ClearImmediate()
Debug.Print VBA.String(200, vbNewLine)
'For a = 1 To 200
'Debug.Print ""
'Next
End Sub

oExcel.Run(oExcel.ActiveWorkbook.Name . "!ClearImmediate")
aamii
Posts: 47
Joined: 23 May 2014, 03:50

Re: 如何获取VBE中的, 鼠标选中文本?

06 Aug 2018, 18:50

建议你插入代码的时候
使用

Code: Select all

Example
aamii
Posts: 47
Joined: 23 May 2014, 03:50

Re: 如何获取VBE中的, 鼠标选中文本?

06 Aug 2018, 22:02

aamii wrote:第一个问题:
^c
第二个问题:
F9::
ControlFocus ,VbaWindow3,ahk_class wndclass_desked_gsk
ControlSend ,VbaWindow3,{CtrlDown}a{CtrlUp}{Delete},ahk_class wndclass_desked_gsk
return
第二个问题是可行的,实际上,你还可以借助vba自身的sendkeys

Code: Select all

#IfWinActive Microsoft Visual Basic for Applications
F9::excel_get().Application.SendKeys( "^g ^a {BackSpace}")
#IfWinActive

Return to “请求帮助”

Who is online

Users browsing this forum: No registered users and 18 guests