Getting path string from Excel in IronPython - excel

I am trying to set working directory through IronPython. Its basically for ANSYS Workbench. I am getting the directory path from excel and i am storing it in a variable in IronPython.
dirpath = worksheet.range["E25"].value
and I am giving this variable value as input path to AbsUserPathName and chdir commands.
dir = AbsUserPathName(dirpath)
os.chdir(dirpath)
But none of it is working, it gives error as expected str, got _comObject
Any help is appreciated.

Assuming you are using Microsoft.Office.Interop.Excel you could use one of the following statements:
dirpath = worksheet.Range["E25"].Text
or
dirpath = worksheet.Cells[25, "E"].Text
or
dirpath = worksheet.Cells[25, 5].Text
Your current statement exposes a COM object from the interop-API that might even represent multiple cells and therefore can not be used by chdir as there is no way of implicitly converting the range to a string.

Related

Open a workbook without knowing the exact name

so i searched but didnt find something good for my use , i have a folder where i will import an excel file , this excel file will have a different name everytime how i can open it with vba , thank you
You can get the file name using the Dir function and multiple character (*) wildcard.
Const Path As String = "C:\Test"
Dim filename As String
filename = Dir(Path & "\*.xlsx")
If Len(filename) > 0 Then
' Do your work
' Remember 'filename' only holds the file name
' you will need to attach the rest of the path to get the full directory.
End If
Note: If there's only one file in the folder you will not have any issues, however if the folder contains multiple files (matching the above pattern), you will need to either loop or provide additional file name characters to the function.
An example:
File name: daily_report_20190404.xlsx
filename = Dir(Path & "\daily_report_*.xlsx")
Hope this helps.

pass variable from excel to access

I have a VBA code in Excel that ask the user where an .xls file is a make something with that file.
What I'd like to do now is to pass the name of the file (myFile) to VBA code that I have in Access, in order to update a table with that file and other fields.
In VBA Excel, the name of the Sub is: Sub UpdateSupplier(myFile As String)
What is the code that I should use in VBA Access in order to use myFile?
Try this: Declare your variable myFile as public, that means not write:
Dim myFile in your Excel Sub,
but write instead
Public myFile in the Declare Section of the module.
Then create a new Function in this module, which only gives back this public variable myFile:
function GiveMemyFile() as string
GiveMemyFile = myFile
end function
Now you can call this function from Access to get the value of myFile.
think I found the solution now, it's so simple if you found it:
Your 'Makro' in Excel should look like this:
function Test (ByRef strFilename)
strFilename = yourFilename
end function
The first 'trick' is, to use a ByRef Parameter 'strFilename' to give back your Filename.
Now in Access you can use:
strNewFilename = ""
xl.Run "Test", strNewFilename
Debug.Print strNewFilename
and you will see the filename the Excel Macro wrote in the variable.
I have tested this and it worked fine for me. Good luck!

Importing multiple excel sheets with multiple tabs into matlab

Hello and good day to all,
I am trying to import some excel sheets having multiple tabs into the matlab. For this I have written small loop but after many tries and adjustments still cant get all the data into matlab. In the source directory I have 15 excel sheets with 8 tabs each containing data in the 52 x 102 cells. The data is in signed form meaning containing positive and negative values. Here below is the code I was working on and I applied different changes which I found on the internet but no success.
srcdir = 'path to the folder';
srcfiles = dir(fullfile(srcdir, '*.xls'));
for i = 1:length(srcfiles)
[status,sheets] = xlsfinfo(srcfiles(i));
for s = 1:numel(sheets)
[data,titles]=xlsread(srcfiles(i).name,sheets{s});
end
end
Right now I am getting this error " Filename must be a string". I even tried to change it to the char to string but still didn't work.
Only once it worked When I instead of giving the path in the source directory i.e srcdir, gave the name of the file directly in xlsread().
Can anybody help where am I doing mistakes?. Thank you
You likely need to specify the full path to the file using fullfile. Also, in the outer for loop you'll need to use srcfiles(i).name instead of srcfiles(i)
srcdir = 'path to the folder';
srcfiles = dir(fullfile(srcdir, '*.xls'));
for k = 1:numel(srcfiles)
filename = fullfile(srcdir, srcfiles(k).name);
[status,sheets] = xlsfinfo(filename);
for s = 1:numel(sheets)
[data,titles] = xlsread(filename, sheets{s});
end
end

