How to set focus on Ms word???

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • darith
    New Member
    • Mar 2008
    • 4

    How to set focus on Ms word???

    Hello all, I want vb.net code to set focus or send cursor to ms word. Could you kindly help me?


    For example,

    Imports Microsoft.Offic e.Core
    Imports Microsoft.Offic e.Interop

    Dim wapp As Word.Applicatio n
    Dim wdoc As Word.Document
    wapp = GetObject(, "Word.Applicati on")
    wapp = Nothing
    wdoc = wapp.ActiveDocu ment
    wapp.Visible = True

    wapp.activedocu ment.range.text 'if i write like this i will get all text from MS word, but i want to set focus on ms word i want to know vb.net coding to set focus on ms word. if we use text box we just write textbox.focus() .

    Please help me.

    thank very much.
  • spacemanafrica
    New Member
    • Mar 2008
    • 6

    #2
    Try something like this:

    In the beginning of your program inside the class declaration:

    Code:
    Declare Auto Function FindWindow Lib "USER32.DLL" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    
    Declare Auto Function SetForegroundWindow Lib "USER32.DLL" (ByVal hWnd As IntPtr) As Boolean
    then in your method:

    Code:
    Dim wordHandle As IntPtr = FindWindow(vbNullString, "Microsoft Office Word")
    If wordHandle = IntPtr.Zero Then
      MsgBox("Oops!  Couldn't find MS Word window!")
    Else
        SetForegroundWindow(wordHandle)
    End If

    Comment

    Working...