I created a form in Excel 2003 using a tutorial online but I had a different use case and now have run into a minor problem. I know very little VB and my searches have not produced anything helpful. I have a text input field and it won't wrap the text which would be preferred so the user can see what he/she wrote to prof read or edit before submitting. My guess is this code block requires something to specify that the text should wrap in that text box. Thank you in advance for any help you can provide.
Private Sub txtopportunity_Change()
End Sub
You don't have to do this in code. You can set the textbox to always wrap like this:
In Designer mode right click your text box and open it's 'Properties'
One of the peoperties is called 'WordWrap' Set it to True. AND set the 'MultiLine' property to True
If you really need to enable/disable this via VBA it would look something like
Me.txtopportunity.WordWrap = True
Me.txtopportunity.MultiLine = True
Related
Apologies for not showing my code but after much trial and error I'm thinking the issue I describe could possibly be a textbox property issue rather than coding error or ommission. The code itself works as it should but there is a phenomenom which frustratingly persists.
On a wsheet amongst a number of ActiveX controls I have a textbox and 2 images. They are used for a search function. As you would expect the textbox is for user entry and the images are for 'run search' and 'erase search'. I set the search text as a string.
My issue is when hitting either 'run search' OR 'erase search' the textbox momentarily shows the previous text string. I have set this previous string to "" all over but without success.
This is best observed when setting a search text which will knowingly fail.
The sequence is...
1) Enter 'XXXX' to search
2) Hit 'run search'
3) Search code executes
4) Prior to textbox narrative returning "XXXX not found" it momentarily shows the previous entry, say "AAAA", before returning the correct result.
How can this be prevented?
EDIT
With no response I posted this on Jon Peltier's site at https://peltiertech.com/forms-controls-and-activex-controls-in-excel/#comment-1481602
Kindly he tested and concluded "It looks like an ActiveX thing, and I guess you’re stuck with it."
From tests this phenomenon occurs even when selecting any cell, not only the image controls. In other words it is triggered as soon as the textbox loses focus. Arguably, because it is a momentary change it does not seem possible to trap the text.
I was experiencing a similar problem and raised a question myself, but managed to have more luck in getting some responses, see:
After setting ActiveX textbox to empty value, previous text briefly appears in box before disappearing again
Through the help of the responders I was able to create a work around using Application.SendKeys. Possibly one of the answers might be able to help you in getting around this issue.
For me personally Application.SendKeys was required as this overwrote the value in the text box and refreshed it meaning the previous value was no longer present. For completeness here is a snippet of the code I used:
'Select text box to update
Sheet1.userName.Activate
DoEvents
'Replace value with ""
Sheet1.userName.Application.SendKeys ("")
'Copy above for other textboxes on sheet
Sheet1.emailAddr.Activate
DoEvents
Sheet1.emailAddr.Application.SendKeys ("")
'Change text box currently active to force change values to take place
Sheet1.userName.Activate
I am selecting my text boxes one at a time then using SendKeys("") as a way to emulate the user deleting the current field in the box, after that I switch back to another text box as this is needed to force the refresh of the value.
Being able to force the text boxes to be blank after I was done with the values meant that there were no previous values that could inadvertently appear later on.
Thank you Skenworthy for your suggestion. Over time I kept returning to my issue but without success however your code has formed the basis of a solution.
I only had 1 textbox used as a search input plus 2 buttons, 1 to begin search and the other to clear.
My solution was to create a hidden dummy textbox and each time I wished to make a comment in the search textbox I would use sendkeys to the dummy box. However that was not the final code. Switching focus between the 3 controls became too complex given the type of potential user errors that could be made. So I abandoned the Begin search button relying instead on the Enterkey and the Clear search button.
Now each time I need to return a response message I use the code below immediately prior to clear the search textbox. It has consistently worked without flaw. So, a long time coming, my specific solution seems so simple in hindsight but thank you again for the pointer.
Sub RemovePrevText()
''' use dummy txtbox to clear old message from SearchInput
With Sheets(1)
.txtDummy.Activate
DoEvents
.txtDummy.Application.SendKeys ("")
End With
End Sub
I have a Word template where I have a checkbox content control in the middle of the document. Once this checkbox is clicked, it triggers some commands using VBA. However, I also have plain text content control and date picker content control earlier on in the document that helps in filling out the template for the user. When these boxes are selected, I keep getting an error message saying "Run-time error 6290 - This property is only available for check box content controls".
My question - is there any way to ignore the earlier content control boxes and only run the code when the checkbox is pressed?
My code at the moment looks something like this:
Private Sub Document_ContentControlOnEnter(ByVal ContentControl As ContentControl)
If (ContentControl.Title = "Checkbox1" And ContentControl.Checked = True) Then
*code in here*
End If
End Sub
So you would think that the code would only get triggered once Checkbox1 is checked... but the earlier text and date fields give me an error code. Anyone know what's going on?
I'm not familiar with "content control" or Word VBA, but in Excel, when coding for the .OnChange event, it's important to make sure the cell passed in to the Sub is (one of) the one(s) you want to work with. I'd assume the same is true here.
My guess is your error is on the
ContentControl.Checked = True
portion of your If statement, so give this a try:
Private Sub Document_ContentControlOnEnter(ByVal ContentControl As ContentControl)
If (ContentControl.Title = "Checkbox1") Then
If ContentControl.Checked = True Then
*code in here*
End IF
End If
End Sub
i.e., make sure you're looking at a checkbox control before testing to see if the box is checked.
VBA does not do conditional short-cutting, so it's going to evaluate all statements in a AND conditional even if the first one is False.
I know this is probably simple, but I have spent 2 hours Googling for the answer and I can't find it. I am using the Office Web Components 2003 (v11) Spreadsheet Control on an Excel 2007 userform. I am using the BeforeContextMenu event to create a custom context (right-click) menu. I'm using the code sample from the Spreadsheet Control's help file to create the menu. The code is:
Sub Spreadsheet1_BeforeContextMenu(x, y, Menu, Cancel)
Dim cmContextMenu(4)
Dim cmClearSubMenu(2)
cmClearSubMenu(0) = Array("&All", "ClearAll")
cmClearSubMenu(1) = Array("&Formats", "ClearFormats")
cmClearSubMenu(2) = Array("&Values", "ClearValues")
cmContextMenu(0) = Array("Cu&t", "owc2")
cmContextMenu(1) = Array("&Copy", "owc3")
cmContextMenu(2) = Array("&Paste", "owc4")
cmContextMenu(3) = Empty
cmContextMenu(4) = Array("Clea&r", cmClearSubMenu)
Menu.Value = cmContextMenu
End Sub
The custom menu appears perfectly when the spreadsheet is right-clicked. What I can't figure out is how to capture the menu item that I select. Can someone please point me to VBA code that will enable me to capture and use the selection?
Thanks In Advance -
After more Googling I found the answer to this problem in the Help for the OCW11 Spreadsheet Control. The BeforeContextMenu event lists the code shown in my original post. I could see that the menu items were defined with statements like:
cmContextMenu(0) = Array("Cu&t", "owc2")
The "Cu&t" is the menu item label, and the "owc2" is the constant (I assume) that is supposed to execute the Cut command (commands are known as "OCCommands" in OWC11). The problem is that the "owc2" constant wasn't firing the Cut command. I checked the "Programming Information" section of the Help, underneath "Enumerations," in the entry titled "OCCommandID" for the OCCommand ID constants. That's exactly where they are located. I looked up the Cut constant, which is 1001, and replaced the "owc2" with 1001, and the Cut command worked perfectly! I did the same thing for the "&Copy" and "&Paste" commands, with 1002 and 1003, respectively, and they worked as well.
My problem may be that I didn't set up the proper references to the OWC constants. Or it could be that the constants changed and weren't updated correctly in the Help documentation. Whichever may be the culprit, the above method fixed the problem.
I need a way to programatically launch the macro-recorder in Excel, and supply the name for the new macro that will get created.
This can be from VSTO or VBA, or using the Office interop assemblies.
Any ideas how this can be accomplished?
In VBA:
Dim ctrlStart As CommandBarControl, ctrlStop As CommandBarControl
Set ctrlStart = Application.CommandBars.FindControl(ID:=184)
Set ctrlStop = Application.CommandBars.FindControl(ID:=2186)
ctrlStart.Execute
'name part would go here, but first you have to deal with a modal dialog
ctrlStop.Execute
It looks like the Execute method on the RecordMacro control opens a modal dialog. There is no way to feed a parameter to this, or to do anything like SendKeys. The only way I see to do it is to write a sub that will rename the macro after the fact. It will be a little complicated to determine what the name of the new macro is, and you will still have a dialog box to deal with.
Does anybody know how to call the import data built-in dialog excel from a macro (vba)?
I've tried Application.Dialogs.Item(...).Show but I can´t find the right dialog.
Please help.
Thanks in advance.
The closest I can find using the dialog system is:
Application.Dialogs(xlDialogImportTextFile).Show
You can get a reference to the command bar button (at least for me in both 2k3 and 2k7) via:
Set button = Application.CommandBars.FindControl(ID:=6262)
But calling the Execute method on the button fails. Sadly, the short answer seems to be that it's not possible.
You can add QueryTable objects by hand. While not an optimum path, you could design your own simple interface for selecting the source data.
If you choose the Object Browser and search for say, xlDialogImportTextFile, you will get a list of possible dialogs.
EDIT:
Perhaps something on these lines would suit:
'Allow user to select text file
sf = Application _
.GetOpenFilename("Text Files (*.txt), *.txt")
If sf <> False Then
'Open text file
Workbooks.OpenText sf
End If
I don't think there is a VBA equivalent, because in one case you are returning data to a worksheet, while in the other case, the data is put into a recordset in memory.
This kludge should pop up the dialog for you, however:
SendKeys "%ddd"