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

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)

Related

How to Maximize Screen from VBScript?

The web application that displays database information is much more readable in full-screen mode rather than a small window. Problem is, I cannot get the window to maximize from UFT/QTP.
I've tried running the browser object from Wscript.Shell, but the application returns to UFT and maximizes that window instead of the newly created browser window.
siteA = "https://google.com"
Const max_window = 3
Set browobj = CreateObject("Wscript.Shell")
Set oShell = CreateObject("WScript.Shell")
'browobj.Run "chrome -k -incognito -url "&siteA
browobj.Run "chrome -incognito -url "&siteA, max_window
oShell.SendKeys "% x"
browobj.sendkeys "{F9}"
browobj.sendkeys "(% )X"
browobj.SendKeys "% x"
Set browobj = Nothing
Any solutions to maximizing the window with the focus object of the new browser?
Edit:
Even below will not maximize taken from this List of Controls is not working.
SystemUtil.Run "chrome.exe" , siteA ,,,3
chrome.exe --incognito --start-maximized
If you shut down all instances of chrome these controls work fine, but if you have an active window in chrome it will take those properties.
We have a function defined during our browser login process that grabs the window and maximises it so that our UFT processes run with a (nearly) full screen browser - we don't need to hide the menu bar etc. This is the function we are using:
Public Function MISC_MaximiseBrowser(ByVal oBrowser, ByRef ErrorMsg)
LOG_Write vbNewLine & "MISC_MaximiseBrowser"
LOG_Write "oBrowser: " & oBrowser.GetROProperty("TestObjectName")
Dim Hwnd
dim bIsMax, bIsMaxable
MISC_MaximiseBrowser = True
Hwnd = oBrowser.Object.HWND
If Window("hwnd:=" & Hwnd).GetROProperty("maximized") Then
bIsMax = True
Else
bIsMax = False
End If
If Window("hwnd:=" & Hwnd).GetROProperty("maximizable") Then
bIsMaxable = True
Else
bIsMaxable = False
End If
If Not bIsMax And bIsMaxable Then
Window("hwnd:=" & Hwnd).Maximize
End If
End Function
What this does is accepts the Browser UFT object you're working with and grabs the handle for it. If the browser is not already maximized and it's possible to maximize it, it will do so.
You can ignore the LOG_Write statements at the start of the function - those are for our internal logging steps

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,

VBS / Making Word Document Visible

