how does objIE.Document.body.innertext work? - vbscript

I'm trying to understand how the objIE.Document.body.innertext works. From what I've read, it's almost like CTRL+C, however, I'm trying to get it to work and something's missing. Here's the code:
Dim objIE
Dim strPrintText
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Navigate "www.bing.com"
strPrintText = objIE.Document.body.innertext
msgbox(strPrintText)

You must wait until the browser is ready (and not use param list () when calling a Sub):
Dim objIE
Dim strPrintText
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Navigate "www.bing.com"
Do Until objIE.readyState = 4 : Wscript.Sleep 10 : Loop
strPrintText = objIE.Document.body.innertext
msgbox strPrintText
Please cf this for background.
WRT param list () in Sub calls:
>> MsgBox "No param () when calling a Sub!", vbOkOnly
>>
>> MsgBox("No param () when calling a Sub!", vbOkOnly)
>>
Error Number: 1044
Error Description: Cannot use parentheses when calling a Sub
>>
>> MsgBox "Do you believe me now?", vbOkOnly
For theory/reason cf this; and reflect on the merit of "it works" when discussing rules of programming (or ethics: stealing works fine, as long you are not caught).

Related

How Can I pause speak command in vbscript? I have to play it from the same paused position

How Can I pause speak command in vbscript? I have to play it from the same paused position.
Code block:
Dim Speak, Path
Path = "string"
Path = "C:\Users\sony\Desktop\TheReunion.txt"
const ForReading = 1
Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile(Path,ForReading)
strFileText = objFileToRead.ReadAll()
Set Speak=CreateObject("sapi.spvoice")
Speak.Speak strFileText
objFileToRead.Close
Set objFileToRead = Nothing
You need to call the speak method asynchronously before using the pause and resume methods as mentioned by LotPings in the comments.
Code:
Dim Speak, Path
Path = "string"
Path = "C:\Users\sony\Desktop\TheReunion.txt"
const ForReading = 1
Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile(Path,ForReading)
strFileText = objFileToRead.ReadAll()
Set Speak=CreateObject("sapi.spvoice")
Speak.Speak strFileText,1 '1=Asynchronous. Click the link below for other possible values "SpeechVoiceSpeakFlags"
'Due to async call to speak method, we can proceed with the code execution while the voice is being played in the background. Now we can call pause and resume methods
wscript.sleep 5000 'voice played for 5000ms
Speak.pause 'paused
wscript.sleep 4000 'remains paused for 4000ms
Speak.resume 'resumes
objFileToRead.Close
Set objFileToRead = Nothing
SpeechVoiceSpeakFlags
Intrigue in this led me to take inspiration from Kira's answer and develop it somewhat (in a bad, novice kind of way), to achieve the pause/resume objective interactively, the code below works for me, and hopefully it's of some help to you...
option explicit
dim strpath, fso, strfile, strtxt, user, voice, flag
flag = 2
call init
sub init
do while len(strpath) = 0
strpath = inputbox ("Please enter the full path of txt file", "Txt to Speech")
if isempty(strpath) then
wscript.quit()
end if
loop
'strpath = "C:\Users\???\Desktop\???.txt"
set fso = createobject("scripting.filesystemobject")
on error resume next
set strfile = fso.opentextfile(strpath,1)
if err.number = 0 then
strtxt = strfile.readall()
strfile.close
call ctrl
else
wscript.echo "Error: " & err.number & vbcrlf & "Source: " & err.source & vbcrlf &_
"Description: " & err.description
err.clear
call init
end if
end sub
sub ctrl
user = msgbox("Press ""OK"" to Play / Pause", vbokcancel + vbexclamation, "Txt to Speech")
select case user
case vbok
if flag = 0 then
voice.pause
flag = 1
call ctrl
elseif flag = 1 then
voice.resume
flag = 0
call ctrl
else
call spk
end if
case vbcancel
wscript.quit
end select
end sub
sub spk
'wscript.echo strtxt
set voice = createobject("sapi.spvoice")
voice.speak strtxt,1
flag = 0
call ctrl
end sub

VBS Object required: '[string: ""]'

