VBScript error 800A01B6 document.parentwindow.screen.availwidth - vbscript

See Error Message here
I have a script that had been working fine since years. Last week, we changed the user account and suddenly it stopped working and started returning with VBScript error 800A01B6 document.parentwindow.screen.availwidth. If we login with the previous account and execute the script, it runs perfect as ever. However, it does not run with the new account. The new user account is also in the same Administrators group as the old account. I can't figure out the difference between the two executions.
Please help.
Here's a sample of the code:
Dim oIEPW, sPWHTML, ctr
Dim objWMIService, colItems, objItem, ScreenWidth, ScreenHeight, Scr
'Get HTML and place it in a variable
Call WriteHTML(sPWHTML)
'Create Internet Explorer object
'Set oIEPW = WScript.CreateObject("InternetExplorer.Application", "IE_")
Set oIEPW = WScript.CreateObject("InternetExplorer.Application", "IE_")
'Set window properties
With oIEPW
.Navigate "about:blank" 'Use blank page as base
Do While .ReadyState <> 4 'Wait for IE process to start
WScript.Sleep 50
Loop
.Document.Open 'Create document that will be in IE window
.Document.Write (sPWHTML) 'Paste in HTML
.Document.Close 'Complete the page creation
.Resizable = 0
.ToolBar = 0 'Disable IE's tool bars
.StatusBar = 0 'Disable IE's status bar
.Width = 294 'Set window width in pixels
.Height = 155 'Set window height in pixels
ScreenWidth = .Document.ParentWindow.Screen.AvailWidth 'Find screen width in pixels
.Left = (ScreenWidth / 2) - (.Width / 2) 'Distance in pixels of window left edge to left side of screen
ScreenHeight = .Document.ParentWindow.Screen.AvailHeight 'Find screen height in pixels
.Top = (ScreenHeight /2) - (.Height / 2) 'Distance in pixels of window top edge to top of screen
'WshShell.Popup "Width: " & ScreenWidth & VbCrLf & "Height: " & ScreenHeight,,"Screen Resolution",4096
Do While .ReadyState <> 4 'Wait for all properties to be set
WScript.Sleep 50
Loop
.Visible = 1 'Make IE window visible
End With
'Bring IE window to the top
WshShell.AppActivate "Internet Explorer"

Related

How can I use an already open IE with VB Script? GetObject is not working

Below is the code block i used to open a new window. I tried using GetObject in place of Create Object as below. But did not work.
Set IE = GetObject("","InternetExplorer.Application")
'
Dim IE
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate "https://msn.com"
Do While IE.ReadyState <> 4
WScript.Sleep 1000
Loop
Set ElementCol = IE.Document.getElementsByTagName("a")
For Each Link In ElementCol
If Link.innerHTML = "News" Then
Link.Click
WScript.Sleep(6000)
Exit For
End If
Next
IE.navigate "https://msn.com"
This uses Explorer which used to do the internet.
Set objShell = CreateObject("Shell.Application")
Do
Set AllWindows = objShell.Windows
Count = 0
For Each window in AllWindows
msgbox window.locationname
' window.refresh2 3
Count = Count + 1
Next
If Count = 0 then Exit Do
Wscript.sleep 5000
Loop

VBscript automation script stuck when IE modal popup appears

I am using vbscript to automate an website operation using IE.
At one place, clicking a button creates a modal popup. When the popup appears, the script does not execute further and sort of freezes.
I thought of executing a second script, but I am unable to activate the popup window. The popup dialog has title as "Proceed -- Webpage Dialog", and 2 buttons - Ok and Cancel. I'm using the below code to activate the pop up window and then click Ok on it. But I am unable to capture the window. Below is the code for second script.
script2.vbs:
WScript.Sleep 5000
Msgbox "Started 2nd script"
For Each wnd In CreateObject("Shell.Application").Windows
If InStr(1, wnd.FullName, "iexplore.exe", vbTextCompare) > 0 Then
If InStr(1, wnd.Document.Title, "Proceed", vbTextCompare) > 0 Then
Set objIE = wnd
MsgBox "Found Window"
Exit For
End If
End If
Next
This does nothing, and I have no clue how to handle this popup. Any thoughts or workarounds for this, will be very helpful.
Update:
Below is the code for the main script.
script1.vbs:
Dim url
url = "http://example.com"
Set objIE = CreateObject("InternetExplorer.Application")
Set WshShell = WScript.CreateObject("WScript.Shell")
objIE.Visible = True
objIE.AddressBar = True
objIE.StatusBar = False
objIE.MenuBar = True
WshShell.AppActivate ("Internet Explorer")
objIE.Navigate url
'''
' Some code to perform website operations
'
'
WshShell.Run "script2.vbs"
''The below line triggers the popup
Set btn = objIE.Document.GetElementById("grayBtn").GetElementsByTagName("a").Item(0)
btn.Click

How to adjust the picture to the size of cell?

I copy the picture. How to adjust this picture to the size of cell, using VBA?
Use this code (Q related to this):
Sub copy()
Dim sh As Shape, ws1 As Worksheet, ws2 As Worksheet
Set ws1 = Worksheets("mechanic")
Set ws2 = Worksheets("character")
ws1.Shapes("Picture").copy
With ws2.Range("A8").MergeArea
.PasteSpecial
Set sh = ws2.Shapes(ws2.Shapes.Count) 'get last shape, i.e. pasted picture
If .Height / sh.Height < .Width / sh.Width Then
sh.ScaleHeight .Height / sh.Height, msoFalse
Else
sh.ScaleWidth .Width / sh.Width, msoFalse
End If
End With
End Sub

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

Displaying an image in a VBScript MsgBox

How can I display an image in a MsgBox?
The answer is: This is not possible. MsgBox can only display strings. (Documentation)
An alternative is to display your image in a small Internet Explorer window. Here's an example:
Set objExplorer = CreateObject("InternetExplorer.Application")
With objExplorer
.Navigate "about:blank"
.ToolBar = 0
.StatusBar = 0
.Left = 100
.Top = 100
.Width = 200
.Height = 200
.Visible = 1
.Document.Title = "Important image!"
.Document.Body.InnerHTML = _
"<img src='http://sstatic.net/stackoverflow/img/venn-diagram.png' height=100 width=100>"
End With
This should display the Venn diagram found in Stack Overflow's about section.
What you want is called a HyperText Application, or HTA. You use HTML to create the form.

Resources