Powerpoint VBA App_SlideShowBegin - events

In order to use the SlideShowBegin event in Powerpoint, you have to have a Class Module configured the following way:
Public WithEvents App As Application
Private Sub App_SlideShowBegin(ByVal Wn As SlideShowWindow)
MsgBox "SlideShowBegin"
End Sub
Then, inside of a non-class module, you have to create an object of that type and set the App to Application.
Dim X As New Class1
Sub InitializeApp()
Set X.App = Application
End Sub
Now, the only issue I have is, if you don't manually called InitializeApp with the Macro Menu in Powerpoint, the events don't work. You have to call this sub before anything can called at the beginning of a slideshow INCLUDING this sub.
How can I go about calling this sub before running my powerpoint? Is there a better way to do this?
EDIT:
I've tried using Class_Initialize but it only gets called once it is first used or you make a statement like Dim X as Class1; X = new Class1

Usually event handlers are installed as part of an add-in, where you'd initialize the class in the Auto_Open subroutine, which always runs when the add-in loads. If you want to include an event handler in a single presentation, one way to cause it to init is to include a shape that, when moused over or clicked fires a macro, which inits your event handler and goes to the next slide.

Answering to an old question, but I hope my solution might helpt somebody ending up at this question.
The general advice for this issue is using a plug-in or placing some element on the slide and when that is clicked or hovered perform the initialization. Both are not always desired so I have the following approach:
In some module:
Dim slideShowRunning As Boolean
-----------------------------
Sub SlideShowBegin(ByVal Wn As SlideShowWindow)
' Your code for start-up
End Sub
-----------------------------
Public Sub OnSlideShowPageChange(ByVal Wn As SlideShowWindow)
If TypeName(slideShowRunning) = "Empty" Or slideShowRunning = False Then
slideShowRunning = True
SlideShowBegin Wn
End If
End Sub
----------------------------
Public Sub OnSlideShowTerminate(ByVal Wn As SlideShowWindow)
slideShowRunning = False
End Sub
For me this works perfectly. NOTE I am by no means a vba expert, actually I might have less than 50 hours of vba programming (maybe only 8 in powerpoint). So this might be an horrible solution. I don't know, but for me it works so I liked to share.

In fact, OnSlideShowPageChange runs when slideshow begins. Just make sure it doesn't work in the subsequent page changes if not needed using a global variable. See answer from C. Binair for details.

Related

LostFocus() event not triggered after application installation

I have simple code in the LostFocus event of a textbox control
that changes the text to upper case:
Private Sub txtIntegration_LostFocus()
If Trim(txtIntegration.Text) <> "" Then
txtIntegration.Text = UCase(Trim(txtIntegration.Text))
End If
End Sub
When the application is installed, the code to uppercase the text does not work in.
I'm creating the installer with the "Package and Deployment Wizard" of Visual Basic 6.
As a troubleshooting step and potential solution for what you are trying to achieve, you can force the text to uppercase as it is being typed:
Private Sub txtIntegration_Change()
Dim iPosition As Integer
With txtIntegration
iPosition = .SelStart ' Save cursor position
.Text = UCase$(.Text)
.SelStart = iPosition ' Restore cursor position
End With
End Sub
You can at least try this approach and see if it works once installed. There should be no reason the installation process breaks any of your code.
The solution was, when you add or remove the program, the removal program does not uninstall the .EXE completely
Simply delete the Directory and solve it!

VB6 - User control/ActiveX control - how to get parent dimensions

I'm writing a vb6 user control (for my sins) and all has gone well up until now. I'm struggling to get the height and width of the parent/container.
The User-control is exported as an activeX control (.ocx), registered on the target machine using Regsvr32 and then embedded into an existing app.
I don't have access to the existing apps code so I can't inject anything that way.
I've tried accessing the UserControl.Parent object but receiving errors.
Does anyone know how to do this or can set me down the right path. My main language is c# so be kind to me please.
Most likely you are trying to fiddle with Parent before the control has been sited. Doing this instead works just fine:
Option Explicit
Public Sub Update()
'Call from parent container's Resize event handler, etc.
Cls
With Parent
Print .ScaleWidth; " × "; .ScaleHeight
Print .Width; " × "; .Height
End With
End Sub
Private Sub UserControl_Initialize()
AutoRedraw = True 'We're using Print here.
End Sub
Private Sub UserControl_InitProperties()
Update
End Sub
Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
Update
End Sub

How to save an Excel file via VB 2010 without any dialogs (such as "save as")

