How to get the WordEditor property of the currently selected email in an explorer reading pane? - vsto

I implement context menus for emails, this includes the explorer reading pane. To get the text under the mouse at the time of a right click I use the WordEditor. I do not think there is any other way of finding out where the mouse has clicked.
'_olItem comes from the current selection
olInspector = CType(_olItem.GetInspector, Outlook.Inspector)
wDoc = CType(olInspector.WordEditor, Word.Document)
'then go off and work with word
For the Explorer reading pane is the only way to get the WordEditor by first calling GetInspector?
One reason for asking is that I see that for Inline responses Outlook has the ActiveInlineResponseWordEditor property.
My addin also listens for new inspectors
Private Sub oInsps_NewInspector(ByVal Inspector As Microsoft.Office.Interop.Outlook.Inspector) Handles oInsps.NewInspector
'do something here with this inspector
End Sub
If I cannot avoid calling GetInspector to get the word editor then is there a property of the inspector at the time the newinspector event fires to tell me that this inspector is actually from the reading pane and from me calling GetInspector?

Use Explorer.PreviewPane.WordEditor.

Related

Automatically Closing Outlook Windows via Script/Macro

I'm hoping someone on here can point me in the right direction with some expertise. I have a situation with a client's outlook where we need to close any open draft windows after a new message/draft is opened. Ideally after the 4th new window has opened.
Any "new message" window that opens after this we need the script to close 1st window that opened. Either killing the process, or something similar.
Recently have been looking into Outlook macros, but am unsure if they will help in this instance. (Maybe they are?). Being more familiar with Powershell, figured we could start there.
Looking to get help writing a powershell script, macro, etc to do this on the backend.
The Outlook object model provides all the required events, methods and properties for that. So, VBA macros is the right choice if you don't need to distribute the solution on multiple machines. Otherwise, you need to consider developing a COM add-in instead (for example, a VSTO based one should work for you). See Walkthrough: Create your first VSTO Add-in for Outlook for more information.
You can handle the NewInspector event which is fired whenever a new inspector window is opened, either as a result of user action or through program code. The event occurs after the new Inspector object is created but before the inspector window appears.
You can also check the number of opened inspector windows in Outlook by using the Inspectors.Count property which returns a long indicating the count of objects in the specified collection.
Finally, the Inspector.Close method closes the Inspector and optionally saves changes to the displayed Outlook item. For example, a VBA sample which closes the active inspector instance:
Sub CloseItem()
Dim myinspector As Outlook.Inspector
Dim myItem As Outlook.MailItem
Set myinspector = Application.ActiveInspector
Set myItem = myinspector.CurrentItem
myItem.Close olSave
End Sub

Slow opening of any email after set value for an object on Application_ItemLoad

Basically I am using below code to run macro after I manually open a specific email.
The code and macro works ,but I noticed there is a slowness while opening of any email on outlook.
After many tests, I find out this line is the cause of issue:
Set MyItem = Item
And this the full code:
Option Explicit
Option Compare Text
Public WithEvents MyItem As Outlook.MailItem
Private Sub Application_ItemLoad(ByVal Item As Object)
If Item.Class = olMail Then
Set MyItem = Item 'This line cause slow opening of any email
End If
End Sub
Private Sub myItem_Open(Cancel As Boolean)
If MyItem.Subject = "Test Email" Then
'Code
End If
End Sub
Kindly How to fix this issue?
Basically I am using below code to run macro after I manually open a specific email.
You may consider using other event handlers in Outlook - for selecting in the Explorer window you may try to handle the SelectionChange event of the Explorer class which is fired when the user selects a different or additional Microsoft Outlook item programmatically or by interacting with the user interface. For opening in the separate window you can handle the NewInspector event which is fired whenever a new inspector window is opened, either as a result of user action or through program code. The event occurs after the new Inspector object is created but before the inspector window appears. Also you may handle the Inspector.Activate event which is fired when an inspector becomes the active window, either as a result of user action or through program code.
If there is a specific reason to use the ItemLoad event handler in the code you may continue using the late-binding technology and keep the item defined as object.
Also don't forget to release corresponding objects in the Unload event of the Item-level events.

