-1

I would need to pass the file name ref instead of giving string (full path) while opening or saving Excel in C#. Below is a code snippet

object fileName = (string)e.Argument;

object oMissing = System.Reflection.Missing.Value;

if (fileName.ToString().EndsWith("xlsx"))
{
    Excel.Workbook wb;
    object oMissing1 = Type.Missing;
    var app = new Microsoft.Office.Interop.Excel.Application();
    wb = app.Workbooks.Open(@"C:\Users\273714\Desktop\ProoferReport1.xlsx", 
                            oMissing1, oMissing1, oMissing1, oMissing1, 
                            oMissing1, oMissing1, oMissing1, oMissing1, 
                            oMissing1, oMissing1, oMissing1, oMissing1, 
                            oMissing1, oMissing1);
    wb.SaveAs(@"C:\Users\273714\Desktop\Proofer Report2.xls", 
                            Excel.XlFileFormat.xlExcel8, Type.Missing, 
                            Type.Missing, Type.Missing, Type.Missing, 
                            Excel.XlSaveAsAccessMode.xlExclusive, 
                            Type.Missing, Type.Missing, Type.Missing, 
                            Type.Missing, Type.Missing);
    app.Quit();
    app.Quit();
}

In case of a word document i can just open it like

oDoc = oWord.Documents.Open(ref fileName, ref oMissing,
               ref readOnly, ref oMissing, ref oMissing, ref oMissing,
               ref oMissing, ref oMissing, ref oMissing, ref oMissing,
               ref oMissing, ref isVisible, ref oMissing, ref oMissing,
               ref oMissing, ref oMissing);

I need the same way of using the file name in Excel too. Any help will be grateful.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
user1665707
  • 615
  • 3
  • 11
  • 24
  • Not sure exactly what your question means. You want to use pass `filename.xlsx` instead of `C:\filename.xlsx`? – CodingIntrigue Sep 05 '13 at 06:55
  • i wanna pass the object filename there instead of string as i browse the file and open that. the file name keeps changing.i dont want to hardcode it – user1665707 Sep 05 '13 at 06:58
  • you have full path to file in `fileName` object? – Damith Sep 05 '13 at 07:06
  • So just use a file open dialog, get the user to select the file, check it is the correct file, then show a save as dialog for the user to save it again..... – iabbott Sep 05 '13 at 07:07
  • I'm confused.. why can't you just use the `fileName` variable? (or just `File.Copy`) – Sayse Sep 05 '13 at 07:08

1 Answers1

0

If I've understood correctly you just want to pass the variable FileName instead of passing a constant string "c:\aFilename.xlsx"?

To do that replace object filename with string filename and then type ref filename instead of ref @"c:\aFilename.xlsx".

Also be aware of the System.IO.FileInfo class - instead of doing fileName.ToString().EndsWith("xlsx") you could do Path.GetExtension(filename).Equals("xlsx",StringComparison.InvariantCultureIgnoreCase) to take advantage of the built in functions for finding file extensions (this also avoids (for example) filename.abcxlsx from passing the test).

JohnLBevan
  • 22,735
  • 13
  • 96
  • 178
  • 1
    `Path.GetExtension(fileName)` is a better alternative (Although I don't know what the question really is to say if this answers the question) – Sayse Sep 05 '13 at 07:13
  • Good point @Sayse - I'd used `System.IO.FileInfo` since originally I'd stuck the filename in there, then realised since it's being passed as a ref it would be better to store it in a string variable so a value could be returned, but stopped thinking at that point. Updating answer now. Thanks. – JohnLBevan Sep 05 '13 at 07:16