I am trying to Save an Excel file Via VB 2010, and I have these questions
How can I disable the "save as" dialog? I tried things such as only "save" instead of "save as", but it doesen't work...
After I saved the file (using the save as) I can't Delete it... (I tried closing the excel file, Visual basic etc...) all i get is an error saying it is allready open in excel...
Is there a way to make VB show me the tips for writing the excel stuff (Ie - when I write messagebox. - it pops up "Show" for help. how can I enable this for excel code [worksheets.cells. ect.])
the connection:
Sub Connect()
' Connect to the excel file
oExcel = CreateObject("Excel.Application")
'Devine the workbook
oBook = oExcel.workbooks.open("e:\Words\Heb.xls")
End Sub
the saveas:
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
oExcel.SaveAs(oExcel.Path & ".xls")
End Sub
Thanks a lot
I think Inafiziger has solved your main issue, it should be a vanilla Save.
As it was unclear to me exactly what your are doind (ie Visual Studio/VB/BA) then
On (1)
I thought it worth clarifying that you can use code inside the ThisWorkbook module to detect and handle a SaveAs if you are providing users with a choice. This Event detects the SaveAs and cancels it
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If SaveAsUI Then
MsgBox "You cannot use SaveAs to save this file", , "Save Cancelled!"
Cancel = True
End If
End Sub
This code can be programmatically added to your target workbook but I doubt you would need to resort to this given you should be able to run the simple Save.
On (3)
You need to use Early Binding to get the benefit of intellisense. You are currently use late binding with oExcel = CreateObject("Excel.Application"). A commonly used approach is to write the code and get it working with early binding, then converting it to late binding for final code publication.
Conditional Compilation (see comment at bottom) can be used to switch between the two binding methods within the same code.
You should be saving the workbook. e.g. oBook.Save.
If you create a new file, you will need to use SaveAs with a valid filename in order to save it the first time.

How do I locate a Word application window?

I have a VB.net test application that clicks a link that opens the Microsoft Word application window and displays the document. How do I locate the Word application window so that I can grab some text from it?
You can use the Word COM object to open the work document and then you manipulate it. Make sure to add a reference for Microsoft Word first.
Imports System.Runtime.InteropServices
Imports Microsoft.Office.Interop.Word
Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strFileName As String
Dim wordapp As New Microsoft.Office.Interop.Word.Application
Dim doc As Microsoft.Office.Interop.Word.Document
Try
doc = wordapp.Documents.Open("c:\testdoc.doc")
doc.Activate()
Catch ex As COMException
MessageBox.Show("Error accessing Word document.")
End Try
End Sub
End Class
The doc object is a handle for the instance of Word you have created and you can use all the normal options (save, print etc). You can do likewise with the wordapp. A trick is to use the macro editor in Word to record what you want to do. You can then view this in the Macro Editor. This give you a great starting point for your VB code.
Also, be sure to dispose of the Word COM objects at the end.
I've done something similar with a SourceSafe dialog, which I posted on my blog. Basically, I used either Spy++ or Winspector to find out the window class name, and make Win32 calls to do stuff with the window. I've put the source on my blog: http://harriyott.com/2006/07/sourcesafe-cant-leave-well-alone.aspx
Are you trying to activate the word app? If you want full control, you need to automate word from your vb.net app. Check here for some samples: 1, 2

Prevent a TreeView from firing events in VB6?

In some VB6 code, I have a handler for a TreeView's Collapse event:
Private Sub MyTree_Collapse(ByVal Node as MSComCtlLib.Node)
This is called whenever a node in the tree is collapsed, whether by the user or programmatically. As it turns out, through some roundabout execution, it may happen that this handler will wind up telling a node to collapse, leading to infinite recursion.
I can think of multiple ways to skin this cat, but what seems simplest to me is to tell the TreeView not to raise events for some period of time. I can't find a simple call to let me do this, though. Has anyone successfully done this, or do I need to keep track of state in some other manner so I can respond appropriately when recursive events come along?
#Phil - I came to the same conclusion. My implementation of MyTree_Collapse now looks something like this (where m_bHandlingCallback is a member variable):
Private Sub MyTree_Collapse(ByVal Node as MSComCtlLib.Node)
If m_bHandlingCallback Then Exit Sub
m_bHandlingCallback = True
DoSomeStuff
m_bHandlingCallback = False
End Sub
Another way in VB6 is to have an alternate WithEvents reference to the control:
Private WithEvents alt as TreeView
and in Form_Load:
Private Sub Form_Load()
Set alt = MyTree
End Sub
Now alt will receive lots of events like this:
Private Sub alt_Collapse(ByVal Node as MSComCtlLib.Node)
Set alt = Nothing
'DoSomeStuff'
Set alt = MyTree
End Sub
But, during the DoSomeStuff, the events are unhooked - which also applies to all other event Subs for alt without the need for them to have intrusive changes.
I think that like many events in VB, it can't be switched off.
Just set a boolean flag as you've suggested.
I would declare the flag variable as STATIC in the Sub. This avoids making the variable global and makes it keep its value between calls.

Resources