Page 1 of 1

VBE GetSelection Error: this lin does not contain a recognized action

Posted: 13 Oct 2021, 08:45
by cgx5871
oExcel:=Excel_Get()
oExcel.Application.VBE.ActiveCodePane.CodeModule.CodePane.GetSelection nStartLine, nStartCol, nEndLine, nEndCol

Re: VBE GetSelection Error: this lin does not contain a recognized action

Posted: 13 Oct 2021, 10:33
by mikeyww

Code: Select all

XL := ComObjActive("Excel.application")
F3::
startRow := XL.Selection.Row 
startCol := XL.Selection.Column
selRows  := XL.Selection.Rows.Count
selCols  := XL.Selection.Columns.Count
MsgBox, 64, Selection, % "Start (col,row) = (" startCol "," startRow ")`n"
                       . "Number of cols = " selCols "`nNumber of rows = " selRows
Return

Re: VBE GetSelection Error: this lin does not contain a recognized action

Posted: 13 Oct 2021, 10:56
by cgx5871
mikeyww wrote:
13 Oct 2021, 10:33

Code: Select all

XL := ComObjActive("Excel.application")
F3::
startRow := XL.Selection.Row 
startCol := XL.Selection.Column
selRows  := XL.Selection.Rows.Count
selCols  := XL.Selection.Columns.Count
MsgBox, 64, Selection, % "Start (col,row) = (" startCol "," startRow ")`n"
                       . "Number of cols = " selCols "`nNumber of rows = " selRows
Return

VBE, NOT EXCEL CALSS

Re: VBE GetSelection Error: this lin does not contain a recognized action

Posted: 13 Oct 2021, 11:27
by mikeyww
Oops, sorry about that! I figured that you were trying to work with a spreadsheet. I do not have the right knowledge for you, but others here will.

Re: VBE GetSelection Error: this lin does not contain a recognized action

Posted: 13 Oct 2021, 12:00
by flyingDman
@cgx5871 May be you can explain what you want to do. Do you want to get the position (x,y,w,h) of the currently selected portion of a macro in the codepane, or do you want to get the text of the macro itself?

This will get you the full text of the macro(s):

Code: Select all

xl := ComObjActive("Excel.application")
for x in xl.VBE.ActiveVBProject.VBComponents
	if (x.name = "Module1")
		msgbox % x.codemodule.lines(1,x.codemodule.countoflines)

Re: VBE GetSelection Error: this lin does not contain a recognized action

Posted: 14 Oct 2021, 14:16
by cgx5871
flyingDman wrote:
13 Oct 2021, 12:00
@cgx5871 May be you can explain what you want to do. Do you want to get the position (x,y,w,h) of the currently selected portion of a macro in the codepane, or do you want to get the text of the macro itself?

This will get you the full text of the macro(s):

Code: Select all

xl := ComObjActive("Excel.application")
for x in xl.VBE.ActiveVBProject.VBComponents
	if (x.name = "Module1")
		msgbox % x.codemodule.lines(1,x.codemodule.countoflines)
thanks
how can i get TypeName(Selection)
Excel_Get(). TypeName(Selection) not work
WheelDown::
oExcel:=Excel_Get()
if (oExcel.Selection.Row=11)
oExcel.run ****
--------if Selection is Picture, run error

Re: VBE GetSelection Error: this lin does not contain a recognized action

Posted: 15 Oct 2021, 01:50
by cgx5871
i want to get the position (x,y,w,h) of the currently selected
then clone it
flyingDman wrote:
13 Oct 2021, 12:00
@cgx5871 May be you can explain what you want to do. Do you want to get the position (x,y,w,h) of the currently selected portion of a macro in the codepane, or do you want to get the text of the macro itself?

This will get you the full text of the macro(s):

Code: Select all

xl := ComObjActive("Excel.application")
for x in xl.VBE.ActiveVBProject.VBComponents
	if (x.name = "Module1")
		msgbox % x.codemodule.lines(1,x.codemodule.countoflines)

Re: VBE GetSelection Error: this lin does not contain a recognized action

Posted: 15 Oct 2021, 09:52
by flyingDman
cgx5871 wrote:
15 Oct 2021, 01:50
...currently selected... then clone it ...
What is selected? Clone what? Is this still related to VBE GetSelection.
You'll need to provide a detailed explanation of what you want to accomplish. If English is not your native language, use Google translator.

Re: VBE GetSelection Error: this lin does not contain a recognized action

