Can I read NOTEPAD with MS Agent?

Posted on 2000-05-08
Last Modified: 2010-05-02
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.
Question by:Jubel
  • 3
LVL 28

Expert Comment

ID: 2789122
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
LVL 28

Expert Comment

ID: 2789163
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

Expert Comment

ID: 2789186
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
LVL 28

Accepted Solution

AzraSound earned 200 total points
ID: 2789456
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.


Author Comment

ID: 2795191
Thanks for your help. I have been trying to do that since I started using agent in my programs.

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question