problem with makro that worked for over a year now (runtime 1004) - runtime

I have a problem with one of my makro that worked so far for over a year now. It keeps on showing runtime error 1004 on paste.
The makro goes as:
Sheets("Allianz GI").Select
Range("A1:J1000").Clear
Range("A1").Select
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = True
.Navigate "https://pl.allianzgi.com/pl-pl/nasze-fundusze/fundusze/list?ShareClassCurrency=(PLN)"
Do Until .ReadyState = 4: DoEvents: Loop
Application.Wait (Now() + TimeValue("00:00:20"))
IE.ExecWB 17, 0 '// zaznacz wszystko
IE.ExecWB 12, 2 '// kopiuj
Range("A1").Select
ActiveSheet.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False, NoHTMLFormatting:=True
IE.Quit
End With
This makro has been working fine up until now and I have no idea what can I do to fix it.
Additionaly when the runtime error shows, when you select debug and then click run(F5) the makro resumes from the error line and goes on like nothing has happened and just pastes everything from the site.
What can I change to make this work again? Can I somehow force makro to ignore error and just do it without going into debug?

Related

How to close all broswers except quality center in QTP?

I have below code but sometimes it gets an error as object not visible.
Dim TempIndex,oDesc
'1) Create a Browser object'
Set oDesc=Description.Create
oDesc("micclass").Value="Browser"
TempIndex=0
'2) loop and Check if a browser is open'
While Browser("micclass:=Browser","index:="&TempIndex).exist(0) and TempIndex<Desktop.ChildObjects(oDesc).count
'3) Close the browser if its not Quality center '
If instr(1, Browser("micclass:=Browser","index:="&TempIndex).getRoProperty("name"),"HP Application Lifecycle Management 12.50") = 0 Then
Browser("micclass:=Browser","index:="&TempIndex).close
else
TempIndex=TempIndex+1
End if
Wend
It's a old thread but maybe my response be useful for anymore.
I had the same trouble. The solution was clean the objects in the final of script. Add this in the final:
Set oDesc = Nothing
Set TempIndex = Nothing
Goodluck,

Window("hwnd:=" & handle).Restore is causing Object not visible error

I have an application that opens up some new tabs. I'm trying to cycle through these tabs, look at them, and then close them.
Dim tab_children, oDesc
Set oDesc = Description.Create
oDesc("micclass").value = "Browser"
Set tab_children = Desktop.ChildObjects(oDesc)
Dim title, handle, cTime
For i = 0 To tab_children.Count-1 Step 1
title = tab_children(i).GetROProperty("title")
handle = tab_children(i).GetROProperty("hwnd")
Window("hwnd:=" & handle).Restore
msgbox title & ": " & handle
Next
When we try to execute the .Restore, I receive an "object not visible" error. The tab that we're trying to restore is not the one that has focus, could that be the issue and if so how can we resolve it? I was under the impression that .Restore would bring that tab into focus based off of this thread, http://www.advancedqtp.com/old_forums/viewtopic.php?t=1970
The IDE I'm using is QTP, the Browser is IE.
A potential work around that I've been thinking about:
After the application opens up the new tabs, the last opened tab has focus. If we close that one, the 2nd to last has focus, all the way down to the original application's tab. Perhaps there's a way to utilize this information.
Restore has worked for me in the past, try using Activate-
Window("hwnd:=" & handle).Activate
Edited: Just tested the following and its working on my machine-
'Create Browser Descriptor
Set oBrowser=Description.Create
oBrowser("micclass").Value="Browser"
'Get the child objects
Set oBrowser=Desktop.ChildObjects(oBrowser)
totalcount = oBrowser.Count-1
For i=0 to totalcount
If Browser("micclass:=Browser", "index:="&i).Exist(0) Then
'get the hwnd everytime there's an iteration
ohwnd= Browser("micclass:=Browser", "index:=" & i).GetROProperty("hwnd")
'For debugging purposes
name = Browser("hwnd:="&ohwnd).GetROProperty("title")
msgbox name
Set oBrowser=Browser("hwnd:="&ohwnd)
'Page descriptor
Set oPage=Description.Create
oPage("micclass").Value="Page"
Set oPage=Browser("hwnd:="&ohwnd).ChildObjects(oPage)
For n=0 to oPage.Count-1
If oPage(n).Exist(0) Then
oBrowser.Close
Exit For
End If
Next
End If
Next
If you want to close only a particular page you can use the GETROPREPERTY("Title") in the If loop - If oPage(n).Exist(0)

How to Zoom in or Zoom out in a webpage while using UFT/QTP

