Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Can I read NOTEPAD with MS Agent?

Posted on 2000-05-08
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 800 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

610 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