I'm generating automatic report with excel as back-end and I'm starting from a predifined template in which I'm only feeding missing parts:
excelApp = actxserver('Excel.Application');
excelWB = excelApp.Workbooks.Open('Mytemplate.xslx');
excelWB.Saved = false;
... Feed template with effective data ...
What I would like is to be able to reset the fullname of the opened template workbook so that when the user clicks on the close button he will prompt for Save, SaveAs as usual but clicking Saveshould ask for a file location and not overwrite the exsiting template.
Is it possible ? (I tried resetting manually Path and FullName properties of the workbook object but these are read-only so it doesn't work).
Save your "Mytemplate.xlsx" file as a Workbook Template instead (".xltx" file). When you open a template file it creates a new instance of the file each time instead of opening the original file. Additionally the default behavior of this new file is that the "Save" method will prompt for a file location.
Related
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 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/
I have a template that we use at work that I want to edit using visual basic. I have a gui built that asks the user for a few pieces of data. I need to open the template and just edit the contents of the template based on the user input. I have done research so far and everything I've seen shows me how to open a new excel document, not a current one. How do I open the current template?
*I get the user to browse for and select the filename and have that stored as a variable
Hard to say what you want to achieve maybe more clarification is needed.
To reference an Opened Excel file
Set execlObj = GetObject(fileName)
To open an Unopened Excel file (in separate Excel application)
Dim oXLApp As Object, wb As Object
Set oXLApp = CreateObject("Excel.Application")
Set wb = oXLApp.Workbooks.Open(fileName)
Where file name is e.g. c:\test.xls
I have a folder of .xls files of which some are genuine excel files and some are excel xml files (but still with the .xls extension). To work with the files in a separate program, I'm trying to convert them all to a consistent format (Excel 98-2004) and want to use an applescript to do this while retaining the original file names and directory structure.
As of now, I have a working loop and list of files, and am accomplishing everything but the save function:
set file_Name to "/Users/me/path/to/data.xls"
tell application "Microsoft Excel"
activate
open file_Name
save workbook as workbook (active workbook) filename file_Name file format (Excel98to2004 file format) with overwrite
close workbooks
quit
end tell
When I run this code, I get the following replies:
tell application "Microsoft Excel"
activate
open "/Users/drewmcdonald/Desktop/Madhupur_10February2014.xls"
get active workbook
--> active workbook
save workbook as workbook (active workbook) filename "/Users/drewmcdonald/Desktop/test.xlsx"
--> missing value
close every workbook
quit
end tell
The spreadsheets are successfully opening and closing, but the file format is not changing and I don't know what to make of the "missing value" error. Any idea how to get this to work? Thanks!
EDIT: Just noticed that the file format parameter is missing from the reply text, so I'm guessing the problem is in there.
Got it.
I needed to set a workbook name variable to get full name of active workbook before trying to save it. This is because the workbook name that Excel assigns as it opens sheets is somewhat inconsistent. Final code looks like this:
set file_Name to "/Users/me/path/to/data.xls"
tell application "Microsoft Excel"
activate
open file_Name
set wkbk_name to get full name of active workbook
save workbook as workbook wkbk_name filename file_Name file format Excel98to2004 file format with overwrite
close workbooks
quit
end tell
In Excel I am writing a macro to move and format data between two files. I begin by opening the first file and running a macro that allows you to choose a file.
I am opening a file using this code:
myFileName= Application.GetOpenFilename(filefilter:="All Files, .", Title:="All Files")
I then follow it with this code:
Workbooks.Open Filename:=myFileName
Later in the code Module, I would like to copy and past things between this newly opened file and the one I ran the macro from inside. Currently I am having to hard code their names like so:
Windows("data.xlsx").Activate
How can I store the name of the newly opened file in a string and how can I get the name of the excel file I am in into a string?
Thanks
Before opening the second workbook assign the name of the current workbook to a variable:
nameFirstWorkbook = ActiveWorkbook.Name
You already get the name of the second workbook from the open file dialog.