Hidden VBA Code (project locked from viewing ) denies User Form Macro - vba

I think I am facing the same problem asked by Gaus Shaikh in the link below but I have the feeling he was not understood at that time. I try to spend some other words on this topic to help you guys for your support.
I create another question because, in my opinion Gaus question title could be misunderstood.
To recap, we have a problem dealing with an excel macro that generate a user form. Actually, if the VBA project is protected by password the macro works. The weird point is that, if I just hide the code (Visual Basic Editor-> VBA project properties -> protection -> lock project from viewing -> insert pw -> OK), the macro won't works even if the project is not protected by password.
I'm stuck here. I am not able to find a solution. Have you guys have any idea?
(I am using excel 2013)
Thank you in advance!
Gaus Shaikh question:
VBA password protected project not opening userform
N.B:
Basically, any macro which generates a user form gives " run-time error 50289. Project is protected." As an example, the following macro that generate an empty user form fails if the code is hidden and works if the code is shown.
Public Sub goUserForm()
Dim mainframe As Object
'Set main frame
Set mainframe = ThisWorkbook.VBProject.VBComponents.Add(3)
'Show the form
VBA.UserForms.Add(mainframe.Name).Show
End Sub
To fully understand the issue, try this macro on your worksheet, then hide your code. Save, close and open the document. The macro won't work unless you show your code.

Ok, now it's clearer. As the VBA project is protected you will not be able to access it even not with code. Any other code will work.
So, this will not work
Set mainframe = ThisWorkbook.VBProject.VBComponents.Add(3)
VBA.UserForms.Add(mainframe.Name).Show
Code not accessing the VBA project like this will work
UserForm2.Hide
UserForm1.Show

Related

Access app freezes open opening a form with embedded macro button

An Access application that has been working fine for years all of a sudden has started freezing any time someone uses the Search for POAs button. This button is an embedded macro that doesn't seem to do much other than assign two variables and attempt to open a form. It's when the form tries to open that it now freezes the app and you have to Ctrl+Shift+Esc to shut it down and reopen the app.
I'm not sure how to diagnose the issue without it being actual VBA code. I've attempted to first remove the embedded macro and turn it into VBA but I am getting different results than what it previously did. With the embedded macro (when it was working) when you would use the search button it would provide filtered results. But when I tried to recreate it, it simply opens the form.
Here is a pic of the embedded macro and then here is the code I tried to use to recreate it.
Private Sub Command155_Click()
Dim EString, EType As String
EString = Me.Text5
EType = Me.Text1
DoCmd.OpenForm ("frmSigSearchResults"), acFormDS, , , acFormReadOnly, acDialog
End Sub
So my question is, is there a good way to diagnose why the embedded macro isn't working? And/or is there a reason why the VBA code isn't doing the same thing that the embedded macro was doing after I replaced it?

Excel 2013 - User can't execute specific macro - File name listed before macro in Macros window

