I want to hide the excel file behind the Userform.
Is it possible that when I move the Userform with cursor, then Excel application behind the UserForm also moves?
The application has properties you can define
Top
Left
Width
Height
used like:
Application.Top = 0
Play around with this in various events in the userform until you find what youre after.
I strongly suggest learning how to capture the userform's values and then assigning them to the application.
Extending the answer by #DougCoats - its important to set the Application.WindowState to xlNormal in order to manipulate Application properties such as Top and Left etc. See the code below - you need to include a Module and UserForm in your workbook:
Module1
Option Explicit
Sub TestForm()
UserForm1.Show
End Sub
UserForm1
Option Explicit
Private Sub UserForm_Initialize()
HideApplicationBehindUserForm
End Sub
Private Sub UserForm_Layout()
HideApplicationBehindUserForm
End Sub
Private Sub HideApplicationBehindUserForm()
With Application
.WindowState = xlNormal
.Top = Me.Top
.Left = Me.Left
.Height = Me.Height
.Width = Me.Width
End With
End Sub
The Initialize event will hide the Excel application behind the UserForm when you open it. The Layout event will move the application behind the UserForm when you move the UserForm around with your mouse. The code is the same in both circumstances - HideApplicationBehindUserForm.
I see a little problem with my Excel where the application is slightly off to the border of the UserForm:
Related
Is it possible to get the same effect using ThisWorkbook.Application.Visible = False but only for one Workbook. I mean, I'd like to limiting user interaction only to UserForm, but I need have an access to anothers workbooks. At the moment this function cause hide workbook, but after open some another excel file - all object from userform are not available.
Private Sub Workbook_Open()
ThisWorkbook.Application.Visible = False
Starter.Show modeless
End Sub
Thanks for your support.
Please, create a form, let us say "Starter", having (at least) a button ("btExit"), copy the next code in its code module and show it. If the form in discussion already has some code in Initialize and Terminate events, please add the next code lines, too:
Option Explicit
Private Sub btExit_Click()
Unload Me
End Sub
Private Sub UserForm_Initialize()
ThisWorkbook.Windows(1).Visible = False
End Sub
Private Sub UserForm_Terminate()
ThisWorkbook.Windows(1).Visible = True
End Sub
So, you can simple use workbook Open event in this way:
Private Sub Workbook_Open()
Starter.Show vbModeless
End Sub
I am just learning VBA and have used some code from an older book (Excel 2010). It could be that Excel 2016 had some changes that make this code not work anymore.
I do not get a compile error for the class or the Subs. The behavior is that NOTHING happens. What is supposed to happen is that the BackColor of either a ComboBox or a TextBox should change color as if is in focus or leaves focus.
As I said, for some reason when I run the code nothing happens. No errors or warnings appear so it's as if the code is running and then just doing nothing.
Here is my code. The comments should make it clear. I am hoping someone can explain to me what is going on and why this code results in no color changes as the focus changes when I tab through the UserForm.
This first block of code is a stand alone Class Module called "clsCtlColor"
Public Event GetFocus()
Public Event LostFucus(ByVal strCtrl As String)
Private strPreCtr As String
'Base Class for chaging Backcolor of ComBoxes and TextBoxes when focus is changed.
Public Sub CheckActiveCtrl(objForm As MSForms.UserForm)
With objForm
If TypeName(.ActiveControl) = "ComboBox" Or _
TypeName(.ActiveControl) = "TextBox" Then
strPreCtr = .ActiveControl.Name
'On Error GoTo Terminate
Do
DoEvents
If .ActiveControl.Name <> strPreCtr Then
If TypeName(.ActiveControl) = "ComboBox" Or _
TypeName(.ActiveControl) = "TextBox" Then
RaiseEvent LostFucus(strPreCtr)
strPreCtr = .ActiveControl.Name
RaiseEvent GetFocus
End If
End If
Loop
End If
End With
Terminate:
Exit Sub
End Sub
The following Subs are in the UserForm Code
Option Explicit
Private WithEvents objForm As clsCtlColor
'*********************************************************************************************************************
'*Subs for managing the BackColor of comboxes and TextBoxes depending on focus.***************************************
'*********************************************************************************************************************
'initializes the Userform with the clsCtlColor class
Private Sub UserForm_Initialize()
Set objForm = New clsCtlColor
End Sub
'Changes the BackColor of the Active Control when the form is activated.
Private Sub UserForm_Activate()
If TypeName(ActiveControl) = "ComboBox" Or _
TypeName(ActiveControl) = "TextBox" Then
ActiveControl.BackColor = &H99FF33
End If
objForm.CheckActiveCtrl Me
End Sub
'Changes the BackColor of the Active Control when it gets the focus.
Private Sub objForm_GetFocus()
ActiveControl.BackColor = &H99FF33
End Sub
'Changes the BackColor back to white when the control loses focus.
Private Sub objForm_LostFocus(ByVal strCtrl As String)
Me.Controls(strCtrl).BackColor = &HFFFFFF
End Sub
'Clears the objForm when the form is closed.
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Set objForm = Nothing
End Sub
In the Class Module the is an On Error Statement that terminates the Sub when an error occurs. However, I commented it out and still, I see no compile errors. So, I can only conclude it is a runtime issue.
Any help would be much appreciated.
UPDATE:
If I use these two subs on a TextBox I get the effect I'm looking for:
Private Sub TextBox1_Enter()
TextBox1.BackColor = RGB(153, 255, 51)
End Sub
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TextBox1.BackColor = RGB(255, 255, 255)
End Sub
What I hate about this is that my UserForm has over one hundred TextBoxes and I would need to write these two subs for each TextBox - so like 200++ Subs!
I am still trying to get the above more general approach to work.
One thing I noticed is that if I change the RGB values in the two subs above to Hex values, they no longer work. I tried changing the hex color values in the more general approach to RGB but it made no difference.
Yet Another Update:
It was pointed out that I had a typo in the class LostFucus. I changed that in two places to LostFocus. However, the code still does not work. Then the question was whether or not my code is in the userform module. It is. Then I tried an experiment. I created a new Workbook and imported the code into a brand new class and userform. I added three textboxes. Abracadabra! It worked! However, it does not work in the form I want it to work in. I have scoured the properties for the form itself and the text boxes and I can see nothing different between my form and the dummy form.
This must be something very simple I am over looking!
After a great deal of head scratching and screaming at my poor monitor I finally found the solution but as of now, I am totally disappointed in Microsoft for the weirdness of working with UserForms. Here is what fixed the problem:
I had not yet set the tab order!
I realized the tab order had my form opening with the first tab stop being set for a TextBox in a MultiPage on my form. I set the tab order so that the first TextBox is active on the UserForm and everything works with the coloring on the main body of the form.
Here is where the weirdness begins, in my opinion.
When the last TextBox on the main body of the form is reached and tab is pressed, the multi-page itself is selected. Only after you hit tab a second time is the first TextBox within the MultiPage selected and then the colors are not applied as they are in the main body of the form at all. The same scenario holds true for Frames as well. Also, there does not appear to be a good way to simply tab from the end of page 1 to the beginning of page 2.
It's very disappointing to me because I would have thought that this is not the way it is. I ASSUMED I could set up 1000 TextBoxes, use the Frames and the Multipage to organize things (SO I COULD MAINTAIN THE WINDOW AT ONE SIZE AND NOT HAVE TO SCROLL THE FORM UP AND DOWN) and then set a tab order that would navigate ALL of the TextBoxes regardless of what organizing container they are in. I assumed it would be this way because it MAKES SENSE! I want to click into the first TextBox and simply never touch my mouse until the form is completely filled out. Otherwise, there really is no point in this effort of making a UserForm! I could point and click around in the spreadsheet without the hassle of designing a form and writing code!
What a bummer!
I suppose I can "make it so!" by writing a bunch of code to jump the selection from container to container...MICROSOFT - It should not be this wonky and stupid!
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'm using a UserForm spawned by Excel that modifies a PowerPoint presentation (it's a roundabout way to avoid needing a macro-enabled spreadsheet). The form works just fine, but every time I focus to it the Excel application takes focus (since Excel is the parent window).
Is there any way to stop this from happening? I'd like to prevent Excel from taking focus when the UserForm is used.
Would something like this work? This will hide/make invisible the parent Excel Application while the UserForm is displayed. Or at least get you started:
Example subroutine that "Shows" the userform:
Sub Test()
Dim ppt As Object
Dim xl As Object
Set ppt = GetObject(, "PowerPoint.Application")
Application.Visible = False
UserForm1.Show vbModeless
End Sub
Use this in the form's Terminate event:
Private Sub UserForm_Terminate()
'Ensures the Excel Application is visible after the form closes
Application.Visible = True
End Sub
You could add a button/etc on the form, if you want to allow the user to unhide the Excel Application
Private Sub CommandButton1_Click()
'Displays the Excel Application:
Application.Visible = True
End Sub
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