The simplest way to create a handle to your currently active Excel sheets in your .ahk script is:
Xl := ComObjActive("Excel.Application")
Nonetheless, if one switches to another workbook, this becomes the new "currently active sheet" and AutoHotkey tries to use methods for sheets and cells on the new workbook through COM: of course scripts designed to work with specific sheets and cells don't work anymore on a different workbook.
Do you know how to create COM handles to specific workbooks instead of currently active sheet?
The goal should be to allow the user to loop between workbooks without Xl object losing its previous handle and going to a new one.
Example
Open an Excel workbook from scratch and type 1234 in cell A1 of sheet named "Sheet1"; then create a new .ahk script with the following content:
#Persistent
Xl := ComObjActive("Excel.Application")
SetTimer, xlRead, 5000
Return
xlRead:
{
value := Xl.Sheets("Sheets1").Range("A1").Value
MsgBox, %value%
}
Return
Script above should display "1234" in a message box every 5 seconds.
While that is running, open a new workbook and type 5678 in cell A1 of sheet named "Sheet1" and wait for 5 seconds: according to my trials, AutoHotkey should just switch the handle to the new active sheet and show a message box whose content is "5678".
Any way to keep it linked to the first sheet? Of course assume one can save Excel files to hard disk with proper names which COM can refer to.
one way is to store the active workbook object in a variable like this
#Persistent
oExcel := ComObjActive("Excel.Application")
this_book := oExcel.ActiveWorkbook
SetTimer, xlRead, 10000
Return
xlRead:
{
value := this_book.Sheets(1).Range("A1").Value
MsgBox, %value%
}
Return
2nd way is to use ComObjGet with the full name of the active workbook if you know it before hand no need for the ControlGetText command just use the workbooks full name
#Persistent
SetTitleMatchMode, 2
ControlGetText, WorkBookName, Excel71, Microsoft Excel
oWorkbook := ComObjGet(WorkBookName)
SetTimer, xlRead, 10000
Return
xlRead:
{
value := oWorkbook.Sheets(1).Range("A1").Value
MsgBox, %value%
}
Return
You can also use ComObjGet with the full path of an excel file it will return the workbook object
#Persistent
fileselectfile, path
oWorkbook := ComObjGet(path)
SetTimer, xlRead, 10000
Return
xlRead:
{
value := oWorkbook.Sheets(1).Range("A1").Value
MsgBox, %value%
}
Return
Hope this helps you do what you need
Related
I want to reaf an excel file with the help of Client_OLE2 in my Oracle forms.
I succeed to do that if the excel just has one sheet because this sheet is the activesheet, but if I have several sheets in my excel,I am not able to read the desire sheet.
----------------Get the file to open ---------------------------
v_fName := :upload.ti_file_name_upload;
IF ( v_fName IS NOT NULL ) THEN
clear_form ( no_validate );
--------------INITIATE EXCEL APPLICATION---------------------------
application := client_OLE2.create_obj('Excel.Application');
client_OLE2.set_property(application,'Visible', 'false');
----------------GET WORKBOOKS FROM EXCEL APPLICATION---------------
workbooks := client_OLE2.Get_Obj_Property(application, 'Workbooks');
----------------OPEN REQUIRED WORKBOOK-----------------------------
args := client_OLE2.CREATE_ARGLIST;
client_OLE2.add_arg(args,v_fName);
workbook := client_OLE2.GET_OBJ_PROPERTY(workbooks,'Open',args);
client_OLE2.destroy_arglist(args);
----------------OPEN REQUIRED WORKSHEET----------------------------
worksheets := client_OLE2.GET_OBJ_PROPERTY(workbook, 'Worksheets');
worksheet := client_OLE2.GET_OBJ_PROPERTY(application,'activesheet');
----------------Specify columns that must be read------------------
Thank you!
I replaced
worksheets := client_OLE2.GET_OBJ_PROPERTY(workbook, 'Worksheets');
worksheet := client_OLE2.GET_OBJ_PROPERTY(application,'activesheet');
with
args:= client_OLE2.create_arglist;
client_OLE2.add_arg(args, 'Sheet Name');
worksheet := client_OLE2.GET_OBJ_PROPERTY(workbook, 'Sheets', args);
client_OLE2.destroy_arglist(args);
and now it works fine!
I am looking to import/copy data from many workbooks into a summary workbook. The workbooks are arranged in different sub-folders, I.e
C:\data1\results_2001.xlm
C:\data2\results_2002.xlm
C:\data3\results_2003.xlm
The names are similar but differ slightly to differentiate them. At present, I import the files individually, and I want to automate the process. The results files (above) are amongst other excel files so I cannot target them by file type.
How would I import these files by partial file name?
One option is to create an array of the filepaths to your excel sheets and then loop over the array and get the data you want into your summary sheet.
Sub CreateSummary()
Dim wkbs() As Variant, wkb As Integer, owb As Workbook
wkbs = Array("C:\data1\results_2001.xlm", "C:\data2\results_2002.xlm", "C:\data3\results_2003.xlm")
For wkb = 0 To UBound(wkbs)
Set owb = Application.Workbooks.Open(wkbs(wkb)) //Open each workbook
With owb
//Get the data you want into your summary workbook
.Close
End With
Next wkb
End Sub
Another way, especially if only a one time operation: Go into Cmd.exe, do a Dir for the files you're looking for, and send it to a text file (eg, something like dir c:\results_*.xlm /s /b > c:\myList.txt). Then import the text file to your worksheet, step thru each cell in the list, opening each workbook in turn.
You can do it in any languages, but for you who is asking this question, i think it's gonna be a little challenging, so here is what you need to do :
create a function that will list files/folders from given path
loop through all items found, if it's a folder , recursive it
if the item fits your target(name, extension, ...) , read it and load the content to the summary
something like this, i believe you will achieve this easily using VBA, look here
Literally, it will be like this, please note that this is not valid code, just something i write down to help you figure it out :
function loopthepath (string pathtoloop)
foreach(dirItem item in pathtoloop.getdirItem)
{
if (item is folder)
{
loopthepath(pathtoloop + item)
}
else
{
if (item fits mydescription)
{
load the content to the summary
}
}
}
When my Office Writer Excel report opens, it randomly un-hides some of the hidden cells and columns. I have verified that it is not the data that causes the columns or cells to not be hidden. Has anyone experienced this before and is there a way to make sure that all columns or cells stay hidden when the excel file is opened?
I work for SoftArtisans. We have not had any other reports of programmatically hidden columns becoming visible in the output file. We also have not been able to reproduce the behavior you are reporting. It would be helpful to see a code snippet, as well as to know which version of OfficeWriter you are using and which version of Excel is being used to open the output file.
There are two ways to hide columns with our API, both using the ColumnProperties object. You can set the hidden property to true or set the width property to zero. You could do both if you like, although that shouldn't be necessary.
For example:
ExcelApplication xla = new ExcelApplication();
Workbook wb = xla.Create(ExcelApplication.FileFormat.Xlsx);
//or if opening an existing workbook
//Workbook wb = xla.Open(inputFilePath);
//Get a handle on the worksheet
Worksheet ws = wb.Worksheets[0];
//Write a value to a cell
ws.Cells[0, 9].Value = "Hidden Value";
//Get a handle on the column you want to hide
ColumnProperties colProps = ws.GetColumnProperties(9);
//set the column to hidden
colProps.Hidden = true;
//or set the column width to zero
colProps.Width = 0;
//Stream the output file to the response
xla.Save(wb, Page.Response, "HiddenColumnTest.xlsx", false);
How do I add more sheets to an excel workbook from within matlab?
I set up the workbook like so (based on code I got from someone else's post in this forum):
%# create Excel COM Server
Excel = actxserver('Excel.Application');
Excel.Visible = true;
%# create new XLS file
wb = Excel.Workbooks.Add();
wsheet=1;
wb.Sheets.Item(wsheet).Activate();
That's fine. Then later on inside the loop I open a new sheet after so many loops:
...
if loop==sheetlimit,
wsheet=wsheet+1;
wb.Sheets.Item(wsheet).Activate();
end
This works up to sheet 3. But when wsheet=4 I get this error message:
??? Invoke Error, Dispatch Exception: Invalid index.
Error in ==> filename at 97
wb.Sheets.Item(wsheet).Activate();
Appreciate any help. Thanks.
I don't know Matlab but I would be surprised if wb.Sheets.Item(wsheet).Activate(); is actually adding any new worksheets. Most likely it is selecting / activating each worksheet in your wb workbook and your default Excel template has three worksheets. Hence why it errors when it gets to more than three.
Something like this might add a new Excel worksheet:
wb.sheets.Add();
Aargh - comment formatting completely messed up - I'll re-enter it as an new answer
Yes wb.sheets.Add(); will work. You can query the available methods of an interface like this:
methods(wb.sheets)
which gives:
Methods for class Interface.000208D7_0000_0000_C000_000000000046:
Add FillAcrossSheets PrintOut addproperty events loadobj set
Copy Item PrintPreview delete get release
Delete Move Select deleteproperty invoke saveobj
I'm trying to get the Workbook in my application(VSTO) like this :
ExcelViewModel mb = new ViewModels.ExcelViewModel();
string NameBox = mb.Workbooks.First().Name;
So when Excel open, it already have the default workbook : Book1.xlsx i open for example a second workbook(example.xlsx) but my string NameBox return the "Book1". How can i get the active Workbook? for my example the "example.xlsx" one.
The ExcelViewModel just return me an ObservableCollection of all Workbooks.
Thank you.
I found it :
Globals.ThisAddin.Application.ActiveWorkbook.Name