I want to link a password-protected Excel document to Access - this is not working due to the it being password-protected.
Does anyone have an alternative method to do this?
It has to be password-protected unfortunately and cannot be moved to a secured folder so not sure how to do it.
Any advice?
Try using a hidden form to automatically open the Excel file when the database file is opened.
Add this to a new Module:
Option Compare Database
Public xl As Object
Function OpenExcelFile()
xl.Workbooks.Open "path to file.xlsx", , , , "password"
End Function
Function CloseExcelFile()
xl.Quit
set xl=nothing
End Function
Create a blank form and set the HasModule property to true. then add the following to the form's code module.
Private Sub Form_Load()
OpenExcelFile
End Sub
Private Sub Form_Close()
CloseExcelFile
End Sub
Now create a new macro with an OpenForm task to open your form. Set the window mode to 'Hidden'. Save the macro with the name 'AutoExec'. This makes it run when the db is opened.
When the db is opened the macro will run and open the form hidden. The form load event will fire, creating a public Excel.Application object that opens your excel file (you should be able to remove the password from the code if you want the user to be prompted for it). The Excel application will remain open until the hidden form closes (when you close the database). At that point the form close event will fire, causing the Excel Application to quit. As long as the Excel file is open, you should be able to use linked tables and queries.
**You could add xl.Visible=true to the OpenExcelFile function if you want it to be visible to the user.
This link as a similar idea: https://www.connectionstrings.com/how-to-open-password-protected-excel-workbook/
Related
I have a VBA add-in that sits in a shared file drive that three users in my organization have added to their Excel. I thought the add-in would be handy because I could push changes to the code without updating the code in any individual Excel sheets. When I tried updating the code in the add-in, the add-in was read-only because other users had Excel open. I've been trying to figure out how to make the add-in read only whenever anyone else (meaning anyone other than me) opens an instance of Excel. I've added the below code to a class module in my add-in. I've tried debugging, but no part of the below code is executed when a user opens a new workbook. I'm totally stuck.
Private WithEvents App As Excel.Application
Private Sub Workbook_Open()
Set App = Application
End Sub
Private Sub App_WorkbookOpen(ByVal Wb As Workbook)
If App.UserName <> "Andrew Lubrino" Then
SetAttr "C:\FilePath\Add-in.xlam", vbReadOnly
End If
End Sub
I've found a number of posts on this topic all with accepted code for seemingly the same problem, but the code simply doesn't work in my environment. Can anyone tell me what's going wrong?
edit:
I'm trying to make the add-in read only for all other users so that when I try to edit and make changes to the distributed code, I can save those changes. With the current set up, I can't save the code whenever any other user has Excel open. For the above code to work, you'd need the event to trigger for any workbook that another user opens
Background: I have a file, AppLauncher.xlsm, that opens App.xlsm in a new instance of Excel, then closes itself. App.xlsm sets Application.Visible = False, then shows a UserForm. This gives the appearance that the UserForm is its own application, unrelated to Excel.
Issue: If the user manually opens another file, the file is opened in the second instance of Excel (the one with App.xlsm open) and makes the Application visible.
Goal: When the user manually opens a file, open the file in either an already open instance of Excel (if one exists) or a new instance of Excel.
What I've Tried/Researched:
Using the Application.WorkbookOpen event to capture the manually-opened Workbook's path and name to then close it and open it in a different instance of Excel; this would work, but it doesn't take into consideration another Workbook.xlsm with code using the Workbook.Open event (the Workbook.Open event fires before the Application.WorkbookOpen event).
Using Access instead of Excel. The VBAWarning registry key is set to 3, requiring all macros to be digitally signed; unfortunately, signing macros in Access appears to be broken.
Using Word or PowerPoint. I assume I would run into the same issue.
Running Object Table (ROT). From what I've read, the purpose of ROT is to not create new instances of an application if there's already one running. I've also read that Excel only registers the first instance of Excel in ROT; using RotView, I've observed that multiple instances of Excel are registered, not just the first instance.
Potential Solution: Remove the ROT entry for the instance of Excel that has App.xlsm open... unsure how to accomplish this using only VBA (using the SendMessage function?).
AppLauncher.xlsm code:
Private Sub Workbook_Open()
Call Shell("excel.exe /x /s " & """" & ThisWorkbook.Path & "\App.xlsm""")
ThisWorkbook.Close
End Sub
App.xlsm code:
Private Sub Workbook_Open()
Application.Visible = False
UserForm1.Show vbModeless
End Sub
Edit 1: Using Application.IgnoreRemoteRequests = True on the instance of Excel that has App.xlsm open does not appear to work in Microsoft 365.
I think you can use a window's API to show or hide the application instance.
Here's some code, I think something like this is what you are after.
Option Explicit
Public Declare Function ShowWindow Lib "user32.dll" (ByVal HWND As Long, ByVal nCmdShow As Long) As Long
Public Const SW_HIDE As Long = 0
Public Const SW_SHOW As Long = 5
Public Sub ShowHide()
ShowWindow ThisWorkbook.Application.HWND, SW_HIDE 'Get the Handle of the application running the form
UserForm1.Show 'Pop the form open
ShowWindow ThisWorkbook.Application.HWND, SW_SHOW 'When the form closes, show Excel again
End Sub
I was able to open another Excel workbook, that new Workbook opened normally, while the original Excel workbook with the form remained hidden.
My solution was to use a batch file.
The batch script first opens a named excel file.
Then follow this with opening the excel app file using /x to open it in a new session.
Double-clicking on another excel file opens it in the first registered session, leaving the excel app alone.
This is okay so long as the user doesn't close the first excel file.
I confess this works well in win10 with Excel 2010 but I am finding it a little more erratic in Win11 with Excel 365.
I have an macro enabled Excel file on a SharePoint that when a user opens it from the SharePoint the file opens programmatically another macro enabled Excel file on the same SharePoint. The file being opened by the vba macro needs to be edited, and must be editable by multiple users at the same time. However I can't get it to even open in edit mode. Using Office 365
I've tried << ActiveWorkbook.LockServerFile >> but always get an error message << Run-time error 1004: Method 'LockServer' of object'_Workbook' failed >>.
The code that I show below is in the Excel file that is opened manually by the user and that opens automatically the other Excel file. The other Excel file when opened works fine (if I remove the LockServerFile command), all it's macro's work fine, but it is open in read only and changes cannot be saved. Again this file should be editable by multiple users simultaneously.
' this code is in the "ThisWorkbook" tab
Sub workbook_open()
Set DB = Workbooks.Open(DBname, 3, False, , , , True)
ActiveWorkbook.LockServerFile ' this is where is crashes
'more code...
End Sub
' Note: DB is declared in a module
Public DB as Workbook
Public Const DBname As String = "https://ledvance365.sharepoint.com ... .xlsm"
Looks like << ActiveWorkbook.LockServerFile >> wasn't working because the SharePoint settings was not on "Open Documents in Client Applications by Default"
But once I got the SharePoint owner to change the SharePoint settings to "Open Documents in Client Applications by Default" the << ActiveWorkbook.LockServerFile >> command worked.
Maybe check out the following link to check if the file is already locked.
https://www.mrexcel.com/forum/excel-questions/906983-vba-support-checking-if-file-locked-sharepoint.html
Also in general it helps if you use your objects when you set them.
Dim DB as Workbook
Set DB = Workbooks.Open(filename:=DBname, editable:=True)
DB.LockServerFile
I had a similar issue. The code crashes at the same point you highlighted.
ActiveWorkbook.LockServerFile
Seems to fail when the document is already editable.
On Error Resume Next
ActiveWorkbook.LockServerFile
Fixes the issue I was experiencing since the code will continue on when the file is already editable and similarly makes a file editable if it wasn't previously.
I need to edit macro enabled Excel workbooks in SharePoint from Excel files stored locally.
Many users can try to update a SharePoint file at the same time. A yellow belt pops-up with information: READ ONLY We opened this workbook read-only from the server (see screen below).
I've a solution to skip that notification:
Private Sub Workbook_Open()
ActiveWorkbook.LockServerFile
End Sub
The problem is when one user has .LockServerFile enabled, another notification window shows for other users, notifying that the file is in use (see screen below).
Is it possible to avoid showing this window? Then ActiveWorkbook.LockServerFile will raise an exception, so I could handle it.
Private Sub Workbook_Open()
'Excel.Application.DisplayAlerts = False 'does not work
'ActiveWorkbook.ReadOnly = True 'always is read only before .LockServerFile
ActiveWorkbook.LockServerFile
End Sub
Or maybe there's some other way to update Excel files on Sharepoint, using Excel 2016?
I have a userform that is used to generate reports.
In case i have to share the userform with someone i share the entire excel sheet.
Is it possible to make the existing user form as an Add-in.so that,once installed it can be accessible through any excel sheet that is opened and not just that particular excel sheet.
Thanks in advance.
Yes, you can, but, you need to do a bit of preparation.
Ensure your addin has a Project name that differs from the name of your workbook. For example, if your user's workbook's Project is called VBAProject, then your add-in's project name must be (and should be named something more appropriate anyway) as something like MyAddin.
So, you have:
Book1.xlsm (Project name = VBAProject), and
MyAddin.xlam (Project name MyAddin)
Steps:
Within Book1/VBAProject, add a reference (Tools..References) to MyAddin.
Within MyAddin, create your UserForm (MyUserForm)
For early-binding, we need to make the form instancing PublicNotCreatable, but the VBE UI doesn't offer that property for forms, so we need to export the form to a file folder, then edit the MyUserForm.frm file, changing the Attribute VB_Exposed attribute to True (by default it's False). That is, in a text editor, edit the exported file named MyUserForm.frm and adjust the existing line as follows:
Attribute VB_Exposed = True
Save the file changes, (delete the original form in MyAddin) and then Import the MyUserForm.frm into the project. The new user form will have PublicNotCreatable instancing.
Add a public factory function to MyAddin, that will create a new instance of the form, and return it to any VBA that calls it:
Public Function GetUserForm() As MyUserForm
Set GetUserForm = New MyUserForm
End Function
In Book1.xlsm, you can now write code like the following, along with full early-binding support.
Public Sub test()
Dim frm As MyAddin.MyUserForm
Set frm = MyAddin.GetUserForm()
frm.Show
End Sub
Yes, you can share the userform.
Right click on the userform and Export it.
And you can add the form by Importing it.
Thanks