Workbook.Unprotect doesn't work from ACCESS VBA - vba

From MS Access, I open an Excel (.xlsx) file to apply some changes, save and upload it on Access. Everything is managed by VBA.
If the Excel file hasn't an open password encryption, VBA works perfectly.
In order to manage a Workbook encrypted with password, I have written a VBA that has many odd bugs. I give in the form the password as an input, but the password isn't read by the following code. Opening the Workbook, Excel asks me to insert the workbook password in the prompt. Then, Workbook.Unprotect doesn't unprotect the workbook that is saved with the password. As expected, Access cannot upload data from the protected Excel file.
I think should be a very easy problem to solve, but I cannot figure it out. Have you any insight to give me?
'( VBA procedure starts with variable declaration)
Set ExcelApp = CreateObject("Excel.Application")
If IsNull(Me!Password_Ins) 'Password_Ins is the input
Then
Strg_Pwd = ""
Else: Strg_Pwd = Me!Password_Ins
End If
On Error GoTo ErrorHandler
Set WorkBookObj= ExcelApp.Workbooks.Open(Percorso, False, , , Strg_Pwd) ' asks to insert the password manually
'ExcelApp.Visible = True
WorkBookObj.Unprotect Strg_Pwd 'doesn't remove the password
' (procedure continues)

WorkBookObj.Unprotect removes the worksheet password. It works well.
Strg_Pwd is the encryption password of the Excel file. Using as parameter of "Workbooks.Open" allows to open the file, but it is encrypted anyway. To avoid encryption, I created a new Excel file and copy&pasted the sheet.

Related

VBA Ribbon Bar Integration

I have an Excel macro that performs a few functions on a document (Creates as form, and a few emails) all from MS-Word documents. If the macro is executed from the main spreadsheet (where the macro is), everything works normally. I want to place this macro on the ribbon allowing a user to launch it without having (or knowing where the main excel document is located or having it open). I created a sub to check to see if the spreadsheet was open and modified the ribbon to include an icon for the macro.
Which works. However, when launched from the ribbon while the main Excel spreadsheet is not open, it opens the workbook and runs the macro in entirety (Without executing the open workbook line of the macro). I assume the spreadsheet is being open because the macro that is being called resides with it (makes sense). Since the macro is dependent on the data contains in the spreadsheet, I need to allow the users to modify it and then re-running the macros from the ribbon again.
Does anyone have a recommended approach or best practices? Thank you in advance.
Sub MainForm()
Dim WorkingFolder As String
Dim File01 As String 'Main Excel Data File, where all data is
Dim File02 As String 'Preliminary Email to send to user
Dim File03 As String 'Final Email to Send to user when production is complete
Dim wb As Workbook
WorkingFolder = "C:\Temp\"
File01 = "01-MainData.xlsm"
File02 = "02-PreProductionEmail.docx"
File03 = "03-FinalProductionEmail.docx"
If wbIsOpen(File01) = True Then
MsgBox "Workbook Is Open"
Run ("'C:\Users\Guest\Nextcloud\Documents\Excel Forms\02-TEST-Production Request-Data.xlsm'!CreateProductionForm")
Else
MsgBox "The Main Datafile is not open, verify the last row before re-runing", vbOKOnly, "Not Open"
Set wk = Workbooks.Open(WorkingFolder & DataFile)
End If
End Sub
The Guide mentioned by Ricardo was not quite what I needed but it was helpful and did put me on the right track. Thank you again Ricardo. To get this working, I needed to do the following.
1 - Make the procedure above its own separate *.xlam file (saved as an add-in).
2 - Add the add-in to start automatically via the developers tab
3 - Add the Macro to the Ribbon.
I have modified the sample above to included the open statement along with the statement that executes a macros from another workbook.
Appreciate the guidance.

Is it possible to run a VBA macro in a READ-ONLY protected PowerPoint?