How to read a specific range of several excel files in MATLAB

I want to write a program that is able to read a specific range of numerous excel files in a folder.
because I need MATLAB to read from several excel files, I can't use a coding like this :
xlsread('Report1',1,'k41')
Is it possible to modify below codes in a way to be able to read 'K41' cellular from each excel file?
clc
clear all
Folder = 'D:\Program Files\MATLAB\R2013a\bin';
XLfiles = dir(fullfile(Folder, '*.xlsx'));
for i = 1:length(XLfiles)
data = xlsread(fullfile(Folder, XLfiles(i).name));
end
As excaza said, xlsread should work, just check the 'range' parameter from xlsread, needs to be a string in this format (this is to import only C1).
'C1:C2'
If you use K41:K41 it imports nothing, as it is a 0 range. Maybe that is the confusion here.
See if this might work
all_cells = []; %store all your cells in here
Folder = 'D:\Program Files\MATLAB\R2013a\bin';
XLfiles = dir(fullfile(Folder, '*.xlsx'));
for i = 1:length(XLfiles)
all_cells (end+1)= xlsread(fullfile(Folder, XLfiles(i).name), 'K41:K42');
end

Trying to convert XLS to CSV and CSV to XLS using VBScript. Receiving error: 800A03EC (Unable to get the Open property of the Workbooks class)

I'm trying to open a CSV then use the SaveAs method to save it as an XLS. Also vice-versa in another script. I accidentally had the file format codes wrong before and was not getting this error. The CSV would in fact open. I accidentally had made the CSV format 2 (which is actually SYLK) and the XLS, 6 (which is actually CSV).
I've looked all over, and most of what I can find has to do with using an incorrect argument (which I have checked multiple times). The rest is for ASP, and suggests changing permissions in Component Services (which probably wouldn't be an issue anyway, since I can get the Open method to work with different formats).
So I'm at a loss as to how to proceed. If I can't even use the Open method, then I'm kind of stuck. If it was as simple as thee SaveAs method not working for this task, I could get around that. But I need to be able to open an XLS using the Open method (since I'm also trying to do XLS to CSV). CSV to XLS can be fixed another way, probably, since the Open method seems to work sometimes.
Anyway, my code for the CSV to XLS is below. The XLS to CSV is essentially identical to this. It just flips the format codes and uses different paths for the files.
strName = "MidCSVTemp.csv"
strSaveName = Month(Now) & "." & Day(Now) & "." & Year(Now) & ".xls"
strPath = "C:\Users\adam\Documents\" & strName
strSavePath = "C:\Users\adam\Documents\" & strSaveName
'Options for Workbook.Open
intUpdateLinks = 0
boolReadOnly = False
intFormat = 6
'Options for SaveAs
intFileFormat = 56
Set objExcel = CreateObject("Excel.Application")
Set objWorkBook = objExcel.Workbooks.Open(strPath,intUpdateLinks,boolReadOnly,intFormat)
Call objWorkBook.SaveAs(strSavePath,intFileFormat)
Call objWorkbook.Close
I think the issue is that you're using the Format parameter of the Workbooks.Open() method like it's the FileFormat parameter. It shouldn't be xlCSV (6), which is a FileFormat constant. According to the docs, Format should be one of the following values:
1 = Tabs
2 = Commas
3 = Spaces
4 = Semicolons
5 = Nothing
6 = Custom
Since you're passing a value of 6, it's expecting that you also include the Delimiter argument. And since you're not including it, you're getting an error.
You should be able to open a CSV without specifying the Format parameter (Excel seemed to guess the delimiter correctly for me without having to specify it). But, to be safe, pass a value of 2 for a comma-delimited (CSV) file.
intFormat = 2
Set objWorkBook = objExcel.Workbooks.Open(strPath,intUpdateLinks,boolReadOnly,intFormat)

Resources