SetFocus in VBComponent - SOLVED

kpark91
StarLounger
Posts: 61
Joined: 29 Jul 2010, 14:52

SetFocus in VBComponent - SOLVED

Post by kpark91 »

Hi, I'm trying to develop a program which looks for words in Code Module1
and set focuses on the found word.

This is a snippet of code in finding the code

Code: Select all

Found = ActiveWorkbook.VBProject.VBComponents("Module1").CodeModule.Find(Target:=FindWhat, StartLine:=SL, StartColumn:=SC, _
                EndLine:=EL, EndColumn:=EC, _
                wholeword:=True, MatchCase:=False, patternsearch:=False)
where,
Dim FindWhat As String
Dim SL As Long ' start line
Dim EL As Long ' end line
Dim SC As Long ' start column
Dim EC As Long ' end column
Dim Found As Boolean

I know the code works well but the problem is...
Once I run the code, it doesn't show where the word was found.

Hence, I am hoping to use SetFocus function to focus onto the word.

If there's any other way than SetFocus function, please tell me :D
and don't be afraid to make suggestions, ideas, or just comments!!

Thanks in Advance
Last edited by kpark91 on 29 Jul 2010, 19:09, edited 1 time in total.
I don't have one

kpark91
StarLounger
Posts: 61
Joined: 29 Jul 2010, 14:52

Re: SetFocus in VBComponent

Post by kpark91 »

All I've gotten so far is

Code: Select all

Application.VBE.MainWindow.SetFocus
but it only shows the opened code's window and does not focus on specific words.
I don't have one

User avatar
HansV
Administrator
Posts: 78417
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: SetFocus in VBComponent

Post by HansV »

Welcome to Eileen's Lounge!

SetFocus can be used to activate a code window, not to select specific text within that window. Use the SetSelection method of the CodePane object for that. The Find method changes the values of the variables passed in the StartLine, StartColumn, EndLine and EndColumn arguments to the values for the found text, so that you can use these variables in the SetSelection method:

Code: Select all

With ActiveWorkbook.VBProject.VBComponents("Module1").CodeModule
  Found = .Find(Target:=FindWhat, StartLine:=SL, StartColumn:=SC, _
    EndLine:=EL, EndColumn:=EC, _
    WholeWord:=True, MatchCase:=False, PatternSearch:=False)
  If Found Then
    .CodePane.SetSelection SL, SC, EL, EC
  End If
End With
Best wishes,
Hans

kpark91
StarLounger
Posts: 61
Joined: 29 Jul 2010, 14:52

Re: SetFocus in VBComponent

Post by kpark91 »

OMG!!!! I love this forum!!!
They actually reply and give the answer you want exactly!!! unlike some forums (ex. MrExcel0

Thank you so much HansV!!!!!
I will definitely get this forum better known within my power :D
I don't have one

User avatar
HansV
Administrator
Posts: 78417
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: SetFocus in VBComponent

Post by HansV »

Thank *you* !
Best wishes,
Hans

kpark91
StarLounger
Posts: 61
Joined: 29 Jul 2010, 14:52

Re: SetFocus in VBComponent

Post by kpark91 »

Another Quick Question (sorry xD)

How do I SetFocus on a certain module?

For example, Module1????
I don't have one

User avatar
HansV
Administrator
Posts: 78417
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: SetFocus in VBComponent

Post by HansV »

You can use the Activate method for this:

ActiveWorkbook.VBProject.VBComponents("Module1").Activate
Best wishes,
Hans

kpark91
StarLounger
Posts: 61
Joined: 29 Jul 2010, 14:52

Re: SetFocus in VBComponent

Post by kpark91 »

Awesome! :D

Thank you again, HansV!

It must be hard trying to answer questions all by yourself in this forum!!
Once I get this done, hopefully, I can help out with simple questions O_o
I don't have one

User avatar
HansV
Administrator
Posts: 78417
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: SetFocus in VBComponent

Post by HansV »

I learn a lot for my own use by answering questions from others! :smile:
Best wishes,
Hans

User avatar
StuartR
Administrator
Posts: 12602
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: SetFocus in VBComponent

Post by StuartR »

kpark91 wrote:...Once I get this done, hopefully, I can help out with simple questions O_o
You'll have to be fast if you want to get in first!
StuartR