Opening a Document from Visio with a SelectionAdded Event - vba

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

Related

Document_Open() does not work in other files

I am writing a VBA project containing 1 module (m1) and 1 userform (uf).
In "ThisDocument" I am calling a public sub from "m1" that initializes a collection I then refer to in the userform. This works perfectly fine until I deploy this project to other files.
I save the file as a .dotm in the %Appdata%/Microsoft/word/startup folder so I can use the project in all of my word files. But as soon as I try to use my project in other files the userform opens itself as designed but the collection is empty.
What could be the problem here?
Manually calling the initialization method from the userform works fine.
'----------------------------------------------ThisDocument
Private Sub Document_Open()
initBetaCollection
End Sub
'----------------------------------------------m1
Option Explicit
Public beta As Collection
Sub initBetaCollection()
Set beta = New Collection
beta.Add Array("0041", "A"), Key:="0041"
'...
End Sub
'----------------------------------------------uf
Option Explicit
Private Sub txtSearch_Change()
Dim arr As Variant
Dim search As String
'Defining the textinput as "search"
search = txtSearch.Value
For Each arr In beta
If search <> "" Then 'No empty search field
If arr(1) Like "*" & search & "*" Then 'Match found
lbResults.AddItem arr(0)
End If
End If
Next
End Sub
I get the: Run Time Error '424' object required
The problem with using Document_Open in the ThisDocument class or a macro named AutoOpen in a regular module is that both execute specifically for this document (or documents created from the template), only.
In order to have an application-level event, that fires for all documents opened, it's necessary to work with application-level events.
For this, first a class module is required. In the class module, the following code is needed:
'Class module named: Class1
Public WithEvents app As Word.Application
Private Sub app_DocumentOpen(ByVal Doc As Document)
'MsgBox Doc.FullName & ": on Open"
'Code here that should fire when a document is opened
'If something needs to be done with this document, use
'the Doc parameter passed to the event, don't try to use ActiveDocument
End Sub
Then, in a regular module, an AutoExec macro can be used to initialize the class with event handling. (AutoExec: fires when Word starts and loads a template with a macro of this name.)
Option Explicit
Dim theApp As New Class1
Sub AutoExec()
'MsgBox "AutoExec"
Set theApp.app = Word.Application
End Sub
'Sub AutoOpen()
' MsgBox "Open in AutoOpen" - fires only for this document
'End Sub

VBA userform now showing in quick access toolbar in excel 2016

I have a userform which I cannot select in the quick access toolbar to add to the ribbon.
All of the other macros I have are visible and can be selected to be added to the quick access toolbar.
I have the name of the userform and command buttons in the userform code matching the name listed within their reposective properties windows.
I have been able to add userforms to previous versions of excel so I am not sure if anything has changed for excel 2016.
Sub userform1_show()
UserForm1.Show
End Sub
Private Sub CommandButton1_Click()
Load.Linux_unix_scan_tab_only
Linux_unix_scan_tab_only.Linux_unix_scan_tab_only
End
End Sub
Private Sub CommandButton2_Click()
Load.unix_SSHkey_tabs
unix_SSHkey_tabs.Linux_unix_scan_and_SSH_Key_trust_tabs
End Sub
Private Sub userform_click()
End Sub

Cannot run the macro… the macro may not be available in this workbook VBA

My question at hand is why do these Macros for creating Menu Command buttons work on my personal computer (Excel 2010/2016), but generate macro may not be available messages on my work one. My only thoughts are it's relative to the bit /configuration of my work laptop, some various complier issue tied to old VBA libraries, assignment references being missed, & something random related to private Subs.
Macros in This Workbook. The coding is done in This Workbook, Module, & User Form (Command Button) . The VBA essentially programs a new button on the Excel Ribbon (Menu Commands) that when you click it an user form will generate. However, this does not happen with the VBA error. In overall, the coding is very sound. Therefore, I believe the errors I'm getting are tied to system/configuration items.
Option Explicit
Private Sub Workbook_BeforeClose(Cancel As Boolean)
RemoveMenuItem
End Sub
Private Sub Workbook_Open()
AddMenuItem
'frmMain.Show
End Sub
MODULE
Option Explicit
Sub Selectfile()
Dim wb As Workbook
Set wb = Workbooks.Open("C:\Users\brian\Desktop\practice file2.xlsm")
End Sub
Sub AddMenuItem()
Dim cmbWorkSheetMenuBar As CommandBar
Dim cmbControl As CommandBarControl
Set cmbWorkSheetMenuBar = Application.CommandBars("Worksheet Menu Bar")
Set cmbControl = cmbWorkSheetMenuBar.Controls.Add(Type:=msoControlButton, temporary:=True) 'adds a menu item to the Menu Bar
With cmbControl
.Caption = "Show Form" 'names the menu item
.OnAction = "ShowTheForm" 'runs the specified macro
.FaceId = 1098 'assigns an icon to the dropdown
End With
End Sub
Sub RemoveMenuItem()
On Error Resume Next 'in case the menu item has already been deleted
Application.CommandBars("Worksheet Menu Bar").Controls("Show Form").Delete 'delete the menu item
End Sub
Sub ShowTheForm()
frmMain.Show
End Sub
UserForm Click Code
Option Explicit
Private Sub btnOne_Click()
Application.FindFile
End Sub

Document_New event on launching Word

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.

Calling a macro from a button in edit mode while in PowerPoint

I'm trying to write a vba macro that can be called in edit-mode in PowerPoint 2007-2010.
I can easily add a Command Button to a presentation. However, this button can only be clicked to trigger the vba macro while in slideshow mode.
However, what I would like to do is have this button trigger the associated vba macro while in edit mode. Clicking on it in edit mode allows me to change its size etc, but it doesn't call the macro.
In Excel on the other hand, I get exactly the expected behaviour when I insert a button -> clicking on it calls the vba action.
So how can I create a button (or other element that acts the same way) that calls a vba macro during edit view in PowerPoint. The only way I can think of is using a ribbon action, however this is unpractical in this case, because the macro will modify a shape that is associated with the button and there might be several of these shapes per slide that should each have their own button.
The only way I can think of is using a ribbon action, however this is unpractical in this case, because the macro will modify a shape that is associated with the button and there might be several of these shapes per slide that should each have their own button.
Depending on what you're trying to do, a ribbon button that launches a macro might be quite practical. The macro could operate on the current selection (and test the current selection to ensure that it's something appropriate).
With ActiveWindow.Selection.ShapeRange
' operate on the currently selected shapes
End with
just answer it a some where else also
it is possible to do so all you need is download this file
http://www.officeoneonline.com/eventgen/EventGen20.zip
install it
create a class module
paste this code
Option Explicit
Public WithEvents PPTEvent As Application
Private Sub Class_Initialize()
End Sub
Private Sub PPTEvent_WindowSelectionChange(ByVal Sel As Selection)
If Sel.Type = ppSelectionShapes Then
If Sel.ShapeRange.HasTextFrame Then
If Sel.ShapeRange.TextFrame.HasText Then
If Trim(Sel.ShapeRange.TextFrame.TextRange.Text) = "Text inside your shape" Then
Sel.Unselect
yoursub
End If
End If
End If
End If
End Sub
insert a new module
paste this code
Dim cPPTObject As New Class1
Dim TrapFlag As Boolean
Sub TrapEvents()
If TrapFlag = True Then
MsgBox "Already Working"
Exit Sub
End If
Set cPPTObject.PPTEvent = Application
TrapFlag = True
End Sub
Sub ReleaseTrap()
If TrapFlag = True Then
Set cPPTObject.PPTEvent = Nothing
Set cPPTObject = Nothing
TrapFlag = False
End If
End Sub
Sub yoursub()
MsgBox "Your Sub is working"
End Sub
Now run TrapEvents and whenver you will click shape with that text in it your sub will run
Credits to the person who wrote this http://www.officeoneonline.com/eventgen/eventgen.html