Can I read NOTEPAD with MS Agent?

I am trying to make Microsoft Agent read text inserted into the windows applet NOTEPAD. I am using the character 'Peedy' and have not had much luck. I have been able to make him readanything in VB that has a caption or text property. If any expert has knowledge of Agent then please get back to me. Thanks.
Who is Participating?
AzraSoundConnect With a Mentor Commented:
in the case you dont know the file that is opened in Notepad and you want to be able to read what is showing in the current running window of Notepad, use this code:


Public Type ProcData
    AppHwnd As Long
    title As String
    Placement As String
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type

Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Public Const WM_GETTEXT = &HD

Public Const GW_CHILD = 5
Public Const GW_HWNDNEXT = 2
Public Const GW_HWNDFIRST = 0

Public str As String

Public Function EditInfo(window_hwnd As Long) As String
    Dim txt As String
    Dim buf As String
    Dim buflen As Long
    Dim child_hwnd As Long
    Dim children() As Long
    Dim num_children As Integer
    Dim i As Integer

    ' Get the class name.
    buflen = 256
    buf = Space$(buflen - 1)
    buflen = GetClassName(window_hwnd, buf, buflen)
    buf = Left$(buf, buflen)
    ' See if we found an Edit object.
    If buf = "Edit" Then
        EditInfo = WindowText(window_hwnd)
        Exit Function
    End If
    ' It's not an Edit object. Search the children.
    ' Make a list of the child windows.
    num_children = 0
    child_hwnd = GetWindow(window_hwnd, GW_CHILD)
    Do While child_hwnd <> 0
        num_children = num_children + 1
        ReDim Preserve children(1 To num_children)
        children(num_children) = child_hwnd
        child_hwnd = GetWindow(child_hwnd, GW_HWNDNEXT)
    ' Get information on the child windows.
    For i = 1 To num_children
        txt = EditInfo(children(i))
        If txt <> "" Then Exit For
    Next i

    EditInfo = txt
End Function
' ************************************************
' Return the text associated with the window.
' ************************************************
Public Function WindowText(window_hwnd As Long) As String
    Dim txtlen As Long
    Dim txt As String

    WindowText = ""
    If window_hwnd = 0 Then Exit Function
    txtlen = SendMessage(window_hwnd, WM_GETTEXTLENGTH, 0, 0)
    If txtlen = 0 Then Exit Function
    txtlen = txtlen + 1
    txt = Space$(txtlen)
    txtlen = SendMessage(window_hwnd, WM_GETTEXT, txtlen, ByVal txt)
    WindowText = Left$(txt, txtlen)
End Function

Public Function EnumProc(ByVal app_hwnd As Long, ByVal lParam As Long) As Boolean
    Dim buf As String * 1024
    Dim title As String
    Dim length As Long

    ' Get the window's title.
    length = GetWindowText(app_hwnd, buf, Len(buf))
    title = Left$(buf, length)

    ' See if the title ends with " - Notepad".
    If Right$(title, 10) = " - Notepad" Then
        ' This is it. Find the ComboBox information.
        str = EditInfo(app_hwnd)

        ' Stop searching.
        EnumProc = 0
        ' Continue searching til find it.
        EnumProc = 1
    End If
End Function

****FORM CODE****

Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Any, ByVal lParam As Long) As Long
Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Const BalloonOn = 1
Const SizeToText = 2
Const AutoHide = 4
Const AutoPace = 8

Public genie As IAgentCtlCharacterEx
Dim geniePath As String

Private Sub Form_Load()
    Dim DirName As String
    DirName = GetWindowsDir()
    geniePath = DirName + "msagent\chars\Genie.acs"
    Set genie = Nothing
    MyGenie.Characters.Load "CharacterID", geniePath
    Set genie = MyGenie.Characters("CharacterID")
    genie.LanguageID = &H409  ' english
    genie.SoundEffectsOn = True
    genie.Balloon.Style = genie.Balloon.Style Or BalloonOn
    genie.Balloon.Style = genie.Balloon.Style Or AutoHide
    genie.Balloon.Style = genie.Balloon.Style Or AutoPace
    genie.Balloon.Style = genie.Balloon.Style Or SizeToText
    genie.Commands.Add "AdvCharOptions", "&Advanced Character Options"
    genie.Left = (Form1.Left + 4500) / Screen.TwipsPerPixelX
    genie.Top = (Form1.Top + 1500) / Screen.TwipsPerPixelY
    EnumWindows AddressOf EnumProc, 0
    genie.Speak str
End Sub

Function GetWindowsDir() As String
    Dim temp As String
    Dim retval As Long
    Const MAX_LENGTH = 145

    temp = String$(MAX_LENGTH, 0)
    retval = GetWindowsDirectory(temp, MAX_LENGTH)
    temp = Left$(temp, retval)
    If temp <> "" And Right$(temp, 1) <> "\" Then
        GetWindowsDir = temp & "\"
        GetWindowsDir = temp
    End If
End Function

I used genie as you can see and he said what was in my open Notepad window upon the form load event.

you would do it the same way...all you need to do is extract the text from the notepad text file and pass that as the text for the character to say
for example, this would read in a text file into a textbox, you can then just pass this string to your character to say:

Const ForReading = 1

Private Sub Command1_Click()
    Dim a, fs As Object
    Dim str As String
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set a = fs.opentextfile("C:\Windows\Desktop\test.txt", ForReading)
    str = a.ReadAll
    Text1.Text = str
End Sub
Try this:

'rtxt = ""
'rtxt = frm_cliptxt.Text1.Text
Clipboard.SetText frm_cliptxt.Text1
rtxt = Clipboard.GetText
If rtxt = "" Then Exit Sub
'Robby.MoveTo 1, 500
Robby.Speak rtxt
JubelAuthor Commented:
Thanks for your help. I have been trying to do that since I started using agent in my programs.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.