1
votes

I have 3 projects consists of one windows form application(WFA) and two excel workbooks in 1 solutions..

I would like to call/open both the workbooks by clicking different buttons in the WFA from the same solutions.. And I am stuck on the coding.. Have no idea how to code to open, activate, ...etc

Anyone have any idea can help?

*ps: I am very new to vsto/c#.net

I tried this.. But the workbook does not open..

private void Button_Click(object sender, EventArgs e)
{
     Microsoft.Office.Interop.Excel.Application xlApp;
     Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
     Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
     object misValue = System.Reflection.Missing.Value;

     xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
     xlWorkBook = xlApp.Workbooks.Open(@"C:\Document\PROJECT\abc.xlsx");
     xlWorkSheet = (Microsoft.Office.Interop.Excel.xlWorkSheet)xlWorkBook.Worksheets.get_Item(1);

     ((Microsoft.Office.Interop.Excel._Worksheet)xlWorkBook).Activate();

     xlWorkBook.Close(true, misValue, misValue);
     xlApp.Quit(); 
    }
2
Hi Ben, Yes..I had added the codes i tried in my question.. - wendxxi

2 Answers

1
votes

Simple tweak on your code worked

Microsoft.Office.Interop.Excel.Application xlApp;
            Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
            Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;

            xlApp = new Microsoft.Office.Interop.Excel.Application();
            xlApp.Visible = true;
            xlWorkBook = xlApp.Workbooks.Open(@"C:\Users\knm\Documents\Book2.xlsx");
            xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            xlWorkSheet.Activate();


            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();
0
votes

VB.net

   Private Sub Button1_Click(sender As System.Object, e As    Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Button1.Click
    Dim Dirdata As String = "\\drive\folder\Template.xlsb"

    Dim xlApp As Microsoft.Office.Interop.Excel.Application
    Dim xlWorkbook As Microsoft.Office.Interop.Excel.Workbook
    Dim xlWorksheet  as Microsoft.Office.Interop.Excel.Worksheet
    xlApp = New Microsoft.Office.Interop.Excel.Application()
    xlWorkbook = xlApp.Workbooks.Open(Dirdata, , True, , "password")
    xlApp.Visible = True

    xlWorksheet = xlWorkbook.Worksheets(1)
    xlWorksheet.Activate()

    MsgBox(xlWorkbook.Name)

    xlWorkbook.Close(False)

End Sub