My colleague and I have been on a six month quest for a VBA macro that can run in a Read-Only, password protected PowerPoint.
Is it possible or will PowerPoint always block the VBA macro from running in the presentation, because of the read-only status?
Private Sub CheckBox1_Click()
Dim ppShp As Shape
Dim eff1 As Effect
Dim ani1 As AnimationBehavior
With ActivePresentation.Slides(1)
Set ppShp = .Shapes("Oval 4")
Set eff1 = .TimeLine.MainSequence.AddEffect(Shape:=ppShp, effectId:=msoAnimEffectAppear)
End With
End Sub
I tried using If ReadOnly = True Then conditions.
I want the user to use the macros and save, but not edit beyond that, or to open and 'look under the hood'.
(It's all for an educational program)
We get
Run-time error '-2147467259 (80004005)'
Presentation (unknown member) : Invalid request. Presentation cannot be modified.
Perhaps as a work around you could save a copy of the original read only presentation, run your macro on the new version and then save the new version as read only?
Hello this might be too late but if I understand you correctly, you want that other users can read, execute macros but not able to read the code behind it. Saving should be okay.
The macro that you used should be password protected. First go to the editor, select the macro and view the properties/protection and set up a password
Then after you are done, close the file. Right Click on the file/properties and check readonly. Users will then have access to the macro, not being able to see what code is behind it and can save it.

MS Excel macro runs different from VB Script?

I have a VB Script file that I open using a task in Windows Task Scheduler each morning. The script opens MS Excel and then runs a macro stored in a workbook. The problem is that the macro runs differently if I use the VB Script file to kick off the macro versus when i run it myself from the workbook. Specifically, I find that "Refresh All" doesn't actually refresh all data connections when I run the macro starting from the VB Script file. It works fine if I run the macro from the workbook itself. I'm thinking that I am missing something in this VB Script to ensure MS Excel opens correctly and makes "refresh all" work properly. Anything plainly wrong with this VB Script?
Dim ObjExcel, ObjWB
Set ObjExcel = CreateObject("excel.application")
ObjExcel.Application.Visible = True
Set ObjWB = ObjExcel.Workbooks.Open("K:\Analytic Reporting\11.Projects\TonyAdHoc\Autorefresher\DashboardAutorefresher.xlsm")
objExcel.Application.Run "DashboardAutorefresher.xlsm!Main"
ObjWB.Close True
ObjExcel.Quit
Set ObjExcel = Nothing
It seems that you are trying to do a refresh to pull data from a database, but you are not seeing any new data from this script when you manually open the Excel file after the script runs.
You will need to save the Excel file after making modifications.
ObjExcel.Save
For anyone still caring about this, what I've found is that for connections to a database table, if the property "Enable Background Refresh" is selected, then the vba command in RefreshAll in the marco doesn't work. So I add a little loop to disable that property for all the connections just to make sure before I call RefreshAll. That fixed it for me.
' -- make sure enable background refresh is not checked else RefreshAll doesn't work
Dim cn As WorkbookConnection
For Each cn In ActiveWorkbook.Connections
cn.OLEDBConnection.BackgroundQuery = False
Next cn
' -- then ok to refresh all
ActiveWorkbook.RefreshAll

VBA - Open XLS App On-Top of MSAccess

I am creating and formatting an XLS Workbook from an Access Database using VBA, initiated via a user button press on a form.
I'd like to have the XLS App ALWAYS open on top of the AccessDB to permit the user to see the worksheet directly. Currently it is intermittent, XLS usually opens UNDER Access on first iteration, but then on-top in subsequent iterations in the same session. User review of the file is necessary prior to final save and export to our client portal. There will typically only be a single iteration per user session.
I am using the following approach (pseudo):
Dim appXLS As Object
Dim wbkNew As Object
Dim wks1, wks2, wks3 As Object
Set appXLS = CreateObject("Excel.Application")
Set wbkNew = appXLS.Workbooks.Add
appXLS.Visible = True
wbkNew.Activate
...populate & format 3 worksheets from Access
wks1.Activate ' go back to sheet 1
wbkNew.SaveAs AccessMode:=xlExclusive, FileName:=myFile
I am saving the file in this routine, so any "overwrite file" warnings also open under MSAccess.
I thought the .Activate lines would ensure the desired function. Any thoughts or ideas on this?
Thanks!

Protect excel with password from vba

I want to design VBA code that protects excel document with password.
I know its possible with excel tools and its more secure, but I want to try this and use it in other projects.
I wrote this code
vPass = InputBox("Password : ")
If StrComp(vPass, "predefinedPass") <> 0 Then Application.Quit
But when I put it in Workbook_Open, the content of the file is still showing while while waiting for user to give password.
I don't want the content of file to be shown.
Any ideas??
It is simple before firing the event set
Application.Screenupdating = False
once the condition is true set screenupdating back to true