Posted: 17 Oct 2021, 05:09
by cgx5871
flyingDman wrote:
15 Oct 2021, 09:52
cgx5871 wrote:
15 Oct 2021, 01:50
...currently selected... then clone it ...
What is selected? Clone what? Is this still related to VBE GetSelection.
You'll need to provide a detailed explanation of what you want to accomplish. If English is not your native language, use Google translator.
I want to get the x,y,w,h of the selected position.
The purpose is:
Ctrl+d, clone the current selection.
Ctrl+t, switch between up and down
Ctrl+q, turn to comment
`Automatic completion if then endif, for next and the like
These all need to get the current selected position.
I have also tried the following various commands.
None of it seems to work.
______________________________________

editor:LineFromPosition(editor.SelectionStart)
editor:LineFromPosition(editor.SelectionEnd)
------------------------------------------
SENDMESSAGE
EM_EXGETSEL EM_EXSETSEL
---------------------------------------------------

Code: Select all

ControlGet, hVBE, Hwnd,, MDIClient1, ahk_class wndclass_desked_gsk
hVBE:=Excel_Get()
CurPos:=DllCall("User32.dll\SendMessage", "Ptr", hVBE, "UInt", 0x7D8,"UInt")
WordPosL:=DllCall("User32.dll\SendMessage", "Ptr", hVBE, "UInt", 0x8DA, "UInt", CurPos, "UInt", 1, "UInt")
WordPosR:=DllCall("User32.dll\SendMessage", "Ptr", hVBE, "UInt", 0x8DB, "UInt", CurPos, "UInt", 1, "UInt")
DllCall("User32.dll\SendMessage", "Ptr", hVBE, "UInt", 0x00B1, "Ptr",  WordPosR , "Ptr",WordPosL , "Ptr")

Re: VBE GetSelection Error: this lin does not contain a recognized action

Posted: 23 Oct 2021, 06:23
by cgx5871
anyone help me?

Re: VBE GetSelection Error: this lin does not contain a recognized action

Posted: 23 Oct 2021, 14:44
by miracle
The purpose is:
Ctrl+d, clone the current selection.
Ctrl+t, switch between up and down
Ctrl+q, turn to comment
`Automatic completion if then endif, for next and the like
how will you implement this actions? I don't see how that would be possible with vba/ahk.

Re: VBE GetSelection Error: this lin does not contain a recognized action

Posted: 24 Oct 2021, 02:37
by cgx5871

Code: Select all

Sub AHK_VBE_Ctrl_d(Optional ByVal rngFix As String _
			, Optional ByVal CloneType As Integer = -1)
    Dim n As Long, tmp As String
    Dim nStartLine As Long
    Dim nEndLine   As Long
    Dim nStartCol  As Long
    Dim nEndCol    As Long
    Dim oLine      As String
    Dim sContent   As String

    With Application.VBE.ActiveCodePane.CodeModule
        .CodePane.GetSelection nStartLine, nStartCol, nEndLine, nEndCol
        If nStartLine = nEndLine And nStartCol = nEndCol Then
            sContent = .Lines(nStartLine, 1)
        Else
            For n = nStartLine To nEndLine
                tmp = .Lines(n, 1)
                If n = nEndLine Then tmp = Left(tmp, nEndCol - 1)
                If n = nStartLine Then tmp = Right(tmp, (Len(tmp) - nStartCol) + 1)
                sContent = sContent & IIf(Len(sContent) > 0, Chr(10), "") & _
                    tmp
            Next n
        End If
        .InsertLines nStartLine + 1, sContent
        .CodePane.SetSelection nStartLine, nStartCol, nEndLine, nEndCol
    End With
End Sub

Re: VBE GetSelection Error: this lin does not contain a recognized action

Posted: 24 Oct 2021, 23:23
by miracle
Optional ByVal rngFix As String _
, Optional ByVal CloneType As Integer = -1
thank you!!! To be honest i didn't truly understand your code. But both args aren't used. What does rngFix stand for??

I guess you are still waiting for the last piece to convert all vba to AHK. Or did you mange to port a part already?

Anyway if you don't mind i will contribute a little piece to your great code. The last part is what you need. I found it in this wonderfull forum. I don't remember where i got it from, so i can't credit it.

I am still using access long day. I made a little tweak for the vba editor if the autocomplete is visible i can move up or down in the list like vim (^j and ^k). Do you have any more tweaks vor vba?

Code: Select all

fn_access_code_test() {
	db_object := fn_access_get_active_object()
	
	if(db_object = "")
	{
		msgbox, db_object leer
		return
	}

	mdl_name := fn_vba_get_module_name()

	if(mdl_name = "")
	{
		fn_debug_print("mdl_name ist leer", true)
		return
	}

	vb_project_object := db_object.VBE.ActiveVBProject
	vb_comp_object := vb_project_object.vbcomponents(mdl_name)
	code_mod_object := vb_comp_object.CodeModule

	msgbox, % "count of lines: " . code_mod_object.countoflines

	vbe_object := vb_project_object.vbe
	active_code_pane := vbe_object.activecodepane

	msgbox, % active_code_pane.countofvisiblelines

	type := (VT_BYREF := 0x4000) | (VT_I4 := 0x3)
	VarSetCapacity(buff, 16, 0)
	params := []

	Loop 4
		params.Push(ComObject(type, &buff + (A_Index - 1) *4))

	active_code_pane.GetSelection(params*)
		
	for k, v in ["startline", "startcolumn", "endline", "endcolumn"]
		%v% := NumGet(buff, 4*(k - 1), "UInt")

	MsgBox, % startline . "`n" . startcolumn . "`n" . endline . "`n" . endcolumn ;
}

