Selectable Text in VBA Message Box - vba

When I create a msgbox in VBA, the user cannot interact with the text at all. In my case, I want the user to be able to highlight and copy the text. I'm thinking the better way to do this may be to add a button that copies the text to the clipboard. Any suggestions?

For this code:
msgbox "Does Control+C work on a lowly, old message box?"
you can press ctrl + c, open notepad, ctrl + v and you will get this:
---------------------------
Microsoft Excel
---------------------------
Does Control+C work on a lowly, old message box?
---------------------------
OK
---------------------------

You can use an inputbox instead of a message box
inputbox "Copy the below text", "Copy Text", "Text value"
inputbox copy text

If you want text that is "selectable" then don't use MsgBox. Use a Custom form and instead of a label use a textbox. However...
Change these properties of the textbox in design mode.
SpecialEffect - fmiSpecialEffectFlat
BackStyle - Transparent
Locked - True
And then use this code
Option Explicit
Private Sub UserForm_Initialize()
Me.Caption = "Message Box"
TextBox1.Text = "Hello World!"
End Sub

You could use VBA to set the clipboard text if the user typically would copy the entire message.
Or use input box instead of msgbox, since the user can copy from the populated value.

You could make your own custom message box using a UserForm. Here's a rudimentary proof-of-concept: a userform with one text box (from which you can select and copy text).
Sub MyMsg(msg As String)
With MsgUserForm ' this is the name I gave to the userform
.TextBox1.Text = msg
.Show
End With
End Sub
Usage:
MyMsg "General failure reading hard drive."

Related

Pause VBA Word macro, allow user to make a selection, and restart where it left off

I have a requirement for VBA script in Microsoft Word to pause so that the user can select text that will be copied to the clipboard so that it can exported to an Excel file. The user will make a number of selections and finally indicate he/she is done when the contents of the clipboard will be copied to a template Excel file.
I have the code working to copy each selection to the clipboard and then all rows to the Excel file. But I need assistence in figuring out how to pause the code to allow the user to make the selection and then restart the code to copy the selection to the clipboard. I am able to get the userform with toggle switch to switch states and labels when pressed. But have not figured out how to pause the VBA code to allow the user to navigate to the next section of the Word document for the next selection.
The Stakeoverflow question/answer below appears to address this requirement but I have not been able to get it to work. It appears that the code is incomplete.
Pause VBA macro, allow user to make a selection, and restart where it left off
Can someone provide example VBA code that accomplishes this?
Your assistence is much appreciated as I have been beating my head against the wall and it is starting to hurt!
There's no way in VBA to "pause" a macro. Code must run to completion... Unless there's a command for user input.
Input can be requested via the InputBox and MsgBox methods, but those block access to the document because they're modal. A UserForm, however, can be set to display as non-modal, meaning it stays on top, but doesn't block access to the document or the application features. Since you're already working with a UserForm, this can be implemented relatively easily.
In the small example below, the Continue button runs the code to perform an action on the user selection. When Done is clicked the entire code is exited and the form unloaded.
Code behind the user form
Option Explicit
Private Sub cmdContinue_Click()
Debug.Print Selection.Range.Text
End Sub
Private Sub cmdDone_Click()
Me.Hide
End Sub
Private Sub UserForm_Activate()
'Position the form near the top-left of the window
'So that the user can work with the document
Me.Top = Application.ActiveWindow.Top + 50
Me.Left = Application.ActiveWindow.Left + 50
End Sub
Code in a regular module
Option Explicit
Sub DisplayModeless()
Dim frm As frmModelessForInput
Set frm = New frmModelessForInput
frm.Show False 'Display as non-modal
Set frm = Nothing
End Sub

Detect Button Press Event on an Excel Sheet MultiPage Form (NOT a VBA MultiPage)

I am struggling to figure out how to detect a button press event on a MultiPage form that resides directly on an Excel sheet. In other words, the form is not generated using VBA, it is built directly on the sheet.
I expect the syntax to be something like:
Private Sub MultiPage1_Frame1_CommandButton1_Click()
Do Stuff
End Sub
However, that doesn't work because I think I also need to specify the page in the MultiPage. For example, a text box on the same form can be accessed via:
MultiPage1.Pages(0).Frame1.TextBox1.Text = "Some Text"
Unfortunately,
Private Sub MultiPage1_Pages(0)_Frame1_CommandButton1_Click()
gives a Compile error: Expected: identifier with the zero inside (0) selected.
So, how do I convert
MultiPage1.Pages(0).Frame1.CommandButton1
to a Sub name that will detect the button press event?
Thank you!
I'm not sure but I think you may have stumbled onto a bug.
There is a Microsoft Forms 2.0 Control under More Controls, but I'm pretty sure it's only intended only for use on UserForms.
I tried adding it to a worksheet and got an error. However, once I added one to a UserForm and went back to the worksheet, I was able to add it... but things got "glitchy" for moment, and when I opened the Properties dialog for the bod, the font was poorly rendered.
All the documentation that I looked at (like this, this and this) only have examples of it being used on a UserForm, or in Outlook.
There are many types of ActiveX controls, and not all of them can be used anywhere. As a rule of thumb in Excel, it's best to stick to the controls that are built-in.
Also, from another forum:
Q: It seems that I could not find and add Multipage control into worksheet.
How to add a Multipage control to Excel worksheet?
A: Unless you put it on a UserForm first, you can't display it on a Worksheet. The UserForm provides the user interface to VBA. The MultiPagecontrol is designed to work with this user interface, and not the Excel Worksheet. Is there a problem using the UserForm to display the MutliPage control? Source: Leith Ross
This evidence combined tells me, even if you can get it to work, you shouldn't. There's no predicting how it will behave.
In case you decide to use a MultiPage on a UserForm, note that in your example above, MultiPage1 is the name of the control; that's not referring to "page 1". The control as a whole has a Click event which is not specific to a page:
Private Sub MultiPage1_Click(ByVal Index As Long)
For the sake of completeness, I'll paste in a complete code sample but once again: this is not recommended for a worksheet-based control.
How to: Access a Page in a MultiPage Control
The following example accesses an individual page of a MultiPage in several ways:
Using the Pages collection with a numeric index.
Using the name of the individual page in the MultiPage.
Using the SelectedItem property.
To use this example, copy this sample code to the Script Editor of a form. Make sure that the form contains a MultiPage named MultiPage1 and a CommandButton named CommandButton1.
Sub CommandButton1_Click()
Dim PageName
Set MultiPage1 = Item.GetInspector.ModifiedFormPages("P.2").MultiPage1
For i = 0 To MultiPage1.Count - 1
'Use index (numeric or string)
MsgBox "MultiPage1.Pages(i).Caption = " & MultiPage1.Pages(i).Caption
MsgBox "MultiPage1.Pages.Item(i).Caption = " & _
MultiPage1.Pages.Item(i).Caption
'Use Page object without referring to Pages collection
If i = 0 Then
MsgBox "MultiPage1.Page1.Caption = " & MultiPage1.Page1.Caption
ElseIf i = 1 Then
MsgBox "MultiPage1.Page2.Caption = " & MultiPage1.Page2.Caption
End If
'Use SelectedItem Property
MultiPage1.Value = i
MsgBox "MultiPage1.SelectedItem.Caption = " & _
MultiPage1.SelectedItem.Caption
Next
End Sub
(Source)
Now I'm going to delete that buggy worksheet and reboot; I'd suggest you do the same!
Mikerickson and Jaafar Tribak on MrExcel.com figured it out:
https://www.mrexcel.com/forum/excel-questions/1054446-detect-button-press-event-excel-sheet-multipage-form-not-vba-userform-multipage-2.html
Big thanks to both of them!

