How to open and edit an excel template using visual basic - excel

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

Related

VBA - File copy inherits read-only mode

I have an MS Access application that uses VBA for a number of things. I need to add a row to an excel spreadsheet before auto-importing the data - trust me there is no better way in this case. Right now, this works as long as the spreadsheet isn't open by someone. However, if it is, I can't modify the spreadsheet and I get the pop-up of the spreadsheet in read-only mode. To get around this, I added code to copy the existing spreadsheet to a temp folder and set the "Read-only" value to normal. This SHOULD work - I think. However, even when I try this, I still get the read-only error. YET, when I check the value of my TEMP file, it's not set to read-only!!!
I'm guessing that read-only is really "locked by another user."
How can I accomplish my goal of getting access to edit that file / a temp copy?
Below is the code where I get the message:
Dim fso as Object
Set fso = VBA.CreateObject("Scripting.FileSystemObject")
Call fso.CopyFile(myOriginalFolderFile, myTempFolderFile)
' copyfile works successfully
SetAttr myTempFolderFile, vbNormal
' still no error
Dim my_xl_app as Object
Dim my_xl_worksbook as Object
Set my_xl_app = CreateObject("Excel.Application")
Set my_xl_workbook = my_xl_app.Worksbooks.Open(myTempFolderFile)
' this is where it throws the message
In the Save dialog box, there is a TINY option for "Tools" and "General Options" and it has a checkbox next to ‘Read-only recommended’.

Activate Workbook WITHOUT filename but using the extension

I have slowly learning some VBA by searching them through google, this site has the best format for understanding the answers. I have been able to really automate many of my reports but have hit a wall in searching out this function.
I have a many reports and we use a web interface to generate specific data. the file names are always randomly generated when exported, but they are always (read only) .xls files.
I have tried other VBA commands that i have searched heavily but none work. using the "*.xls" doesnt seem to work. these have no file path, but are the only .xls workbook open when running this report.
I would appreciate any assistance.
Welcome to SO.
In your question you say have no file path, but are the only .xls workbook open when running this report.
If this is true (the .xls workbook you want is the ONLY one opened at this time), then this code may work for you.
It will loop trough each opened workbook and check the extension of the file. If it is XLS then it will activate it.
Dim f As FileSystemObject
Set f = New FileSystemObject
Dim WB As Workbook
For Each WB In Application.Workbooks
If f.GetExtensionName(WB.FullName) = "xls" Then
WB.Activate
'rest of your code
End If
Next WB
Set f = Nothing
To make this code work, you need to activate a reference:
In the VBA Editor, go to TOOLS-> REFERENCES and search for Microsoft Scripting Runtime
Hope this can help you

How to open an embedded object in spreadsheet as read only through excel VBA

I have few objects embedded in a hidden sheet in workbook which runs the VBA code. These objects (word, excel, pdf etc.) are just the templates and I need to open a copy of these or open these as read only upon a click on a command button, so that the template content remains same.
I have searched over the internet but did not find a way to open these embedded objects as read only. I am running this code but save as operation is not successful.
Private Sub M114_Click()
Dim WDObj As Object
Dim WDApp As Object
Set WDApp = GetObject(, "Word.Application")
Set WDObj = Sheets("Tools").OLEObjects("MO")
WDObj.Activate
WDApp.ActiveDocument.SaveAs ("MO_copy.doc")
Set WDObj = Nothing
Set WDApp = Nothing
End Sub
You need to protect the document to prevent changes before you embed it in Excel.
With the example of a Word Document:
You can use Word's protection functionality to protect it, and could optionally give different rights to different users.
Go to the Review tab on the ribbon.
In the Protect group, click Restrict Editing
In section 2 of the Restrict Editing options on the side of the screen, check the box that says "Allow only this type of editing" and make sure the dropdown is set to **No Changes (Read Only)**.
Set other protection options as desired.
In section 3 click Yes, start enforcing.
Save & close the document and then embed in your Excel file.
Alternatively, if your embedded object is linked to the source file, you can set the file to Read Only in the Windows file properties. This won't work if the object isn't linked to the file.

Excel automation - Reset fullname of a template workbook

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.

Get table data in Excel 2007 from query in Access 2007

I have an automated process that is mostly run in Access. But, in the middle, it puts some data in Excel to scrub it into the correct form (it's much faster than doing it in Access), and at the end it opens another Excel file and puts data from some Access queries into the Excel file. For these connections from Excel to Access, I accomplished them all by going into Excel and doing Data --> Get External Data --> From Access, then selecting the Access file and the query I want to get the data from and tell Excel to make it into a Table.
So, I do that one time and then I want to be able to run this automated process that simply refreshes the data. To do this refreshing of the data, I do a line like:
Worksheets("Data").Range("A1").ListObject.QueryTable.Refresh _
BackgroundQuery:=False
The problem is, half the time (and I can't figure out why it does it one time and not another), it says "Do you want to connect to path\filename?" Of course I do, how else would the table refresh? So, this stops the automation. Even if I click Yes, I still can't get it to continue on. If I click Yes, it opens up the Data Link Properties. After I click OK for that, it opens a window titled "Please Enter Microsoft Office Access Database Engine OLE DB Initialization Information". It has info in it, including the path and name of the data source I want to access, but if I click OK, it says, sorry that didn't work, would you like instead to connect to (and then it lists the exact same path and file name it just said didn't work). It repeats the steps I just mentioned, and after that it errors out.
In case it matters, here is the (basic idea) code I use to connect to Excel from Access:
Public Sub ExportToExcel()
Dim ObjXLApp As Object
Dim ObjXLBook As Object
Dim ExcelFilePath As String
ExcelFilePath = CurrentProject.Path & "\"
Set ObjXLApp = CreateObject("Excel.Application")
Set ObjXLBook = ObjXLApp.Workbooks.Open(ExcelFilePath & "filename.xlsm")
ObjXLApp.Visible = True
' Runs the "DataSetUp" macro in the Excel file.
ObjXLApp.Run ("DataSetUp")
' The DataSetUp macro saves the Excel file
' Quit Excel
ObjXLApp.Quit
' Free the memory
Set ObjXLBook = Nothing
Set ObjXLApp = Nothing
End Sub
I have no idea how to fix this! Any help would be much appreciated.
This may be happening because your access database is still open from which the new excel file needs to input data back into. The database cannot be open when this takes place, hense the reason why excel errors and asks for another location to connect to.
So, I would work on generating the needed scrubbing via vba inside access probably.

Resources