CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2021
    Posts
    1

    Speak (VBA) - Method or data member not found

    Suddenly I cannot get .Speak to work without the above error. The macro below worked fine, until it didn't and started getting this error. Suggestions?

    Code:
    Sub Range()
    '
    ' Range Macro
    '
    '
    Dim rng As Word.Range
    Dim SearchString As String
    'Dim Speak As SpVoice
    Dim Speech As SpeechLib.SpVoice
    Set rng = ActiveDocument.Content
    Set Speech = New SpVoice
    SearchString = """"
    
    With rng.Find
    'rng.Find.ClearFormatting
        Do While .Execute(findText:=SearchString, Forward:=True) = True
          rng.MoveEndUntil (".?,;!")
        
    'Speech.Speak "Hello"
     
          Application.Speech.Speak "speaking", SVSFlagsAsync '+ SVSFPurgeBeforeSpeak
       'Speak "speaking", SVSFlagsAsync '+ SVSFPurgeBeforeSpeak
           MsgBox rng.Text
          Exit Do
          rng.Collapse wdCollapseEnd
        Loop
        
    End With
    
    End Sub
    Last edited by VictorN; June 8th, 2021 at 01:44 AM. Reason: added CODE tags

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,096

    Re: Speak (VBA) - Method or data member not found

    [Moved from Chit Chat forum]
    Victor Nijegorodov

  3. #3
    Join Date
    Aug 2021
    Posts
    2

    Re: Speak (VBA) - Method or data member not found

    [This is the third time I've tried to answer this question, but the site keeps crashing on me. So I'll be brief)

    Hi,

    Your code mostly looks as though you are attempting to run it in Word (e.g., Word.Range, ActiveDocument), but the key TTS part of your code uses Application.Speech.Speak, which is exclusively for the Excel application object. In order to use TTS in word, you will need to use the SAPI library, which I note that you have sought to do through early-binding ("Microsoft Speech Object Library").

    Just trying to reverse engineer what may have changed since when it was working, I wonder if maybe
    Code:
    SVSFlagsAsync
    is partly the culprit. Because you are instructing SAPI to run asynchronously (meaning, not hold up any other processes), the code completes before it has had a chance to run, thus destroying the SPVoice object at End Sub.

    Running your code, if Word is unable to find anything (which it won't given what your code tells it to look for) it escapes the loop via Exit Do and heads straight for End Sub, and destroys the SPVoice object before it could speak. The MsgBox that would otherwise stop it would have helped in debugging the problem, but I suspect that by that point you had already tried the Application.Speech.Speak approach and that didn't work either. That's what I meant by SVSFlagsAsync being 'partly' the culprit. You could use the default SVSFDefault instead, but that would make the code your trying to run annoyingly slow - Word would wait until SAPI had finished saying the word before it could look again, and then stop again and wait again... The same also (though to a lesser extent) applies to the flag, SVSFPurgeBeforeSpeak.

    Anyway, (1) You cant use Application.Speech.Speak in Word; (2) The speech object is being destroyed before it can execute (because you have it searching for nothing). I would recommend : (a) giving it a chance to run; (b) using the SAPI library (as you had previously and as below).

    Code:
    Dim Speech As SpeechLib.SpVoice
    Set Speech = New SpVoice
    Speech.Speak Content
    Hope that helps.

  4. #4
    Join Date
    Aug 2021
    Posts
    2

    Re: Speak (VBA) - Method or data member not found

    Sorry, I should qualify this - you can use the Application.Speech.Speak approach if you really want, it's just that you would need to instantiate Excel to do it.

    Code:
    Sub SpeakToMe()
        Dim XL As Object
        Set XL = CreateObject("Excel.application")
        
        XL.Speech.Speak "This seems a burdensome approach."
        XL.Quit
        
        Set XL = Nothing
    End Sub
    But it's a bit overkill when there are quicker/less CPU intensive options available. I mention it here for completeness.
    Last edited by VictorN; August 14th, 2021 at 07:51 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured