PDA

View Full Version : Solved: Remove Excel Userform Using VBA



Cora419
08-28-2008, 06:58 PM
I'm using Excel 2003. I have a userform that loads when the file is opened and allows the user to select from among 10 pictures to insert into the Excel document. The Excel doc is a nomination form that is automatically emailed when a command button is clicked.

The problem is that the file is too large with the pics displayed on the userform. I want to be able to either remove the userform before the email code is run or just remove all 10 pics from the userform. I'm not sure how to do either. Any help would be greatly appreciated.
Thanks!

Aussiebear
08-28-2008, 08:39 PM
What code do you have already?

mikerickson
08-28-2008, 09:43 PM
In a normal module, to be run when the userform is not loaded:With ThisWorkbook.VBProject
.VBComponents.Remove .VBComponents("UserForm1")
End With

Cora419
08-31-2008, 01:47 PM
The code I was trying was very similar to what Mikerickson shows, just without the With/End With. When I try the code mentioned here, I get the follwoing error:

Runtime Error 1004
Programmatic access to Visual Basic is not trusted

Is there a setting I need to change to make this work? I would try to remove the pictures from the userform, but I'm sure I'd get the same error. I'd much rather just remove the whole userform.

Any ideas? Thanks!!!!!

Bob Phillips
08-31-2008, 01:55 PM
The code works fine, so perhaps syou are running it in the wrong place. Where are you using that code?

mikerickson
08-31-2008, 02:16 PM
How high is your security set?

Bob Phillips
08-31-2008, 02:23 PM
Also, do you have the Trust Access to Visual Basic Project in Tools>Macro>Security...>Trusted Publishers?

Cora419
08-31-2008, 04:40 PM
Perfect! Thanks, xld. That did the trick. I did not have "Trust access to Visual Basic Project" checked. Once I did that, the userform is deleted. One question -- does this change apply to the document? I want to make sure other users will not run into the same issue.

And thanks, mikerickson! Your code is exactly what I needed to make this work. Appreciate the help!

Bob Phillips
09-01-2008, 12:40 AM
It is an application setting, so you will need to make sure that theirs is set too.

You can check it with this code, and tell the users what to do if it is not.



Function VBAIsTrusted() As Boolean
Dim mpVBC As Object
Dim mpAlerts As Boolean
mpAlerts = Application.DisplayAlerts
Application.DisplayAlerts = False
On Error Resume Next
Set mpVBC = ThisWorkbook.VBProject.VBComponents.Item(1)
On Error GoTo 0
Application.DisplayAlerts = mpAlerts
VBAIsTrusted = Not mpVBC Is Nothing
End Function

Cora419
09-01-2008, 03:12 PM
Thanks xld! I'll give this a try.