15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

OC74 ❘ ChaPTer 49 visuAl studiO tOOls fOr Office<br />

foreach (string documentName in documentEditTimes.Keys)<br />

{<br />

if (doc.Name == documentName)<br />

{<br />

isMonitored = true;<br />

break;<br />

}<br />

If the names don’t match, then the document references are compared, which enables you to detect name<br />

changes to documents, as shown in the following code:<br />

}<br />

else<br />

{<br />

if (documentEditTimes[documentName].Document == doc)<br />

{<br />

// Monitored, but name changed!<br />

oldName = documentName;<br />

oldNameTimer = documentEditTimes[documentName];<br />

isMonitored = true;<br />

requiresNameChange = true;<br />

break;<br />

}<br />

}<br />

For unmonitored documents, a new monitor is created:<br />

// Add monitor if not monitored<br />

if (!isMonitored)<br />

{<br />

Globals.ThisAddIn.MonitorDocument(doc);<br />

}<br />

Whereas documents with name changes are re-associated with the monitor used for the old named<br />

document:<br />

}<br />

// Rename if necessary<br />

if (requiresNameChange)<br />

{<br />

documentEditTimes.Remove(oldName);<br />

documentEditTimes.Add(doc.Name, oldNameTimer);<br />

}<br />

After reconciling the document edit timers, a list is generated. This code also detects whether referenced<br />

documents are still loaded, <strong>and</strong> pauses the timer for documents that aren’t by setting the IsActive property<br />

to false. Again, this is defensive programming.<br />

// Create new list<br />

foreach (string documentName in documentEditTimes.Keys)<br />

{<br />

// Check to see if doc is still loaded<br />

bool isLoaded = false;<br />

foreach (Word.Document doc in<br />

Globals.ThisAddIn.Application.Documents)<br />

{<br />

if (doc.Name == documentName)<br />

{<br />

isLoaded = true;<br />

break;<br />

}<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!