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.
Related
I have an Order Sheet which includes a combination of check boxes and also option buttons.
There are 10 rows of 3 option buttons and 1 checkbox. Each row is grouped together.
The check boxes work fine and I am able to refer to them using a variable i which refers to the row number.
I have tried to do the same for the Option buttons. I.e I am trying to refer to the option buttons using variable i. However, I have been unable to find a solution.
This is the code I am using to refer to the check-boxes using variable i. This works fine.
If Sheets("Order Sheet").Shapes("Check Box " & i).ControlFormat.Value = 1 Then
I have used a similar logic to try and refer to the option buttons. However, all variations of my trial and error based on vaguely similar things that I have found online have failed.
If OLEObjects("OptionButton" & i + 30).Object.Value = True Then
The current error with the code snippet above is "Sub or function not defined". Highlighting the OLEObjects line of code.
Any help or direction on where to go with including a variable in the naming of the option buttons would be greatly appreciated.
You need to specify in which worksheet your OLEObjects is:
ThisWorkbook.Worksheets("Sheet1").OLEObjects
Also see the documentation: OLEObjects object.
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
Hi I am using excel 2010 with activex controls. I know activex controls can cause corruption and random resizing of the controls. This problem starts when I change screens.
I need to be able to add elements to a form control through a button being clicked and I also need to access elements in the form through VBA.
My question is how do you add an element into a form control listbox and how do you access it and how do you delete it.
My code uses all active x components but I want to switch them up. I am not sure how to set a name on a form control listbox to even access it in VBA.
Any help would be awesome!
PS. I am using windows 7, excel 2010
This method creates a listbox runtime. You just need to leave space on UserForm1 for it.
Dim lb As msforms.ListBox
Set lb = UserForm1.Controls.Add("forms.listbox.1", "MyListBox1")
This is also a good demostration on how to refer to an (ActiveX) object. A workaround to avoid accidental/unexpected resizing you can intentionally run the following snippet regularly from within the code:
If lb.width <> 100 Then ' or you can check other properties, too
lb.Top = 120
lb.Left = 40
lb.Width = 100
End If
This can be applied to other ActiveX objects, too, e.g.:
If UserForm1.Width <> 200 Then
UserForm1.Width = 200
...
You just need to find a good place in your code where this snippet is run often, and also definitely in UserForm_Initialize. You can also set other properties like caption or tabstop, etc. this way.
For adding and removing items you need to use indices like this:
lb.AddItem "First item", 1
lb.AddItem "Second item", 2
lb.AddItem "Third item", 3
lb.RemoveItem 2
I have the following code that works in all other circumstances except in a single where it returns the error Can't move focus because it is invisible, not enable, or type that does not accept focus. The data in sheet consists only basic numbers and words. My objective is to select a range from one work book and paste it to another. It appear that excel does not recognise anything to be in the cells, although there in fact is. Does anyone know why this may be happening?
Set Users = Application.Workbooks.Open(PathA)
With Prices
.Sheets("Sheet").Range("A:AJ").Select
Selection.Copy
End With
'Set Risk = Application.Workbooks.Open(PathX)
With Risk
.Sheets("Sheet").Range("A1:AJ1048576").PasteSpecial Paste:=xlPasteAll
.Save
' .Close
End With
Users.Close
it looks like someone else had the same issue and was able to resolve it on an MSDN forum
http://social.msdn.microsoft.com/Forums/office/en-US/3263b079-7e4f-452c-8dcc-92c682b8370b/excel-form-cant-move-focus-to-the-control-because-it-is-invisible-not-enabled-or-of-a-type-that?forum=exceldev
maybe this fix will apply to your situation too.
... OK so on that page, one guy has a kludge that seemed to work for him:
Error was:
Can't move focus to the control because it is invisible, not enabled, or of a type that does not accept the focus.
How to Fix
1. Select the object with the problem, e.g. the Form or Control.
2. In the properties windows, select the name of the object and rename adding an "x" to the end, e.g. Rename CPanel to CPanelx.
3. Save the Form.
4.Now rename the CPanelx back to CPanel.
5. Run your form.
6. Problem solved.
Something screwy with Excel VBA, not sure what !, but this will get you working again.
Steve D.
and then someone else described the underlying problem, but his solution involved using design mode, which seems like it is maybe defeating the purpose of automating in vba, but the moderator seemed to like his answer better, so here that is as well
In normal use activeX controls are intended only to be activated, not selected, there's a difference. If you partifcularly want to Select the control (manually or with code), try putting Excel into Design mode first.
See also the topic "Why can't I select form and ActiveX controls?" in Excel's help (not VBA's help)
Peter Thornton
"Run-time error '-2147352565 (8002000b)': Can't move focus to the control because it is invisible, not enabled or of a type that does not accept the focus" Can be overcome but sequentially defining the area you wish to select. Obviously some may do this in any case, but it this specific instance it is needed to overcome the issue. This is an example for copying from one workbook to another and pasting where U is the sheet excel finds to be "invisible".
Dim U As Workbook
Dim Us As Worksheet
Set U = Application.Workbooks.Open(Path)
Set Us = U.Worksheets("sheet")
With Us
.Range("A:AJ").Select
Selection.Copy
End With
U.Close SaveChanges = True
With DestinationWorkbook
.Sheets("sheet").Range("A:AJ").PasteSpecial Paste:=xlPasteAll
.Save
End With
Just to help in case you didn't find the answer till now :
In my case this message came out when I redimensioned the application window while it was maximized :
e.g. Application.width = 100 (on Excel 2003 or 2007)
The solution in this case, is to first bring the application window to NORMAL.
e.g. ActiveWindow.WindowState = xlNormal
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