I've got a simple test .pptm file to try to debug the failure of the AfterShapeSizeChange event in PowerPoint 2013 (15.0.4659.1001) on Windows 7 x64.
In a standard module:
Public EH As New ClassEH
' Run to initialise PowerPoint application events
Sub InitApp()
Set EH.App = PowerPoint.Application
End Sub
In a class module with name ClassEH:
Public WithEvents App As PowerPoint.Application
Private Sub App_WindowSelectionChange(ByVal Sel As Selection)
Debug.Print "App_WindowSelectionChange"
End Sub
Private Sub App_AfterShapeSizeChange(ByVal shp As Shape)
Debug.Print "App_AfterShapeSizeChange"
End Sub
After running InitApp, the WindowSelectionChange event is firing as expected with a selection change but the AfterShapeSizeChange event is not firing after changing the size of a shape on the slide.
This event was added in PowerPoint 2013 as per this MSDN article:
http://msdn.microsoft.com/en-us/library/office/jj227375(v=office.15).aspx
Any ideas?
The shape size change fires OK here Jamie. I have 2013 Pro (msi version)
Related
I have created a VSTO addin for Word 2016
On a tab in Word I have a number of buttons, but I only want to show some buttons if the name of the document or another property is a certain value.
But I cannot get it to fire when a document is opened.
Am I missing something?
Imports Microsoft.Office.Interop.Word
Public Class ThisAddIn
Dim wordApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application")
Private Sub ThisAddIn_Startup() Handles Me.Startup
End Sub
Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown
End Sub
Private Sub Application_DocumentOpen(Doc As Document) Handles Application.DocumentOpen
MsgBox("opened")
If wordApp.ActiveDocument.BuiltInDocumentProperties("Title").Value = "Posting Slip - Client Receipt" Then
PostClientReceipt.Visible = True
End If
End Sub
End Class
First of all, there is no need to get the Word Application running instance in the following way from your VSTO Word add-in:
Dim wordApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application")
Instead, you can use the Application property of the add-in class. For example:
Globals.ThisAddIn.Application
Second, the DocumentOpen event is fired before the add-in is loaded when you double click the file. Since Word 2010, the Word startup behavior is changed, VSTO runtime waits for Word to be ready before firing the ThisAddIn_Startup event. In this scenario by that time the DocumentOpen and WindowActivate events are already fired.
You may find the DocumentOpen and WindowActivate events do not fire on Word 2010 thread helpful.
As a possible workaround at startup you may check for any opened document and call the DocumentOpen event handler programmatically.
I am trying to make a shape object in Visio (2013 or later) open a local document through VBA when selected.
I have tried using the code below to get the event to create a msgbox thinking I would later change the msgbox functionality out to open a document but the msgbox won't display when I select a shape.
Private WithEvents OrgChartApp As Visio.Application
Private Sub OrgChartApp_SelectionAdded(ByVal Selection As IVSelection)
If Selection.Count = 1 Then
MsgBox "Hello"
End If
End Sub
Using App_DocumentBeforeSave (in Class1), and Document_New with Call Register_Event_Handler in ThisDocument, I cannot use my usual code to open up a Userform (Autoopen, AutoNew).
How should I place code to allow both DocumentBeforeSave code as well as Userform initiation code?
Sorry for not being more clear. In the template with the working code for changes to a document BeforeSave:
In Microsoft Word Project ThisDocument:
Private Sub Document_Open()
Call Register_Event_Handler
End Sub
Private Sub Document_New()
Call Register_Event_Handler
End Sub
In Modules, Module1:
Dim X As New Class1
Public Sub Register_Event_Handler()
Set X.App = Word.Application
End Sub
In Class Modules, Class1:
Public WithEvents App As Word.Application
Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
‘
‘ Then follows my clean up code in this Sub, which also Calls Subs, which are also placed in Class1 under this Sub.
All this works as intended. However, I wrote the code above for all of the Templates we use to generate medical reports, many of which also bring up UserForms upon opening. However, all my templates with UserForms have this code in In Microsoft Word Project ThisDocument to open the Userform whenever the document or template opens:
Sub Autoopen()
Options.ButtonFieldClicks = 1
MACROS.Show
With MACROS
.Top = Application.Top
.LEFT = Application.LEFT
End With
End Sub
Private Sub Document_New()
Options.ButtonFieldClicks = 1
MACROS.Show
With MACROS
.Top = Application.Top
.LEFT = Application.LEFT
End With
End Sub
I cannot get the Userform opening code to work with the initializing code for the DocumentBeforeSave – for one thing they both use Sub Document_New(). I tried changing the Userform opening code to Sub AutoNew(), which still didn’t function.
The Userforms without the DocumentBeforeSave code open fine, and the DocumentBeforeSave code works OK without the Userform code. How do I get both to work in the same project? BTW - the error that occurs: it won't save the project/document.
Thank you.
AutoOpen, AutoNew and AutoSave belong in a "plain vanilla" module. It looks like you've tried putting them in a class, which won't work...
I am making an Excel 2010 AddIn and am trying to add some code to the Application Event Application_WorkbookActivate(ByVal Wb As Workbook)
However, my Module inside the AddIn cannot have these events. I tried making a ClassModule with the events and then initializing it on the AddIn's load. I got this idea after reading this.
Here is what I have in the AddIn's Class "EventListener:"
Dim WithEvents app As Application
Private Sub app_WorkbookActivate(ByVal Wb As Workbook)
Debug.Print "Workbook Activated:"; Wb.Name
Call MyAddInModule.RefreshVisibility
End Sub
And here is what I have in the module "MyAddInModule:"
Public Listener As EventListener
Public myRibbonUI As IRibbonUI
' Ribbon callback : runs when ribbon is loaded
Public Sub onLoadRibbon(ribbon As IRibbonUI)
' Get a reference to the ribbon
Set myRibbonUI = ribbon
Debug.Print "Ribbon Reference Set"
Set Listener = New EventListener
End Sub
Public Sub RefreshVisibility()
myRibbonUI.Invalidate
Debug.Print "Ribbon Invalidated"
End Sub
Unfortunately this did not work. It is not possible to declare a WithEvents object in the normal coding module and the Class Object didn't fire as expected.
Is there a better way to have Application Events be coded into an AddIn?
I have a VBA addin which I want to run every time someone opens a document. It's working fine for opening existing documents (AutoOpen) and creating new documents from the File > New menu (AutoNew) but when I just open Word up for the first time, neither of these events are firing. The only event I can seem to hook into is the AutoExec event and this is not great as the document doesn't exist yet so ActiveWindow is null.
Can anyone help?
Sub AutoNew
MsgBox "New"
End Sub
Sub AutoOpen
MsgBox "Open"
End Sub
Sub AutoExec
MsgBox "Exec"
End Sub
I would start with DocumentOpen and NewDocument. An additional level of complexity exists if you need to support ProtectedView documents; Word triggers a different event. I've found that if I try to check for that event (and it doesn't occur) it raises an error. I didn't had much luck and eventually it wasn't worth the time I was spending. I've posted an example below of some code that opens the Style Pane when a document is opened or a new one created (assuming the add-in is loading) and expands the style margin in draft view if not already expanded.
In my UI module:
Dim X As New clsAppEvent 'This is in the declarations
Public Sub OnRibbonLoad(objRibbon As IRibbonUI)
Do While Documents.Count = 0
DoEvents
Loop ' I find this useful as sometimes it seems my ribbon loads before the document.
Call Register_Event_Handler
' Other stuff
End Sub
Private Sub Register_Event_Handler()
Set X.App = Word.Application
End Sub
Then, in a class module I call clsAppEvent:
Option Explicit
Public WithEvents App As Word.Application
Private Sub App_DocumentOpen(ByVal Doc As Document)
App.TaskPanes(wdTaskPaneFormatting).visible = True
End Sub
Private Sub App_NewDocument(ByVal Doc As Document)
App.TaskPanes(wdTaskPaneFormatting).visible = True
End Sub
Private Sub App_WindowActivate(ByVal Doc As Document, ByVal Wn As Window)
If Wn.StyleAreaWidth <= 0 Then
Wn.StyleAreaWidth = 60
End If
End Sub
Other than the caveats I've noted above, the one problem I've had is if a user has Auto code in their Normal template as well. This has only come up once so I haven't investigated it.
I wish I could find the site where I learned about this (and from which the Register_Event_Handler was derived. If I find it I'll add a comment.