Skip to content

Commit

Permalink
Fix some minor bug cause crash Excel
Browse files Browse the repository at this point in the history
  • Loading branch information
KitCognac committed Aug 11, 2020
1 parent adc2dba commit e6ff236
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
19 changes: 17 additions & 2 deletions XLIG/Init.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,31 @@ public void AutoOpen()
AddinContext.XlApp = (Excel.Application)ExcelDnaUtil.Application;
// Hook Mouse on First Load
MouseHook_Main.M_AppHook = Hook.AppEvents();
MouseHook_Main.Init_Unload(XLIG.Properties.Settings.Default.HScroll);
// Hook ws event to Ctrl+Tab switch back to old sheet
AddinContext.XlApp.SheetDeactivate += XlAppEvent_SheetDeactivate;
AddinContext.XlApp.WorkbookDeactivate += XlAppEvent_WorkbookDeactivate;

}
public void AutoClose()
{
// Dispose Mouse Hook
MouseHook_Main.M_AppHook.Dispose();
// It is recommended to unattacth this IntelliSense server
IntelliSenseServer.Uninstall();
// Kill Shadow Excel Instance
AddinContext.XlApp.Quit();
// Dispose Mouse Hook
MouseHook_Main.M_AppHook.Dispose();
AddinContext.XlApp.SheetDeactivate -= XlAppEvent_SheetDeactivate;
AddinContext.XlApp.WorkbookDeactivate -= XlAppEvent_WorkbookDeactivate;
}
private void XlAppEvent_WorkbookDeactivate(Excel.Workbook Wb)
{
XLCommand.PreWbookName = Wb.Name;
}
private void XlAppEvent_SheetDeactivate(object Sh)
{
XLCommand.PreWsheetName = (Sh as Excel.Worksheet).Name;
XLCommand.PreWbookName = AddinContext.XlApp.ActiveWorkbook.Name;
}
}

Expand Down
1 change: 0 additions & 1 deletion XLIG/Ribbon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public void SayHello(IRibbonControl control1)
// Toggle Horizontal Scroll Section
public bool Toggle_HScroll_GetPressed(IRibbonControl control1)
{
MouseHook_Main.Init_Unload(Settings.Default.HScroll);
return Settings.Default.HScroll;
}
public void Toggle_HScroll_Control(IRibbonControl control1, bool pressed)
Expand Down
29 changes: 29 additions & 0 deletions XLIG/XLCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public class XLCommand
public static Excel.Workbook wb = null;
public static Excel.Worksheet ws = null;
public static Excel.Application XLApp = AddinContext.XlApp;
public static string PreWbookName = null;
public static string PreWsheetName = null;
[ExcelCommand(Description = "Open Sheets List", ShortCut = "^q")]
public static void OpenSheetsList()
{
Expand Down Expand Up @@ -74,5 +76,32 @@ public static void DecreaseColWidth()
}
}
}
[ExcelCommand(Description = "Back Previous Sheet", ShortCut = "^{tab}")]
public static void Back_Prev_Sheet()
{
if (PreWsheetName == null && PreWbookName == null)
{
return;
}
try
{
if (PreWbookName == null)
{
(XLApp.Sheets[PreWsheetName] as Excel.Worksheet).Select();
}
else
{
if (XLApp.ActiveWorkbook.Name == PreWbookName)
{
(XLApp.Sheets[PreWsheetName] as Excel.Worksheet).Select();
}
else
{
XLApp.Workbooks[PreWbookName].Activate();
}
}
}
catch { /* Sheet Deleted or workbook closed could cause errors. Throw catch here for that. */ }
}
}
}

0 comments on commit e6ff236

Please sign in to comment.