Word Add-in - find if dialog has focus? - vb.net

I am writing a word add-in in VB .NET (using Add-in Express, but I don't think that's relevant).
I have created shortcuts, but I only want them to take effect if the document itself is in focus, not a dialog - for example, "Find and replace" or any other.
How do I determine if a dialog has focus?
The "Selection" property of the application points to the selection in the document, even if a dialog is currently selected. I can't find any "HasFocus"-equivalent property anywhere either. I'm running out of ideas :o)
Thanks!

This workaround worked for me:
My add-in keeps a reference to the handle of the most recently activated Word window, by using the GetActiveWindow API during the WindowActivate event of the Word application. This is necessary since, up until Office 2013, the Window object does not expose a handle property.
When a shortcut is triggered, I compare that to the handle of the currently active window, using the same API. If they don't match, I simply don't proceed :o)

Related

Purge an IMAP folder in Outlook 2016 using VBA

In Outlook 2016 I have connected an IMAP folder. Outlook is configured to mark items as deleted in IMAP folders. This is necessary because I want to process those items, that are marked for deletion seperately. After my macro is completed, that IMAP folder should be purged automatically.
In Outlook there is the ribbon "Folder" with the group "purge". In there is the menu "delete" with an menu item to purge the current folder. I can't find a way to execute the function from a VBA macro.
For Outlook 2010 there is this solution:
http://www.vboffice.net/en/developers/purge-deleted-imap-messages/
In Outlook 2016, the findControl method does not find the required control.
Is there any way to purge that folder?
Best solution would be some kind of API function.
Second best would be to simulate a click event on the control of the ribbon. "CommandBars" seems to contain some sort of context menus but not the ribbon controls.
Is it possible to define custom commandbars with standard controls in it? The control id seams to be still "12771".
I very briefly looked into the UI Automation toolkit but have found no good example of how to access the ribbon of a specific application.
Alternatively: can I get access to controls of the quick access toolbar? Adding the correct purge folder control to the quick access toolbar would rely on the user to click on that button at the right moment.
For buttons you can add to a ribbon or the QAT, the idMso can be seen at the end of the text when hovering over the command.
Sub PurgeFolder_Button_idMso()
' For buttons you can add to a ribbon or the QAT,
' the idMso can be seen at the end of the text when hovering over the command.
ActiveExplorer.CommandBars.ExecuteMso ("PurgeFolder")
End Sub

VSTO Addin - Own button type like "ShapeWidth" and "ShapeHeight"

I am currently working on an PowerPoint Addin and I am currently finishing the Ribbon. I'd like to create a control to change the size of objects that looks similar to the inbuild button "ShapeWidth" and "ShapeHeight":
However, I couldn't find the correct control type and hence I built the below by myself using an editbox and buttons. Although it's working I find the other style way cleaner but I haven't found the correct type.
Not all Ribbon X (Office Fluent UI) control types that Microsoft uses in its Office applications are made available to third-party developers. This, I'm afraid, falls into that category.
You should probably be able to get the image, at least, by specifying the idMso for the built-in control.

Late binding object to existing instance via handle in VBA

This question pertains to VBA Internet Explorer automation
What I have is an Internet Explorer window that loads a dialog box with buttons when the page loads.
Here are the givens:
The dialog box is of the "Internet Explorer_TridentDlgFrame" class
It isn't considered an app window like its originating IE window.
Its "form" and button "ID's are obtainable but not within DOM of the originating IE document
So, referencing said IDs to interact with them does not work.
Similarly, injecting JavaScript attached to its buttons does not work
The dialog box can't be bypassed by closing it. A "continue" button must be pressed.
So I figured I'd use another method:
If I cycle through all windows, I can obtain the dialog box's window handle via its window title.
With that said, is there a way to late-bind "Object A" to the dialog box's window via handle?
And if so, is it then possible to late-bind "Object B" to "Object A's" document/form in order to interact with it?
This would be like accessing an existing instance of an open Excel workbook and late binding an object to it in order to interact with the workbook itself.
Note: I have used Selenium in the past, and while it may be a better solution to get around this dialog box, translating the rest of my (already) written program into Java would be a nightmare.
Note2: Using winHttp or other "headless" methods isn't an option
Any help is appreciated! Thanks!

Run a 3rd party Excel Add-in from VBA

I want to programatically run a 3rd party Add-in with VBA alone. Im using Excel 2010
Ideally, I'd like to be able to call the functions individually however I don't have any access to the code of the Add-in (it doesn't even appear as a password protected VBA Project it did in Excel 2003).
Hours of Googling has told me this was possible in earlier versions of Excel, either through Application.CommandBars("Add-Ins").Controls("Custom Button").Execute or CommandBars.ExecuteMso("Custom Button") - AFAIK, the latter now only works with in-built functions.
This custom button also appears in the 'Right-Click' menu so could possibly be run with some sort of SendKeys implementation. This is however, clearly far from ideal.
I'm pulling my hair out over this - any help would be greatly appreciated.
Depending on how it has been added to the menu, this may work:
Application.Commandbars("Cell").Controls(Application.Commandbars("Cell").Contro‌​ls.Count).Execute
which simply executes the last control. Also this should work by the control caption:
Application.Commandbars("Cell").Controls("the button caption").Execute

Excel Interop in .Net - Catch VBA errors?

Similar to Errors when calling certain Excel VBA macros from C#, I want to implement an error handler that will catch VBA errors and return gracefully.
Basically, I'm opening up an arbitrary macro workbook in Excel, and if the macro fails (i.e. you get a VBA error dialog with "end" and "debug" buttons), I want my application to handle it internally instead of using the VBA's dialogs. This is primarily so that a buggy book won't freeze processing (I'm using a queue of sorts to process multiple workbooks).
I already know about the DisplayAlerts=False property, and On Error Resume Next inserted into each routine, but I don't want to need to inject this into everything (especially since some of these workbooks may already modify the mentioned settings and reset them mid-routine!)
My best idea so far is to write up some sort of process watcher that looks for windows titled "Microsoft Visual Basic" and sendkey an "E" (to click the end button).
I've read in a few places that there's a property called Application.BreakOnVBAError that can be set to False, but it doesn't seem to exist in the interop assembly.
With the help of pinvoke.net and MWinAPI, I've been able to enumerate all of the windows created under Excel's process ID, which I can then drill down into to discover the elements of the dialog (if it's a dialog such as the Microsoft Visual Basic dialog).
Since it's an automated instance, the main window is hidden so the only thing that should show up are message boxes and the VBA error box, which I can then send keyboard input messages to to End the script when necessary.
Of course, this method never hits the "Debug" button so I should never see the VBE windows, but just in case I handle that too.
Source code available upon request?