Set focus on textbox after performing print command

Please help me case below:
When I write code for print command by vba then it run OK.
But after run print command then cursor don't focus on textbox.
what do I must use command to cursor focus on textbox?
Thank you so much.
If TextBox1.Text = "PRINT" Then
ActiveSheet.PrintOut
TextBox1.Value = ""
Call CData
ThisWorkbook.Save
ThisWorkbook.Activate
UserForm1.TextBox1.SetFocus
End If
Because I am writing macro for scan barcode on textbox, so I need to automatic process after perform print, it can continue receive scan barcode on textbox, no must click on textbox before scan barcode.
Check first if there is any error message and where your code is located (module, userform, worksheet or workbook code).
Are you sure that your If TextBox1.Text = "PRINT" condition is executed? Check this by inserting a beep command, some Debug.Print info for the immediate window or a Stop command allowing you to check code continuation manually by pressing F8.
You could try Windows(ThisWorkbook.name).Activate instead of only ThisWorkbook.Activate, especially if you are using more than one workbook and do some selection or activating.
If the TextBox situated on a page of a MultiPage control, you should activate this page first: e.g. Multipage1.Value = 0 (...) and then set focus via TextBox1.SetFocus.
Don't reference :-) UserForm1.TextBox1.SetFocus, but use Me.TextBox1.SetFocusor TextBox1.SetFocus :-) if this is code withIN your UserForm module.
You could also perform a Click() event on your TextBox, though that isn't best programming style: TextBox1_Click

How to make macro create an alert when changing a value in the drop down list

I am currently working on an project that contains forms. Therefore users pick a specific type of form from the drop down list. How do you create an alert in excel using macro when a user picks certain type of form from the dropdown list. And after completing the form and before changing it I want to issue an alert saying that please make sure to Save the form. How is this done in excel.
Thank you for your help.
You can using event "Change" in Combobox
Private Sub ComboBox1_Change()
MsgBox "Value changed"
End Sub
Or check value after change, like this
Private Sub ComboBox1_Change()
If ComboBox1.Value = "ABC" Then
MsgBox "Value changed"
End If
End Sub
And about "alert Save the form", because I don't know your form, so I can't give you ideal.

VBA cannot find my macro when it has parameters

I am trying to write a macro that will be attached to a series of buttons in an Office 2010 backstage tab. Depending on the button clicked the Macro should be called with different parameters.
The issue I am having is that if the Macro is defined as having parameters then VBA will display the "Macros" dialog box, with no Macros listed. Removing the parameters from the declaration will allow the macro to run, but it needs the Macros to make sense.
The VBA being used is below:
Sub NewDocs(docType As String, docTemplate As String)
Dim sMyShellCommand As String
sMyShellCommand = "C:\NewDocs.exe " & docType & docTemplate
WordBasic.Shell (sMyShellCommand)
End Sub
Any ideas
If I've got your question right....
Because there is no place in the Macros dialog box for entering parameters, the macros with parameters are simply not shown.
If you want to make them visible in the dialog box, you can enumerate those functions you need (and I hope it is not a big number).
For example,
Sub NewDocs1
Dim docType As String
Dim docTemplate As String
docType = "the type you want"
docTemplate = "the template you want"
NewDocs docType, docTemplate
End Sub
In addition, as you said in the question, you wanted the macro to run when buttons were pressed. Then there is no need to make the macro visible in the dialog box (which saves your labor). Simply associate it with the button with correct parameters.
You can pass arguments from the Macro dialog. For example, if you have this macro
Sub myMacro(n As Long)
MsgBox n
End Sub
To run it, enter
mymacro 1000
... and press the Run button.
You can't call functions or subs with parameters from UI components, just subs without parameters. The best solution is to create a parameter free sub for each button you need to associate with, and call the parameterized sub or function from inside each sub