We have a single user unable to execute a specific macro in an XLSM file using Excel 2013. The issue seems to be tied to the user's entitlements because he gets the same error even on a freshly imaged machine. I would love to know if anyone has seen this issue before so we can pinpoint what's causing it internally. This can't be replicated by any other user, but is persistent to the user's account on any PC.
When he tries to execute a macro, nothing happens. No error message. Just nothing. On further review, the macro name looks weird in the macros window. It looks like (File Name).xlsm'!(Sheet Name).(Macro Name) instead of just listing the macro name. Screenshot here:
The macro is a public sub sitting on the sheet. If I move the code to a module, the name displays correctly and the macro runs, but that's not the way the code was designed to function. I'm not looking for help with the code. (This isn't mine to change and it works fine for dozens of users.) But I would be extremely grateful if someone familiar with this could give me an idea of what's happening here and perhaps why it could be tied to a user's corporate entitlements.
Thank you!

Launch Userform when Outlook Template is opened

I have a User Access Request form that I created in Word, and have now been asked to replicate in an outlook email template. This .oft file will be placed on our website, so that when users click on the link, they will download the .oft file. When the user opens the file, an email with the User Access Request form in the body will load.
I was able to set up the bookmarks, and import the Userform from Word, and have even gotten everything to populate correctly in the email from the Userform...however I have hit a wall.
I can only get the Userform to appear when I run it from Visual Basic in the Developer tab.
What I am looking to do is have the Userform pop up as soon as the template is opened, and only when that specific template is opened by a user (I don't want the Userform to open if they are trying to create a new email, or when they open outlook, etc). I have tried adding a module with the Userform.show code, I have tried adding the code below into the UserForm's code as well, with no luck.
Private Sub UserForm_Activate()
UserForm1.Show
End Sub
Do you have any suggestions as to how I can get this to work properly?
Thanks in advance!
You will need your custom logic to be present on every user's PC in order for this to work. Since you have created a UserForm in your Outlook VBA project, you will need to copy that form and supporting macros to every user's Outlook VBA project.
Often it is better to develop a COM Add-in to host your logic, and deploy it using a Windows Installer package. Deploying the .otm file where the VBA is stored is not supported.
Looks like I was able to get it to work. I added the following code:
Private Sub Application_ItemLoad(ByVal Item As Object)
R1AccessRequest.Show vbModeless
End Sub
Worked beautifully... until I sent the template over to a colleague to test... Userform appears to be tied to my outlook, and didn't appear for him. We opened up VB on his laptop, and saw that the form was not present :( From what I have been able to gather online, it doesn't seem possible.

MS Excel User forms vba

Hi "im kind of new with excel VBA. I'm trying to do something simple as creating a USERFORM1 in VBA and showing it when workbook opens. I've looked it up online but for some reason something is not working.
I open excel, go to developer, create a userform1, add some stuff to it.
I open code for THISWORKBOOK and under Open procedure I type
Private Sub Workbook_Open()
UserForm1.Show
End Sub
Then I save it as Macro Enabled and when i open it, nothing happens. What is going on? I know this is a silly question but am i doing something wrong?
In trust center I didn't have macros enabled. I did that and everything seems to work perfectly.

Prevent Word macro in Normal.dotm for some templates

I have a Normal.dotm file that contains an AutoNew macro.
This macro is automatically executed each time a new document is created using any other template.
Is there any way I can prevent this automatic behavior for a specific template?
I have a Word VSTO add-in running, so I can hook into Word's events, but so far I havn't found a way to prevent this.
I do know that I can prevent macro execution when using templates programmatically, for example like this:
' Disable auto-macros before opening document
wordApplication.WordBasic.DisableAutoMacros(1)
' Open document
newWordDocument = wordApplication.Documents.Open(template.FullName, ConfirmConversions:=False, [ReadOnly]:=True, AddToRecentFiles:=False, Revert:=True)
' Re-enable auto-macros
wordApplication.WordBasic.DisableAutoMacros(0)
But this solution doesn't work when the user uses a Word template from Windows explorer or the Open-dialog in Word, since in those cases I can't execute code before it's too late already.
Or can I?
I hope someone has a trick for me :-)
-
Edit: While trying different solutions, I discovered something that might help others in similar situations, though unfortunately it doesn't help me.
It seems that if a template contains a module containing an AutoNew (or AutoOpen for that matter), that local macro is executed instead of the one in Normal.dotm.
Example:
Normal.dotm contains the following macro:
Sub AutoNew()
MsgBox "Normal.dotm"
End Sub
Test.dotm contains the following macro:
Sub AutoNew()
MsgBox "Test.dotm"
End Sub
When executing Test.dotm the message "Test.dotm" is displayed, while the message "Normal.dotm" is not displayed.
If the AutoNew macro is removed from the Test.dotm template, the message "Normal.dotm" is indeed displayed.
So it is possible to easily override the auto-macros.
The local versions of AutoNew and AutoOpen can even be empty subs that do nothing. It still works.
This is not possible in my case though, since the template I use is generated by code, and cannot contain macros (because adding macros to templates programmatically requires the user to manually activate the option "Trust access to the VBA project object model", and that's something I cannot ask my customers to do for all users. It's also a security risk.)
Based on the workaround described in the "Edit" part of the question - providing a template with "empty" Auto-macros - it's possible to use the Open XML SDK to create a template and add the VBA project to it in order to provide this functionality. This approach avoids the user needing to allow access to the VBA project on his installation. The only "macro security" that could be triggered is that for not allowing macros to run. But since the client uses macros, anyway, this should not be a major obstacle.
The simplest method is to create as much of the basic template as possible in the Word UI and use this as a starting point.
Since you're unfamiliar with the Open XML SDK, the next step would be to create one (or more) templates in the Word UI using the basic template as the starting point, saving under a different file name.
You can then use Open XML SDK Productivity Tool to see the code required to generate any one of these files, as well as, using the Compare tool, the code for converting the basic template to the derived version. This should give you a decent start with the SDK and it's object model. Once you get a feel for how the Open XML SDK works, if you're familiar with Word's object model, using that provided by the SDK is relatively straight-forward as an effort was made to make it correspond as closely as possible to the "COM" object model.
The VBA project can be added later, but you can also include it in the basic template. That would be the simplest approach.
Include this "starting point" basic template as part of your solution, installing it as part of the solution.
Within the AutoNew macro you can check the AttachedTemplate property. Only if it is a template where you want to apply the cleaning you can execute the respective macros.
Sub AutoNew()
If ActiveDocument.AttachedTemplate <> "Normal.dotm" Then
Exit Sub
End If
' rest of the macro
End Sub
If you don't control the Normal.dotm you can put an empty AutoNew macro in your own templates. As Word only executes the auto macro in the closest context, the macro in the Normal.dotm file would not be executed.
If you don't control the other templates either, you can tell your users to hold down the SHIFT key while creating a document. This prevents the execution of the auto macro.
Probably it is best, however, if you ask the owner of the other system to find another solution that does not rely on polluting the Normal.dotm file.