4

I get currentWrkBook by fileName with the code below:

using Excel = Microsoft.Office.Interop.Excel;
...
Excel.Workbook currentWrkBook = Excel.Application.Workbooks.Open(fileName);
...

Result get OK (when run to the statement above, the fileName[D:/DataProject/Resources.xlsm] is open):

enter image description here

Now, I would like get currentWrkBook by fileName without open file.

That is possible? Any tips on these will be great help. Thanks in advance.

MinhKiyo
  • 191
  • 3
  • 15
  • What are you trying to achieve with this? – default locale Oct 25 '16 at 04:00
  • 1
    Because every time run, the fileName is opened, so annoying, so I want it to be hidden away – MinhKiyo Oct 25 '16 at 04:05
  • So, you still want to open the file, just hide excel application from user. In this case set [Application.Visible](https://msdn.microsoft.com/en-us/library/office/ff198119.aspx) property to false. Also this question might be useful: http://stackoverflow.com/questions/8179835/c-sharp-show-hide-excel-window – default locale Oct 25 '16 at 04:20
  • Thank you for reply. This way I've tried before but it can not. The fileName has been opened up and the hidden. I think there is no way not open fileName that can to get workbook. – MinhKiyo Oct 25 '16 at 04:35

2 Answers2

2

Try to open your file from new Application object:

var excel = new Excel.Application();
var workbook = excel.Workbooks.Open(filename);

It is possible, that you have excel instance already running, then new workbook becomes visible as well.

EDIT: In case if you need to get a reference for a workbook that is already opened you can get it by name:

Excel.Workbook currentWrkBook = Excel.Application.Workbooks[fileName];
default locale
  • 13,035
  • 13
  • 56
  • 62
  • Because using method Open() should definitely the fileName is opened. After opening the first time, then set Application.Visible property to false, it will hide. So I think there is no way to do that. – MinhKiyo Oct 25 '16 at 04:53
  • Surely, there's a way to do this :) You need to be more specific, though. Is this file already open? Was it opened by your application or another one? Also, I've updated my answer, see the edit. – default locale Oct 25 '16 at 05:02
  • My's problem is I have a filename that is not already opened. And Excel application is openning another file. ^^ I understand. Thank you for your effort! – MinhKiyo Oct 25 '16 at 05:17
2

I seem to be able to do this by opening the file in read-only mode. For me, that's good enough as I just need to read the data.

var oXL = new Microsoft.Office.Interop.Excel.Application();
oXL.Visible = false;

Workbook Wbook = oXL.Workbooks.Open(txtFileName.Text, ReadOnly: true, Password: "password");

var sheet = Wbook.Worksheets[1];

foreach (Range row in sheet.UsedRange.Rows)
{
    var firstName = sheet.Cells[row.Row, 1].Value2,
    var lastName = sheet.Cells[row.Row, 2].Value2
}

If I switch the ReadOnly parameter to false, then Excel will open the file visibly.

Trent
  • 1,280
  • 11
  • 12