VBS GetObject will not bind to saved workbooks - vba

I'm trying to write a script which will switch to a specific excel workbook and enter keys, however, the script will only run if the workbook which is being referenced is unsaved.
For example:
If I open a new excel workbook which creates a new workbook named "Book1"; the following works:
Set xl = GetObject("Book1").Application
If I run the above when the workbook has been saved, even if the name remains the same, the above results in the following error: (0x800401E4).
Is anyone aware of this being a problem, or can anyone point out what I'm doing wrong? I'd guess the error has something to do with the object name being different from the actual file name, so when excel creates the default workbook the code works because the name of the instance is "Book1" whereas the saved "Book1" is assigned another object name when saved?

I wasn't sure if I needed to close this question as answered, however, it seems that https://stackoverflow.com/users/4088852/comintern has resolved the issue.
It needs to be the full path -
GetObject("C:\Users\Me\Documents\Book1.xlsm").
Thanks,

Related

How to reference a workbook from another excel instance

I believe my problem is rather simple: I have a workbook and I'm using it to get some data from another software (SAP). When I export the data from the software, it automatically opens a .xlsx file, then what I'd need to do is copy some of the data from this file, paste on my original workbook, and then close the file. The section of my code which is giving me an error is this one:
fileName = "temp1.xlsx"
Set wbBasis = Workbooks(fileName)
This happens because the "temp1.xlsx" file that was opened by the SAP software is in another instance of excel, so my code isn't able to find it.
What I need to know is basically this. How to properly reference this "temp1.xlsx" workbook on my original code so that I'm able to edit it, copy stuff from it, close it, etc.
I've found some similar topics to my problem, like the two I'm listing down here, but couldn't adapt the solutions to my situation, and that's why I'm posting this new one.
Having multiple excel instances launched, how can I get the Application Object for all of them?
Finding a workbook in one of multiple Excel instances
Thank you in advance.
You don't need multiple instances of Excel, and you don't need the Excel file to be open in order to get information from it, either with VBA, or even with regular "linked formulas".
For example:
=SUM([C:\yourPath\Filename.xlsx]SheetName!A1:C25)
...returns the sum of cells A1:C25 on a worksheet named Sheetname in an Excel file located at C:\yourPath\Filename.xlsx.
Or you can import all the data directly with Get External Data.
See:
MSDN : Create an external reference (link) to a cell range in another workbook
MSDN : Connect to another workbook
...for more information and examples, search Google for "get data from another Excel workbook".

How to use the Workbooks.add(template) to create a workbook in Excel 2016

I have been trying to do the following:
In a template I have built in Excel 2016. When the template opens,the Workbook Open event is used to create a new workbook from this now open template and save it as a 'xlsm' file.I can't get the Workbooks.Add(template) to work. This is what I have attempted so far:
dim wb as workbook
set wb=workbooks.add(Me) 'Run-time error 1004' Application-defined or object defined error.
set wb = workbooks.add("x:\Users\name\Documents\name of template.xltm")
Where name is user name and template is valid name copied from File Explorer.
' Run-time error 1004...
'Set wb = Workbooks.Add(Template:="C:\Users\Ed\Documents\Custom Office Templates\Itemized_Deduction.xltm") ' The exact path is shown in this attempt to show exactly what I was doing. I had to use a _ between the Itemized and Deduction- the compiler did not like it with just a space as it is shown in the folder. The error was the same.
It would sure help if MS would give complete examples of the samples so we,the users of their fine products, could get it the first time.
I have been to this site, MS developer, Office Developer,and God knows where else and everything that was shown does not work.
Am I supposed to use a template of a workbook that is closed?
Can a XLTM template be used in this scenario?
Can someone show me how to use the .Add(template) please?
I had given it some thought after sleeping on it again and getting my head out of my *&^67! I was making it way to complicated. All I had to do was do a Save As on the template and the problem took care of itself.
Thanks for the help and support Roy.

How to check if VBA module has been modified?