Modifying default behavior of outlook buttons

Is it possible to modify the behavior of existing buttons in outlook (or generally in ms office programs)? E.g. can I make the "send mail" button show a dialog before sending a mail?
I know that you can make add ins and put them into ribbon but can you add certain behavior to existing controls?
Use Microsoft.Office.Interop.Outlook und handle the Send event. You can find the documentation here.
You have use your application reference to get the active inspector and by the active inspector you retrieve the message class:
(CType(inspector.CurrentItem, Outlook.ItemEvents_10_Event)).Send += New Outlook.ItemEvents_10_SendEventHandler(Inspector_Send)
Private Sub Inspector_Send(ByRef Cancel As Boolean)
... your code...
End Sub

VB macro to disable "Edit Document" prompt when opening a word document from sharepoint

Is there a way to disable the message (and have the document editable by default):
Server Document To modify document, click Edit Document followed by the button with text "Edit Document".
I cannot find a word setting to do this. In addition I cannot see a way to make a VB macro to do this with a key stroke. I have used a small autohotkey script to position the mouse and click this prompt, but this does not always work since it depends on the position of the window. it is impossible to use the tab key to get to this prompt.
I have to modify about 50+ documents a day from sharepoint, ideally I would like to combine this with another macro which does other automated processing for me. But I can't find a VB solution for clicking the Edit button.
Depending on your security settings (you mentioned that they were blocked), this may or may not work.
Create a new macro enabled template in your Word startup folder (usually at C:\Users[YourID]\AppData\Roaming\Microsoft\Word\STARTUP), and add a new class module. I called mine "AutoEditEnable". You can name it anything, but you'll need it to match how you declare it in the other module.
This code goes in the class:
Option Explicit
Private WithEvents app As Application
Private Sub Class_Initialize()
Set app = Application
End Sub
Private Sub app_ProtectedViewWindowOpen(ByVal PvWindow As ProtectedViewWindow)
PvWindow.Edit
End Sub
Basically, this will hook any Application events you need to - in this case the ProtectedViewWindowOpen event or the ProtectedViewWindowActivate event (either should work).
Put the following code in ThisDocument to grab a reference to it when your template loads:
Option Explicit
Private hook As AutoEditEnable
Private Sub Document_Open()
Set hook = New AutoEditEnable
End Sub
Close Word and restart it, then make sure your new template shows up as a loaded add-in.

Excel VBA hiding custom ribbon tab

I have a custom ribbon which works fine but I only want to enable it and have my add in show for certain workbooks so I check for the workbook title on load and try to use the Invalidate method if the condition is false. Unfortunately nothing happens the custom ribbon tab is still showing.
The following is my sub:
Public Sub loadMyRibbon(ribbon As IRibbonUI)
Set RibUI = ribbon
If Not workbookTitle = "My Workbook" Then
If Not RibUI Is Nothing
RibUI.Invalidate
MsgBox "Not Working"
End If
End If
End Sub
Which seems correct to me from reading through the method documentation:
Microsoft Documentation
I see my MsgBox message displayed on the screen so I know the code is executing correctly up to that point but RibUI.Invalidate doesn't hide my tab. Appreciate any pointers!
I have also tried:
RibUI.InvalidateControl "myTag"
But this also doesn't work
Ribbon.Invalidate doesn't mean the ribbon will not show. Invalidate function just tells the ribbon to invalidate and re-initialize the ribbon controls with their default/dynamic properties.
I worked with few Add-ins where the clients wanted to hide the ribbon items if users cannot pass the authentication. So in such a case, I used "GetVisible" attribute in all of my Control and then I used this code
Sub GetVisible(control As IRibbonControl, ByRef Visible)
On Error Resume Next
Visible = shouldShowOrNot
End Sub
shouldShowOrNot is a boolean variable, which I set in Ribbon Load to true if user passes the authentication. See the following image:
Now the above image is a representation of Ribbon in case User has failed the authentication. There might be a better way to do it, but i found it to be the best way so far.
Hope this helps,
Vikas B