Re: VBE GetSelection Error: this lin does not contain a recognized action

Posted: 25 Oct 2021, 11:13
by flyingDman
Good find @miracle! :bravo: . I still can not find the post where you found this. What you provided is still not complete, but I now think the OP can fill in the blanks. The important part is how does one retrieve the highlighted text in a macro? In VBA .GetSelection passes on x,y,w,h to variables ("startline", "startcolumn", "endline", "endcolumn"). In AHK I knew some kind of array would be involved and expected a safearray. But it's different kind of array.
Here is how you would retrieve highlighted text in a macro:

Code: Select all

xl := ComObjActive("Excel.application")
mdl_nm := xl.VBE.ActiveCodePane.CodeModule.Name
type := (VT_BYREF := 0x4000) | (VT_I4 := 0x3)
VarSetCapacity(buff, 16, 0)
params := []
Loop 4
	params.Push(ComObject(type, &buff + (A_Index - 1) *4))
xl.VBE.ActiveCodePane.CodeModule.CodePane.GetSelection(params*)
for k, v in ["startline", "startcolumn", "endline", "endcolumn"]
	%v% := NumGet(buff, 4*(k - 1), "UInt")
for x in xl.VBE.ActiveVBProject.VBComponents
	if (x.name = mdl_nm)
		msgbox % x.codemodule.lines(startline, endline - startline + 1)
The big question is: why would you want to do this? What is benefit of all this. The OP mentioned to insert this copied text back in the macro. Why? Why tinker with Macros when you can do it all with AHK?
BTW before running this code you need to tell Excel to allow external access to modules. Go to the Excel Trust Center and check "Trust access to the VBA project object model".

Re: VBE GetSelection Error: this lin does not contain a recognized action

Posted: 25 Oct 2021, 16:04
by miracle
Good find @miracle! :bravo: .
Thanks!! But ever if i see an vba question in this ahk forum i think immediately of @flyingDman and can't wait for your answers. ;)
I still can not find the post where you found this.
got it here: viewtopic.php?t=76934&p=334068
and of course its from @teadrinker :)
Guess google search is still the best, with the exact same query i didn't find it at bing , only on google.
What you provided is still not complete, but I now think the OP can fill in the blanks.
My version should work, but of course only the lower source part from teadrinker. I am evaluating the active access vba window when working with vba. It doesn't mind which access db i have oben or in which module i am, so i don't need hard coded module or db names.
In AHK I knew some kind of array would be involved and expected a safearray. But it's different kind of array.
Do you know what kind of array it is? The name i mean, so it can be looked up in msdn.
The big question is: why would you want to do this? What is benefit of all this. The OP mentioned to insert this copied text back in the macro. Why? Why tinker with Macros when you can do it all with AHK?
To be honest, i didn't ask me this question since i guess i am using ms access with it and saw only this stone. In ms access you need own vba code to run it. If i rethink it, you are right. I never used vba in the other programs (excel/word/outlook) ever since having found AHK. But in ms access vba is necessary. I might be wrong. But to add "features" to the vba code editor im ms access where i am regulary or control it externally i use only ahk. The regulary job is done with vba. Guess vba is in the other office apps obsolet :D

But anyway it would be interessting to know what the indention of the OP is. I also would like to know what else he used the VBE for, for getting new ideas. Hopefully we will get a reply, since many don't respond anymore after getting a solution for their problem. BTW: Mine is still open, maybe you know now better and can give me a hand (viewtopic.php?f=76&t=71930&p=311273#p311273)

Re: VBE GetSelection Error: this lin does not contain a recognized action

Posted: 25 Jan 2022, 09:09
by cgx5871
flyingDman wrote:
13 Oct 2021, 12:00
@cgx5871 May be you can explain what you want to do. Do you want to get the position (x,y,w,h) of the currently selected portion of a macro in the codepane, or do you want to get the text of the macro itself?

This will get you the full text of the macro(s):

Code: Select all

xl := ComObjActive("Excel.application")
for x in xl.VBE.ActiveVBProject.VBComponents
	if (x.name = "Module1")
		msgbox % x.codemodule.lines(1,x.codemodule.countoflines)

Code: Select all

xl := ComObjActive("Excel.application")
for x in xl.VBE.ActiveVBProject.VBComponents
	if (x.name = "Module1")
		msgbox % x.codemodule.ProcCountLines("test","vbext_pk_Proc")
		;~ msgbox % x.codemodule.ProcCountLines("test",vbext_pk_Proc)
why not work?