I would like to control the zoom in and out feature of my webpage of the application under test using UFT. This is required as the zoom level changes dynamically and it becomes difficult to identify the objects.
I have found a code but it is useful if you need to change the zoom level at one instance or at the start. below is the code
Function ChangeIEZoom
Dim intZoomLevel, objIE
intZoomLevel = 110
Const OLECMDID_OPTICAL_ZOOM = 63
Const OLECMDEXECOPT_DONTPROMPTUSER = 2
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Visible = True
objIE.Navigate ("www.google.com")
While objIE.Busy = True
wait 5
Wend
objIE.ExecWB OLECMDID_OPTICAL_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER, CLng(intZoomLevel), vbNull
End Function
with this code, it opens up a new browser and navigates it to a URL.
I do not want it to create a new instance of the browser.
What I need is that it changes the zoom level on the same page which is already under test execution, also the page where zoom level change is required not known at the start and it may or may not require change based on the fact that it identifies certain objects.
Has anyone faced the same issue or has a solution to it ?
I found a solution - combining what you mentioned in comments. this works if you want to change the zoom level on current webpage you are working on. helps when you want to zoom in and out at multiple instances
Dim ShellApp
Set ShellApp = CreateObject("Shell.Application")
Dim ShellWindows
Set ShellWindows = ShellApp.Windows()
Dim intZoomLevel
intZoomLevel = 110
Const OLECMDID_OPTICAL_ZOOM = 63
Const OLECMDEXECOPT_DONTPROMPTUSER = 2
Dim i
For i = 0 To ShellWindows.Count - 1
If InStr(ShellWindows.Item(i).FullName, "iexplore.exe") <> 0 Then
Set IEObject = ShellWindows.Item(i)
End If
If IEObject.Visible = True Then
While IEObject.Busy = True
wait 5
Wend
IEObject.ExecWB OLECMDID_OPTICAL_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER, CLng(intZoomLevel), vbNull
End If
Next
print "it works"

Saving snapshot using vbscript

I am new to vbscript. I want to save a snapshot taken using vbscript of a internet explorer window opened by vbscript.
Code to load the page
Dim IE, stateString
Set IE = WScript.CreateObject("InternetExplorer.Application")
ie.toolbar = 1
ie.statusbar = 1
ie.width = 999
ie.height = 500
ie.left = 20
ie.theatermode = false
ie.theatermode = true
ie.theatermode = false
ie.top = 50
ie.navigate("file:///C:\Users\Vinit_Tiwari\Documents\vbscripts\someform.html")
'stateString = cstr(ie.readystate)
waitforload(ie)
ie.visible = 1
Set wshShell = CreateObject("Wscript.Shell")
wshShell.AppActivate "Some Form"
wshShell.SendKeys "% x"
Code to take snapshot
Dim oWordBasic
set oWordBasic = CreateObject("Word.Basic")
oWordBasic.sendkeys "{prtsc}"
oWordBasic.AppClose "Microsoft Word"
Set oWordBasic = Nothing
Wscript.Sleep 2000
Saving the snapshot
dim paint
set paint = wshShell.exec("mspaint")
do while paint.status = 0:loop
wshShell.appactivate("untitled-Paint")'this returns false
Wscript.sleep 500
WshShell.SendKeys "^v"
wscript.sleep 500
wshshell.sendkeys "^s"
wscript.sleep 500
wshshell.sendkeys "d:\test.png"
wscript.sleep 500
wshell.sendkeys "{Enter}"
Set wshshell = Nothing
Actually the previously opened ie window is having focus and the keystrokes are sent to the ie instead of paint. so is there any function which performs opposite work of AppActivate.
This won't work. Microsoft Word is in focus while the Print Screen button is pressed. You can easily eliminate this problem by not using Microsoft Word at all. It's not necessary. The Print Screen function is a system hotkey and has nothing to do with Microsoft Word. You should be using the Alt+PrintScreen combination which captures a screenshot of the currently focused window to the clipboard.
Second, if Paint is not in focus instead of IE, it's because you have the window title wrong. You are doing that part correctly. AppActivate returns false if it cannot find a window with the specified title. To be honest, Paint should alread have focus to begin with but it is good practice to make sure the window is activated first.
Also, is this a very old system? Why are you using the Word.Basic automation object anyway out of curiosity?

Clicking on a link that contains a certain string in VBS

I'm trying to run an automated vbs script that clicks on a link on a page. I have things of the form:
Const READYSTATE_COMPLETE = 4
Set IE = CreateObject("INTERNETEXPLORER.APPLICATION")
IE.Visible = true
IE.navigate ("http://mywebpage.com")
How do I then make it click on a link on that page that doesn't have an ID but is like
ClickMe!
Thanks!
Along the lines of
Dim LinkHref
Dim a
LinkHref = "link"
For Each a In IE.Document.GetElementsByTagName("A")
If LCase(a.GetAttribute("href")) = LCase(LinkHref) Then
a.Click
Exit For ''# to stop after the first hit
End If
Next
Instead of LCase(…) = LCase(…) you could also use StrComp(…, …, vbTextCompare) (see StrComp() on the MSDN).

Resources