I have an Excel workbook that I would like to export the code for every time the workbook is closed but I would like to do so selectively so that each module will only get exported if it has been modified since the workbook was opened. I would like to be able to use the exported files to run a diff on the code and I'm assuming that re-exporting every time will cause the files to always be flagged as different. Here's an attempt at some pseudo-code to show what I mean:
for each module in Workbook
if module.isModified() then
module.export
end if
next module
VBComponent has a readonly property. Add reference to VBE and you should be able to access "changed" VBComponents
bool Saved
Saved Property - (Excel 2003 VBA Programmer's Reference Google Books)
I was just being lazy about it and hoping someone already new a method that I hadn't found. Sorry about that.
I found that I can use the following.
for each module in Workbook
if not module.saved then
module.export
end if
next module
Then, I'll just run the export function every time the workbook is saved.

Copy Method of Worksheet class fails with .xlam file

CONTEXT: I have created an Excel Add-In in which there is a worksheet named "Mother". When the user clicks a button from any workbook in which the Add-In is installed, I would like the worksheet "Mother" to be copied inside its same workbook and being renamed. In code:
ThisWorkbook.Sheets("Mother").Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
WHAT I EXPECT: being the line of code above placed into the .xlam code, I know that ThisWorkbook refers to the Add-In and this is observable through a watcher as well:
So, I expect that a copy of "Mother" will be added to the workbook, at the end.
WHAT HAPPENS: the method simply fails, returning the error Copy Method of Worksheet class failed.
WHAT I HAVE TRIED: By simply looking for the name of the error online I have figured out that, apparently, I cannot copy a hidden worksheet and that's why the method fails. So I have tried:
1) To change the visibility with ThisWorkbook.Sheets("Mother").Visible = True... No success;
2) To activate the workbook before/after to change the visibility, so ThisWorkbook.Activate... No success.
I'm not being able to find much documentation online about this, so I'm almost wondering if it's actually possible to edit/show the workbook.xlam, something that I would really need to do for my project.
A BIT MORE OF CONTEXT: The reason why I need to both add the worksheet and show it to the user is that the Add-In, which basically consists in a custom function =myFunction(), needs to be fed by user-inputs for make its calculus.
In particular, neither I can write the data in the sheet before to distribute the Add-In (because I don't know the user inputs in advance), nor I can prepare a user-form where to insert the data and show it at running-time (because the user-inputs are usually a big amount of data that can be copied-pasted easily into an Excel spreadsheet, but not inserted manually into a form nor in the function parameters themselves). So:
MY QUESTION: Is it possible to copy (duplicate) a worksheet within a .xlma workbook and activate it after to allow the user data insertion? If yes, where am I being wrong on the above?
Looks like this link would help: http://forums.techguy.org/business-applications/1120165-add-extra-sheet-xlam-file.html.
The operation cannot be performed on add-in workbook, hence you just need to set it as "not add in":
ThisWorkbook.IsAddIn = False
'your copy operation
This Workbook.IsAddIn = True

change worksheet name, a difficult one

When I use excel to open a .txt file (a notepad file), the worksheet name is the file name of the notepad file that was opened by default. Therefore, the sheet name will be different when open a different notepad file. Downstream code need this worksheet name be a fixed one. Is there anyway to make change the sheet name to a fixed name such as "sheet1". By the way, codename can not be used, since the macro to use the data in the open file is not another workbook.
Thanks!
You don't need the codename not the worksheet name when you are opening .txt files from Excel. There will always be 1 sheet. So in your code you can always address that sheet as
wb.Sheets(1)
Where wb is the workbook object.
For your reference every .txt file that you open with VBA cannot have a common name unless you set it via code. And if you do that, you will have to still use wb.Sheets(1)
For example
wb.Sheets(1).Name = "Blah Blah"
Could you call your text file sheet1.txt? Would that solve your problem?
I am picturing that your macro opens the text file dynamically because you want to use excel's built in csv parsing. Perhaps sorting and filtering the data afterwards.
Siddarth gave you a good lead, but you shouldn't worry about the name or the sheet because as he said you have the worksheet object to use for your downstream code.
wb.Sheets(1)
Now, if you want to reference this sheet outside of the subroutine that you opened the file. Use a global variable for your
wb