I'm getting reports of VBA "Run-time error '1004': Method 'OnKey' of object '_Application' failed" at least on Excel 2016 under Windows in this code:
Private Sub Workbook_Deactivate()
Application.OnKey "^x"
End Sub
(The code overrides certain hotkeys when my VBA-enabled workbook is active, and restores the default behavior when the user switches back to another workbook.)
What could be the reason?
Unexpectedly, many API calls fail when the active workbook is in the Protected view mode. I wasn't even able to switch the active workbook to ThisWorkbook via Activate.
The workaround I came up with is to dismiss "Protected view" when the user switches to such a document from my app/workbook:
If Not (Application.ActiveProtectedViewWindow Is Nothing) Then
Application.ActiveProtectedViewWindow.Edit
End If
Application.OnKey "^x"
This gives up some of its protection, but as my users are working mainly with internal documents, this tradeoff makes sense.
Related
I use the Private Sub Workbook_Open() for some VBA code in order to run those code when I open the workbook. I try it on my laptop, it works fine but when I send the file to my colleague the file, it opens in protected view, and then when clicking the enable editing, it got Error code 1004: Method ‘Sheets’ of Object ‘_Global’ Failed. Try to surf the internet, it said that because the file opens on protected view so the VBA won't run.
How can I solve this problem?
Thank you
Scenario
Create a blank workbook and add a ToggleButton and an InkPicture control (the latter inserted by means of ActiveX Controls -> More Controls). In the SizeChanged event of the InkPicture control, add the line:
Sheet1.ToggleButton1.Caption = "foo bar"
Problem
A "Method or data member not found" error in the line above that highlights the occurence of .ToggleButton1 from that line.
Reason
The ink picture's SizeChanged event happens before the toggle button is created, and in some cases, the event occurs before the workbook's Open event! You can Debug.Print relevant messages in the Immediate Window to see that.
Question
How can I postpone the execution of the ink picture's SizeChanged code until the toggle button is created and can be accessed as a property of the worksheet? I can, of course, workaround the issue by creating a Boolean variable to avoid the InkPicture's SizeChanged event's execution until the end of the workbook's Open one, but this is only paper over the cracks and doesn't solve the actual issue.
What I've tried
stopping the debugger, re-compiling the VBA project and saving it
afterwards
add DoEvents before the line above
set the toggle button's AutoLoad property to True, in an attempt
to create the toggle button before the ink picture
Every attempt appeared to work after saving and reopening the workbook, only to fail on subsequent saves and openings.
Note: I've edited my post to be more concise and easy to read, so try not to be overzealous and downvote a legitimate question...
Depending on which properties you need to access, you can try and grab a reference to your control via the Worksheet.OLEObjects collection.
Example:
Private Sub InkPicture1_Resize(Left As Long, Top As Long, Right As Long, Bottom As Long)
Dim oleObj As OLEObject
Set oleObj = Sheet1.OLEObjects("ToggleButton1")
oleObj.Left = 1
oleObj.Top = 1
'...
End Sub
In this way, you are interacting with the control through the OLEObject Interface. As such, some of the control properties may not be available. Normally, you can interact with the control through its specific control interface (ie, the ToggleButton interface) by grabbing the OLEObject.Object property.
Private Sub InkPicture1_Resize(Left As Long, Top As Long, Right As Long, Bottom As Long)
Dim tb As MSForms.ToggleButton
Set tb = Sheet1.OLEObjects("ToggleButton1").Object
tb.Caption = "foo"
End Sub
However, this seems to cause an error when the workbook is starting up. I believe it is a security thing. But, at least this error is a runtime that you can catch and handle, as oppose to the unhandlable Method or data member not found compiler error you are seeing right now.
Hope this helps!
I have a progress using a userform. I show the userform when a worksheet button is clicked. The button calls startProgressIndicator()
Sub startProgressIndicator()
UserForm1.Show
End Sub
I have this as my userform code:
Private Sub UserForm_Activate()
Call myCode
End Sub
I then want to hide the progress bar and ask for user input. This occurs in myCode. I inlcude UserForm1.Hide in beginning of myCode.
After getting the user input, I want to show the progress indicator again.
I try UserForm1.Show, however, this just calls myCode all over again. I just want the same userform to be visible again.
I tried using UserForm1.Visible = False, but then I get this error
Function or interface marked as restricted, or the function uses an
Automation type not supported in Visual Basic
Short answer is to rewrite myCode to not include .Hide. Break myCode into logical chunks.
However, you should separate the logic from the display (see Rubberduck UserForm1.Show (*)). By doing so - you would only call .Hide from the form when you want to (e.g. on the 'Close' button click).
#ChrisNeilsen suggested using _Initialize and this will solve the immediate problem but will not set you up for better programming practices in the future. lso, if you decide to modify myCode you may get bugs that are harder to identify.
#ChrisNeilsen: Use 'UserForm_Initialize' rather than 'UserForm_Activate'
(*) No disclaimer required, I am in no way affiliated with Rubberduck, but it does make good reading!
I can writing VBA in Mac os. The below code in excel (windows) check an option button.
ActiveSheet.Shapes("Otion button 1").OLEFormat.Object.Value=true
This code does not work in Mac excel.
Can anyone help to get the code for Mac excel to do this function.
Thank you,
The code you pasted doesn't work in the Windows environment either. It results in an error: "Runtime error '438': Object doesn't support this property or method"
After exploring the locals window, I found that the correct way to access this value is through a double .object ... So your code would be:
ActiveSheet.Shapes("Otion button 1").OLEFormat.Object.Object.Value = True
So I have been able to load a PDF into my userform (Excel 2013 on all machines) setting reference Adobe Acrobat Webrowser Control and adding control to userform. When i open the file on another computer or try to create another excel userform and pdf I get the following errors "Run-time error-459" or "Element not found" or "Compile error: Method or data member not found"
I am using Acrobat Adobe Reader DC
I also noticed when adding control to form it labels it as Control1 instead of AcroPDF1
Private Sub Workbook_Open()
With UserForm1.Control1
.LoadFile ThisWorkbook.Path & "\sample.pdf"
End With
UserForm1.Show
End Sub
Please any help much appreciated
Make sure that the Additional Control for Adobe has been added. Also take a quick look into this post for workaround and suggestions. http://www.ozgrid.com/forum/showthread.php?t=88177
After some trial and error i found a solution. I was currently using google chrome when experiencing this issue. The problem was that i had a google app conflicting with the pdf. So i disabled google apps and problem dissappeared. Sorry comunity i didnt resopnd earlier..thanks for all the input