none
C# - Open Excel at this Worksheet using Process and ProcessStartInfo RRS feed

  • Question

  • Is it possible to open an Excel Workbook at this Worksheet using Process and ProcessStartInfo.  I don't want to use Microsoft.Interop approach because I want to process the changes made after exiting.  I was hopeing List of command-line switches for Office products would help, but there is none for opening at a specific Worksheet switch.

    Here my code:

    if (e.Parameter is WorkbookDictionary workbookDictionary)
    {
    	Type officeType = Type.GetTypeFromProgID("Excel.Application", true);
    
    	if (officeType != null)
    	{
    		BeforeWorkbookDictionary = workbookDictionary;
    		beforeFileInfo = new FileInfo(BeforeWorkbookDictionary.FilePath);
    		afterFileInfo = default(FileInfo);
    		ProgressController = await _dialogCoordinator.ShowProgressAsync(this, string.Format("Progressing {0}", BeforeWorkbookDictionary.FileName), "Waiting for modifions for update");
    		ProgressController.SetIndeterminate();
    
    		Process myProcess = new Process()
    		{
    			StartInfo = new ProcessStartInfo(BeforeWorkbookDictionary.FilePath) { WindowStyle = ProcessWindowStyle.Maximized }
    		};
    
    		ExitedEventArgs = Observable.FromEventPattern<object, EventArgs>(myProcess, MethodParameter.Exited);
    		DisposableExited = ExitedEventArgs.Subscribe(evt => OnExitedEvent(evt.Sender, evt.EventArgs));
    		myProcess.EnableRaisingEvents = true;
    		myProcess.Start();
    		myProcess.WaitForExit();
    		myProcess.Refresh();
    	}
    	else
    	{
    		throw new ArgumentException("Microsoft Excel is not installed!");
    	}
    }
    

    Thanks!...


    Code is like a box of chocolates!...

    Sunday, March 10, 2019 9:28 PM

Answers

  • Well after doing an intensive search and reading, I've found it better working with and using Microsoft.Office.Interop.Excel to be the best option for my project, it allows me to had more features.  So this is what I'm doing now:

    private void OpenInteropWorkbookDictionary(WorkbookDictionary workbookDictionary, WorksheetDictionary worksheetDictionary, bool activateWorksheet = false)
    {
    	if (ExcelApplication != null) Marshal.FinalReleaseComObject(ExcelApplication);
    	if (ExcelWorkbook != null) Marshal.FinalReleaseComObject(ExcelWorkbook);
    	if (ExcelWorksheet != null) Marshal.FinalReleaseComObject(ExcelWorksheet);
    	if (ExcelWorksheets != null) Marshal.FinalReleaseComObject(ExcelWorksheets);
    
    	ExcelApplication = new Microsoft.Office.Interop.Excel.Application
    	{
    		// if you want to make excel visible to user, set this property to true, false by default
    		Visible = true,
    		WindowState = Microsoft.Office.Interop.Excel.XlWindowState.xlMaximized
    	};
    
    	ExcelApplication.WorkbookOpen += new Microsoft.Office.Interop.Excel.AppEvents_WorkbookOpenEventHandler(OnWorkbookOpen);
    	ExcelApplication.WorkbookActivate += new Microsoft.Office.Interop.Excel.AppEvents_WorkbookActivateEventHandler(OnWorkbookActivate);
    	ExcelApplication.WorkbookDeactivate += new Microsoft.Office.Interop.Excel.AppEvents_WorkbookDeactivateEventHandler(OnWorkbookDeactivate);
    	ExcelApplication.WindowActivate += new Microsoft.Office.Interop.Excel.AppEvents_WindowActivateEventHandler(OnWindowActivate);
    	ExcelApplication.WindowDeactivate += new Microsoft.Office.Interop.Excel.AppEvents_WindowDeactivateEventHandler(OnWindowDeactivate);
    	ExcelApplication.WorkbookAfterSave += new Microsoft.Office.Interop.Excel.AppEvents_WorkbookAfterSaveEventHandler(OnWorkbookAfterSave);
    	ExcelApplication.WorkbookBeforeClose += new Microsoft.Office.Interop.Excel.AppEvents_WorkbookBeforeCloseEventHandler(OnWorkbookBeforeClose);
    
    	ExcelApplication.SheetBeforeDelete += new Microsoft.Office.Interop.Excel.AppEvents_SheetBeforeDeleteEventHandler(OnSheetBeforeDelete);
    	ExcelApplication.SheetActivate += new Microsoft.Office.Interop.Excel.AppEvents_SheetActivateEventHandler(OnSheetActivate);
    	ExcelApplication.SheetDeactivate += new Microsoft.Office.Interop.Excel.AppEvents_SheetDeactivateEventHandler(OnSheetDeactivate);
    	ExcelApplication.WorkbookNewSheet += new Microsoft.Office.Interop.Excel.AppEvents_WorkbookNewSheetEventHandler(OnWorkbookNewSheet);
    
    	// open an existing workbook
    	ExcelWorkbook = ExcelApplication.Workbooks.Open(BeforeWorkbookDictionary.FilePath, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
    	ExcelWorkbook.SheetChange += new Microsoft.Office.Interop.Excel.WorkbookEvents_SheetChangeEventHandler(OnSheetChange);
    
    	if (activateWorksheet)
    	{
    		ExcelWorksheet = ExcelWorkbook.Sheets[worksheetDictionary.Name];
    		ExcelWorksheet.Activate();
    	}
    }

    Now I can interact with Excel while its running to update any modifications the user makes. WindowDeactivate is the last event before it closes after WorkbookBeforeClose, so I can do other things if needed. Also, I keep the user from closing my application before closing Excel, to make sure they are finished and saved any changes.


    Code is like a box of chocolates!...




    • Marked as answer by VcDeveloper Wednesday, March 13, 2019 3:16 AM
    • Edited by VcDeveloper Wednesday, March 13, 2019 8:38 PM
    Wednesday, March 13, 2019 2:10 AM