To sum up, you have two solutions:
Either you put WaitUntilDone before EndSub
Either you launch your function on another thread
@Konti in another thread, it also doesn't work because it breaks the text as soon as the function or sub called ends.
And if you run non-asynchronous, it waits for the end of the function or sub to continue.
I finally found
Function Msg_sonore(sText As String) As Boolean
'Permet de lire le message de manière asynchrone en passant par .vbs
Dim swApp As Object
Dim fso, Dossier, Fichier, f
Dim oWsh As Variant
Set swApp = Application.SldWorks
Set oWsh = CreateObject("Shell.Application")
Set fso = CreateObject("Scripting.FileSystemObject")
Dossier = Left(swApp.GetCurrentMacroPathName, InStrRev(swApp.GetCurrentMacroPathName, "\"))
Fichier = "PART_MEP_suppr contraintes.vbs"
If Dir(Dossier & Fichier) <> "" Then fso.DeleteFile Dossier & Fichier
Set f = fso.CreateTextFile(Dossier & Fichier, True)
f.WriteLine "'Fichier temporaire, pour lecture message sonore asynchrone"
f.WriteLine "Dim speech"
f.WriteLine "Set speech = CreateObject(""SAPI.SpVoice"")"
f.WriteLine "speech.Speak """ & sText & """"
f.Close
oWsh.ShellExecute Dossier & Fichier
Set oWsh = Nothing
End Function
1 Like