I have a simple script that I would like to run and it check to see if there's a word document open, and if it's open, then make it visible. It can be ANY word document, so I can't make it specifically to any file name.
Here's the code so far:
Dim Word
Set Word = GetObject(, "Word.Application")
If Word Is Nothing Then
MsgBox "Is not running"
Else
MsgBox "Is running"
Word.Visible = True
End If
Everything works except for Word.Visible = True. The popup shows "Is running" but the word document isn't brought to the front, selected or made visible at all. What am I missing? Thanks!
Updated details:
I've even tried it like this...
Dim Word
Set Word = GetObject(, "Word.Application")
Word.Visible = True
Word.Selection.TypeText "Hello Word"
With this code...As long as Word is currently opened, it should make it visible, then write Hello Word. It does write Hello Word, but doesn't make it visible. One I run it, I can see Word flashing in my task bar because Hello Word was added, but still doesn't make it visible. Hope that helps!
It seems you're misunderstanding how the Visible property works. That property defines if the application is visible at all or not (as in "does or doesn't show up in the taskbar").
Apparently your application already is visible (otherwise you wouldn't be able to see it flashing in the taskbar), but what you actually want is to un-minimize it and bring it to the front. The WindowState property should do that for you:
Const wdWindowStateNormal = 0
Const wdWindowStateMaximize = 1
Const wdWindowStateMinimize = 2
Set wd = GetObject(, "Word.Application")
If wd Is Nothing Then
MsgBox "Is not running"
Else
MsgBox "Is running"
wd.WindowState = wdWindowStateNormal
End If
Set oWord = CreateObject ("Word.Application")
oWord.Visible = True

How to add a Add ins menu tab to Power Point 2007?

I am working with Power Point 2007 but there is no Add Ins menu tab and I can not find how to add it.
When PPT 2007 and onward runs code that creates "legacy" command bars or menu modifications, it automatically adds the Add-ins tab to the ribbon and puts the command bars/menu changes there. Here's some simple example code. You can run it as is, or save it as an add-in. Once the add-in is loaded, the Auto_Open code will run every time PPT starts up.
Sub Auto_Open()
Dim oToolbar As CommandBar
Dim oButton As CommandBarButton
Dim MyToolbar As String
' Give the toolbar a name
MyToolbar = "Kewl Tools"
On Error Resume Next
' so that it doesn't stop on the next line if the toolbar's already there
' Create the toolbar; PowerPoint will error if it already exists
Set oToolbar = CommandBars.Add(Name:=MyToolbar, _
Position:=msoBarFloating, Temporary:=True)
If Err.Number <> 0 Then
' The toolbar's already there, so we have nothing to do
Exit Sub
End If
On Error GoTo ErrorHandler
' Now add a button to the new toolbar
Set oButton = oToolbar.Controls.Add(Type:=msoControlButton)
' And set some of the button's properties
With oButton
.DescriptionText = "This is my first button"
'Tooltip text when mouse if placed over button
.Caption = "Do Button1 Stuff"
'Text if Text in Icon is chosen
.OnAction = "Button1"
'Runs the Sub Button1() code when clicked
.Style = msoButtonIcon
' Button displays as icon, not text or both
.FaceId = 52
' chooses icon #52 from the available Office icons
End With
' Repeat the above for as many more buttons as you need to add
' Be sure to change the .OnAction property at least for each new button
' You can set the toolbar position and visibility here if you like
' By default, it'll be visible when created. Position will be ignored in PPT 2007 and later
oToolbar.Top = 150
oToolbar.Left = 150
oToolbar.Visible = True
NormalExit:
Exit Sub ' so it doesn't go on to run the errorhandler code
ErrorHandler:
'Just in case there is an error
MsgBox Err.Number & vbCrLf & Err.Description
Resume NormalExit:
End Sub
Sub Button1()
' This code will run when you click Button 1 added above
' Add a similar subroutine for each additional button you create on the toolbar
' This is just some silly example code.
' You'd put your real working code here to do whatever
' it is that you want to do
MsgBox "Stop poking the pig!"
End Sub

How to call a visio macro from a stencil

i have written some Macros for Visio. Now I copied these to a Stencil called Macros.vss
How can I call my Macros now?
It all depends on what the macros do and how you'd like to call them. I'm going to assume they're simply macros that will execute something within the active Visio page.
By default in Visio VBA, any public subs with no arguments get added to the Visio Tools->Macros menu, in a folder named by the document holding the macros (in this case Macros) and then separated into folders by module name. If you're the only person using the macros then you probably don't need to do anything else.
However, since you put them in a vss file I'll assume you'd like to distribute them to other people.
There's something funny (and by funny I mean irritating) about Visio and how toolbars and buttons work, when added programmatically. Unfortunately, when you create a toolbar using the UIObject and Toolbar and ToolbarItem classes, Visio is going to assume the code you're calling resides in the active drawing, and cannot be in a stencil. So I can give you a little guidance on using those classes, but basically it consists of distributing a .vst template along with your .vss files, with just a single required sub in the .vst file.
So, instead of using a custom toolbar, you can attach code to shape masters in your .vss file that execute the code when they get dropped on a drawing document (using CALLTHIS and the EventDrop event in the shapesheet). With this method I just have a sub that gets called using callthis that takes a shape object as an argument, executes some code, then deletes the shape (if I don't want it around anymore).
And lastly, you can manipulate the Visio UI programmatically to add a toolbar and buttons for your macros. Below is some sample code, basically the way I do it with a solution I developed. As I mentioned above, the most important part of using this method is to have a document template (.vst) that holds a sub (with the below code it must be named RunStencilMacro) that takes a string as an argument. This string should be the "DocumentName.ModuleName.SubName". This sub must take the DocumentName out of the string, and get a Document object handle to that document. Then it must do ExecuteLine on that document with the ModuleName.SubName portion. You'll have to step through the code and figure some things out, but once you get the hang of what's going on it should make sense.
I'm not sure of any other ways to execute the macros interactively with VBA. I think exe and COM addons may not have this issue with toolbars...
Private Sub ExampleUI()
Dim UI As Visio.UIObject
Dim ToolbarSet As Visio.ToolbarSet
Dim Toolbars As Visio.Toolbars
Dim Toolbar As Visio.Toolbar
Dim ToolbarItems As Visio.ToolbarItems
Dim ToolbarItem As Visio.ToolbarItem
Dim TotalToolBars As Integer
Dim Toolbarpos As Integer
Const ToolbarName = "My Toolbar"
' Get the UIObject object for the toolbars.
If Visio.Application.CustomToolbars Is Nothing Then
If Visio.ActiveDocument.CustomToolbars Is Nothing Then
Set UI = Visio.Application.BuiltInToolbars(0)
Else
Set UI = Visio.ActiveDocument.CustomToolbars
End If
Else
Set UI = Visio.Application.CustomToolbars
End If
Set ToolbarSet = UI.ToolbarSets.ItemAtID(visUIObjSetDrawing)
' Delete toolbar if it exists already
TotalToolBars = ToolbarSet.Toolbars.Count
For i = 1 To TotalToolBars
Set Toolbar = ToolbarSet.Toolbars.Item(i - 1)
If Toolbar.Caption = ToolbarName Then
Toolbar.Visible = False
Toolbar.Delete
Exit For
End If
Next
' create toolbar
Set Toolbar = ToolbarSet.Toolbars.Add
Toolbar.Caption = ToolbarName
Dim IconPos As Long ' counter to determine where to put a button in the toolbar
IconPos = IconPos + 1
Dim IconFunction As String
IconFunction = """Macros.Module1.SubName"""
Set ToolbarItem = Toolbar.ToolbarItems.AddAt(IconPos)
With ToolbarItem
.AddOnName = "RunStencilMacro """ & IconFunction & """"
.Caption = "Button 1"
.CntrlType = Visio.visCtrlTypeBUTTON
.Enabled = True
.state = Visio.visButtonUp
.Style = Visio.visButtonIcon
.Visible = True
.IconFileName ("16x16IconFullFilePath.ico")
End With
' Now establish the position of this toolbar
With Toolbar
.Position = visBarTop 'Top overall docking area
.Left = 0 'Puts it x pixels from the left
.RowIndex = 13
.Protection = visBarNoCustomize
Toolbar.Enabled = True
.Visible = True
End With
Visio.Application.SetCustomToolbars UI
Visio.ActiveDocument.SetCustomToolbars UI
End Sub

Resources