Windows UI Automation - Not Finding Window - vba

I'm working on a VBA program to help automate a task using UI Automation, but I've run into an issue where the window of the program I'm trying to control does not show up as a child of the Root Element (desktop).
It's my understanding that all elements are children/descendants of the desktop element, so why it's not showing up is puzzling. Here is the code I'm using to see all the children of the Desktop element:
Sub test_get_windows()
Dim oAutomation As New CUIAutomation
Dim oDesktop As UIAutomationClient.IUIAutomationElement
Dim oCondition As UIAutomationClient.IUIAutomationCondition
Dim oChilds As UIAutomationClient.IUIAutomationElementArray
Dim i As Integer
Set oDesktop = oAutomation.GetRootElement
Set oCondition = oAutomation.CreateTrueCondition
Set oChilds = oDesktop.FindAll(TreeScope_Children, oCondition)
For i = 0 To oChilds.Length - 1
Debug.Print oChilds.GetElement(i).CurrentName
Next i
End Sub
I see everything except the window I'm trying to interact with. I tried the code on another computer, and I'm able to see the window.
Computer where it doesn't work:
Computer where it does work:
The AcSELerator Quickset window does not show up on the first computer but it does on the second, so I don't think it's a problem with the application. Both computers have the same version installed and are running Windows 10 Enterprise. Any ideas on what the issue could be or any troubleshooting steps I can try?

The window doesn't have to be minimized. Try veryfyng that. That's what was happening to me. Similar case and Similar routine.

Related

GetElementbyID not showing for IE.Dcoument (VB.Net)

I'm trying to make a webscraping program in Visual Studio 2019 using VB.Net Framework but I'm running into an issue. I have the below code to locate an open IE window so that I can manipulate it. The issue I'm running into is when I use IE.Document. the list that appears does not show getelementsbyid. Here is a snippet of my code, it's able to find the appropriate IE window and bring it into focus but I need to be able to manipulate the elements on the page.
I have also added the Com References
Microsoft HTML Object Library
Microsoft Internet Control
Dim SWS as new SHDocVw.ShellWindows
Dim IE as SHDocVw.InternetExplorer
For Each IE in SWS
If IE.locationName = "XXXXXXXX" Then Exit For
NExt
'To get focus on IE window
IE.Visible = False
IE.Visible = True
IE.Document.
I would appreciate any insight since I'm at a loss as to why getelementbyid won't show.
As an alternative, you can try to refer to the code example below that may help you to get/ set values.
If you want to set data then refer to the line below.
IE.Document.All.Item("fname").Value = "ABC"
If you want to fetch data then refer to the line below.
Console.WriteLine(IE.Document.All.Item("fname").Value)

All Chrome open window names in VBA

I have a macro on VBA in the process of which several Chrome windows open.
In each of the windows I need to do different actions and for this I need to switch between them.
Can I somehow get a list of the names of the open Chrome windows or their handles in the VBA?
Such methods did not work in VBA:
driver.getWindowHandles
driver.getWindowNames
In JS, you can get the window name:
window_name = driver.ExecuteScript("return window.name;")
Perhaps there is a way to get the names of all the windows and cycle through them in the VBA?
I found a way to more or less resolve this issue.
We save the name of each window to be opened in a variable
window_count = driver.Windows.Count 'count all chrome windows
i = 0
For i = 1 To window_count
If driver.Windows.item(i).Title <> "SomeTitle 1" And driver.Windows.item(i).Title <> "SomeTitle 2" Then
window_name = driver.Windows.item(i).Title 'at this moment the desired window is
'activated and you can work in it even
'without driver.SwitchToWindowByTitle
MsgBox driver.URL
End If
Next i
I think this is not the best solution to the problem, but with my knowledge it’s better to do so far I can’t

VBA office 2016 windows7/10 difference: Set IXMLDOMNodeList = DOMDocument60.childNodes

I've got an issue with a piece of VBA code that runs fine on a windows 7 machine, but doesn't work on windows 10. My VBA skills aren't good enough to figure this one out.
This part of the code in run in a Excel class and is used to load a xml file and return the xml as a class.
Public Function GetDomNodeList(ByRef log As Logger) As MSXML2.IXMLDOMNodeList
Dim domdocument As New MSXML2.DOMDocument60
'Open file for handling
domdocument.async = False
domdocument.Load (strThisFilePath)
'Call the helper sub to do the actual work:
Set GetDomNodeList = domdocument.childNodes
End Function
When debugging it tells me there is a type mismatch. I've tried to debug the issue, but I'm running short on knowledge here. My main question is why this works on windows 7 and why not on windows 10 running the same office version (2016) VBA7.1
After a lot of trials I found a solution. Export the class, remove it and import the class again and all magically works again. I'm baffled by this.

Close MS Project using VSTO

I have a VSTO on MS Project. I use VB.NET. What I need is when I press the button I created on the ribbon, it will perform some codes which will update the info of some task, however, I would need to close the MS Project automatically. I tried application.FileCloseEx(), but it only closes the file, the MS Project is still loaded. I need similar to clicking the x button of the window.
Thanks,
Gilbert
If your MS Project application object is represented by "appMSProject" then it's as simple as:
appMSProject.Quit
OR say in a macro running under Project:
Application.Quit
Here's how I do it in VBA from Excel or Access. As far as I can tell the objects & methods are the same in VB.NET. Bottom line is that I create an instance of the MS Project object which starts the app & opens a file, execute some work, close the file, then destroy the MS Project object by setting it to Nothing. That has the effect of closing the app. You can also use "appMSProject.Quit" followed by setting it to Nothing. Frankly the 2nd option looks more orderly & easier to understand in code. Anyway, here's a sample of the way I do it:
Dim appMSProject As MSProject.Application
Dim prjPrj As MSProject.Project
Dim strPrjFile As String
strPrjFile = "C:\where_is_my_file\file_name.mpp"
Set appMSProject = New MSProject.Application
appMSProject.FileOpenEx Name:=strPrjFile
Set prjPrj = appMSProject.ActiveProject
'''Do something in here with the prjPrj
'Close the file, in my case w/o saving
appMSProject.FileCloseEx pjDoNotSave
'Destroy the objects
Set prjPrj = Nothing
Set appMSProject = Nothing
FYI - In this example I'm doing background work so I don't show the app. I also use "early binding".
Here's an MSDN example that does show the app with more info on early -vs- late binding - https://msdn.microsoft.com/en-us/library/office/ff865152.aspx

How to bring word application to the front in vb.NET

I am using winforms and when I try to open a word document programmatically it goes behind all the opened windows randomly. How can I bring it to the front? Following is my code.
Dim pAppWord As Object = CreateObject("Word.Application")
'//do my stuff
pAppWord.Visible = True
pAppWord.Application.Activewindow = True
'//pAppWord.Active() found this online but throws an error
pAppWord.WindowState = vbNormal
Thanks!!
What worked for me was
pAppWord.Activate()
This doen't seem trivial... maybe this one helps: http://social.msdn.microsoft.com/Forums/pl-PL/vblanguage/thread/fd9215cf-0893-4c61-886c-5d22fa7c2055
If you've added or opened a document (e.g. wordOb.Documents.Add) then you can simply pass wordOb.ActiveWindow.Hwnd to the Windows API function SetForegroundWindow.