I have the code
set WshShell = CreateObject("WScript.Shell")
Set ie = CreateObject("InternetExplorer.Application")
ie.Offline = True
ie.Navigate "about:blank"
ie.Height = 200
ie.Width = 425
ie.MenuBar = False
ie.StatusBar = False
ie.AddressBar = False
ie.Toolbar = False
ie.Visible = True
WshShell.Run "%windir%\notepad.exe"
WshShell.AppActivate "Notepad"
WScript.Sleep 3000
set a = WshShell.SendKeys("a") & Wscript.Sleep("100")
a
It does type "a" in notepad, but then it gives the error," Object Required: '[string: ""]' " and it will prevent any code after it from running.
If anyone knows how to fix this and prevent it in the future that would be great.
You're trying to assign something to a variable as an object that isn't an object (Set a = ...). Don't do that. Neither SendKeys() nor Sleep() return output, so there's no point in assigning that non-output anyway. Or in concatenating it (you're probably confusing the VBScript string concatenation operator & with the batch command chaining operator &).
Change this:
Set a = WshShell.SendKeys("a") & WScript.Sleep("100")
into this:
WshShell.SendKeys("a")
WScript.Sleep(100)
and the problem will disappear.
If you're trying to implement a procedure that you can invoke as a shorthand, that would be done e.g. like this:
Sub k
WshShell.SendKeys("a")
WScript.Sleep(100)
End Sub
k 'sends keystroke "a" and waits 100 ms
or like this, if you want it parametrized:
Sub k(keys)
WshShell.SendKeys(keys)
WScript.Sleep(100)
End Sub
k "b" 'sends keystroke "b" and waits 100 ms

Why does my VBA script not continue after debugging?

Whenever I hit an error with my script, the focus turns to the VBA code and the offending line. I fix it, and hit save. Then I notice that the script is no longer running, even after I make sure that it's not paused.
For example, right now I'm using a Form_Timer() event to do some testing (interval set to 1000ms). To test the script again, I just set it to a minute in the future (e.g. if the current time is 8:54:00 AM I set it to fire at 8:55:00 AM). But this stops working after an error. Does anyone know why this is? I don't want to have to tell my users to close and re-open their copies of the Access DB just to make the script work again.
Code:
Private Sub Form_Timer()
On Error GoTo ErrorHandler
current_date_time = Now
If current_date_time = #6/28/2016 8:52:00 AM# Then
MsgBox ("the current_date_time variable holds: " & current_date_time)
'Declare objects
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim qdf As DAO.QueryDef
Dim oApp As Outlook.Application
Dim oMail As Outlook.MailItem
Dim mail_body As String
'Set objects
Set dbs = CurrentDb
Set qdf = dbs.QueryDefs("qry_BMBFLoc")
Set rst = qdf.OpenRecordset
Set oApp = New Outlook.Application
Set oMail = oApp.CreateItem(olMailItem)
mail_body = "The following jobs do not have the special BF location set in Job Orders: " & vbCrLf
If Not (rst.EOF And rst.BOF) Then
rst.MoveFirst
Do Until rst.EOF = True
mail_body = mail_body & rst!job & "-" & rst!suffix & vbCrLf
rst.MoveNext
Loop
'Email contents
oMail.Body = mail_body
oMail.Subject = "Blah"
oMail.To = "someone#something.com"
oMail.Send
'Close stuff
rst.Close
dbs.Close
Set rst = Nothing
Set oMail = Nothing
Set oApp = Nothing
End If
End If
Exit Sub
ErrorHandler:
Dim msg As String
If Err.Number <> 0 Then
msg = "email Form Timer Error #" & Str(Err.Number) & " error Line: " & Erl & Chr(13) & Err.Description
MsgBox msg, , "Error", Err.HelpFile, Err.HelpContext
End If
Exit Sub
End Sub
In order to reactivate the code, you could close the form when the error is triggered. The user would then have to reload the form to complete the action.
However, without any intervention the error is likely to occur again.
Edit: Or you could write a Function to automatically close, and re-open the offending form. Calling it in the on error command.
When there is an error in access form, the timer will stop working, you don't need to close and reopen the whole database, only the form to start the timer again. Otherwise you can add a button called "refresh" and bind macro to it which will turn the timer on again.
Yeah this sucks. I am writing a vba script for outlook and so the only way to debug is to close and reopen outlook after every error.

VBscript Object required 800A01A8

Please help. cant make this code work. It requires an object. Don't know how to resolve this. I see no typo error on my script. I am trying to check if there is an Internet Explorer that is open to https://www.test.com in my PC. Badly needed for a school project. Thanks in advance
Option Explicit
Dim objShell, objShellWindows, i, objIE
Set objShell = CreateObject("Shell.Application")
Set objShellWindows = objShell.Windows
For i = 0 to objShellWindows.Count - 1
Set objIE = objShellWindows.Item(i)
strURL = objIE.LocationURL
If InStr(strURL, "https://www.test.com/")Then
blnFound = True
End If
Next
Below code will check all the URLs loaded in the IE,
Note that here bFound is True when sURL is inside .LocationURL, not begin with. Modify to suit your need.
Option Explicit
Const sURL = "https://www.test.com/"
Dim oShell, oWindow, oIE, bFound
Set oShell = CreateObject("Shell.Application")
bFound = False
For Each oWindow In oShell.Windows
If InStr(1,oWindow.Fullname,"iexplore.exe",vbTextCompare) > 0 Then
Set oIE = oWindow
With oIE
'Wscript.Echo .LocationURL
bFound = (InStr(1,.LocationURL,sURL,vbTextCompare) > 0)
If bFound Then Exit For
End With
Set oIE = Nothing
End If
Next
If bFound Then
Wscript.Echo sURL & " is loaded"
Else
Wscript.Echo sURL & " is NOT loaded"
End If
Set oShell = Nothing

Show msg box in less than a second

Is there a way to make a msgbox show and disappear in under a second,I have been using the script below, but can only get it to close in a second.
Option Explicit
Dim Wshell, BtnCode
Set Wshell = CreateObject("wscript.shell")
BtnCode= Wshell.Popup ("test", 1, "testing")
I'm afraid this is not possible with popup.
You can do it with Internet Explorer as UI though.
Set oIE = CreateObject("InternetExplorer.Application")
With oIE
.navigate("about:blank")
.Document.Title = "Countdown" & string(100, chrb(160))
.resizable=0
.height=200
.width=100
.menubar=0
.toolbar=0
.statusBar=0
.visible=1
End With
' wait for page to load
Do while oIE.Busy
wscript.sleep 50
Loop
' prepare document body
oIE.document.body.innerHTML = "<div id=""countdown"" style=""font: 36pt sans-serif;text-align:center;""></div>"
oIE.document.all.countdown.innerText= "test message"
'the parameters is in miliseconds, so here the message is shown for half a second
wscript.sleep 500
oIE.quit

Resources