Skip to content

Commit 376707f

Browse files
committed
Add AddIns tracking for .xlams
1 parent a89a1d8 commit 376707f

File tree

1 file changed

+54
-14
lines changed

1 file changed

+54
-14
lines changed

Source/ExcelDna.IntelliSense/Providers/WorkbookIntelliSenseProvider.cs

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,9 @@ public void Initialize()
130130
var xlApp = (Application)ExcelDnaUtil.Application;
131131
xlApp.WorkbookOpen += Excel_WorkbookOpen;
132132
xlApp.WorkbookBeforeClose += Excel_WorkbookBeforeClose;
133-
//xlApp.WorkbookAddinInstall += Excel_WorkbookAddinInstall;
134-
//xlApp.WorkbookAddinUninstall += Excel_WorkbookAddinUninstall;
133+
xlApp.WorkbookAddinInstall += Excel_WorkbookAddinInstall;
134+
xlApp.WorkbookAddinUninstall += Excel_WorkbookAddinUninstall;
135135

136-
//var app = ExcelDnaUtil.Application;
137-
// app.WorkbookLoaded...
138136
lock (_workbookRegistrationInfos)
139137
{
140138
foreach (Workbook wb in xlApp.Workbooks)
@@ -150,6 +148,35 @@ public void Initialize()
150148
RegisterWithXmlProvider(wb);
151149
}
152150
}
151+
if (ExcelDnaUtil.ExcelVersion >= 14.0)
152+
{
153+
foreach (AddIn addIn in xlApp.AddIns2)
154+
{
155+
if (addIn.IsOpen && Path.GetExtension(addIn.FullName) != ".xll")
156+
{
157+
// Can it be "Open" and not be loaded?
158+
var name = addIn.Name;
159+
Workbook wbAddIn;
160+
try
161+
{
162+
// TODO: Log
163+
wbAddIn = xlApp.Workbooks[name];
164+
}
165+
catch
166+
{
167+
// TODO: Log
168+
continue;
169+
}
170+
171+
WorkbookRegistrationInfo regInfo = new WorkbookRegistrationInfo(name);
172+
_workbookRegistrationInfos[name] = regInfo;
173+
174+
regInfo.Refresh();
175+
176+
RegisterWithXmlProvider(wbAddIn);
177+
}
178+
}
179+
}
153180
}
154181
}
155182

@@ -203,6 +230,27 @@ void Excel_WorkbookBeforeClose(Workbook wb, ref bool cancel)
203230
}
204231
}
205232

233+
void Excel_WorkbookAddinInstall(Workbook wb)
234+
{
235+
var name = wb.Name;
236+
var regInfo = new WorkbookRegistrationInfo(name);
237+
lock (_workbookRegistrationInfos)
238+
{
239+
_workbookRegistrationInfos[name] = regInfo;
240+
RegisterWithXmlProvider(wb);
241+
OnInvalidate();
242+
}
243+
}
244+
245+
void Excel_WorkbookAddinUninstall(Workbook wb)
246+
{
247+
lock (_workbookRegistrationInfos)
248+
{
249+
_workbookRegistrationInfos.Remove(wb.Name);
250+
UnregisterWithXmlProvider(wb);
251+
}
252+
}
253+
206254
void RegisterWithXmlProvider(Workbook wb)
207255
{
208256
var path = wb.FullName;
@@ -225,16 +273,6 @@ void UnregisterWithXmlProvider(Workbook wb)
225273
_xmlProvider.UnregisterXmlFunctionInfo(xmlPath);
226274
}
227275

228-
//private void Excel_WorkbookAddinInstall(Workbook Wb)
229-
//{
230-
// throw new NotImplementedException();
231-
//}
232-
233-
//private void Excel_WorkbookAddinUninstall(Workbook Wb)
234-
//{
235-
// throw new NotImplementedException();
236-
//}
237-
238276
void OnInvalidate()
239277
{
240278
Invalidate?.Invoke(this, EventArgs.Empty);
@@ -251,6 +289,8 @@ public void Dispose()
251289
{
252290
xlApp.WorkbookOpen -= Excel_WorkbookOpen;
253291
xlApp.WorkbookBeforeClose -= Excel_WorkbookBeforeClose;
292+
xlApp.WorkbookAddinInstall -= Excel_WorkbookAddinInstall;
293+
xlApp.WorkbookAddinUninstall -= Excel_WorkbookAddinUninstall;
254294
}
255295
}
256296
catch (Exception ex)

0 commit comments

Comments
 (0)