private void buttonMark_Click(object sender, EventArgs e) { int HitCount = 0; try { //search foreach (Word.Range StoryRange in Globals.ThisAddIn.Application.Selection.Document.StoryRanges) { HitCount += FindAndMarkInStory(StoryRange); } //show results if (HitCount > 1) { labelResults.Text = HitCount + Resources.OccurrencesFound; } else if (HitCount == 1) { labelResults.Text = HitCount + Resources.OccurrenceFound; } else //we didn't find anything, tell them. { labelResults.Text = string.Empty; GenericMessageBox.Show(Resources.TextNotFound, Resources.AppName, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, (MessageBoxOptions)0); } } catch (COMException cex) { //something failed, show them the error message GenericMessageBox.Show(cex.Message, Resources.AppName, MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, (MessageBoxOptions)0); } }
/// <summary> /// Finds the next redaction mark in the document. /// </summary> /// <param name="CurrentRange">The range containing the starting point of the search. Changes to the location of the output mark if one is found.</param> /// <param name="CurrentView">The current view type.</param> /// <param name="WrapAtEnd">True to prompt to wrap around at the end, otherwise False.</param> /// <returns>True if a mark was found, otherwise False.</returns> private bool FindNextMark(ref Word.Range CurrentRange, Word.WdViewType CurrentView, bool WrapAtEnd) { if (FindNextMarkInCurrentStory(ref CurrentRange) || FindNextMarkInOtherStory(ref CurrentRange)) { //extend it out and select it ExtendNextMark(ref CurrentRange, false); CurrentRange.Select(); return(true); } else { if (WrapAtEnd) { if (GenericMessageBox.Show(Resources.SearchFromBeginning, Resources.AppName, MessageBoxButtons.OKCancel, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, (MessageBoxOptions)0) == DialogResult.OK) { //move the range to the start of the document and search again CurrentRange = CurrentRange.Document.StoryRanges[Word.WdStoryType.wdMainTextStory]; CurrentRange.Collapse(ref CollapseStart); return(FindNextMark(ref CurrentRange, CurrentView, false)); } else { return(false); } } else { GenericMessageBox.Show(Resources.NoRedactionMarks, Resources.AppName, MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, (MessageBoxOptions)0); return(false); } } }
/// <summary> /// Finds the previous redaction mark in the document. /// </summary> /// <param name="CurrentRange">The range containing the starting point of the search. Changes to the location of the output mark if one is found.</param> /// <param name="CurrentView">The current view type.</param> /// <param name="WrapAtStart">True to prompt to wrap around at the start, otherwise False.</param> /// <returns>True if a mark was found, otherwise False.</returns> private bool FindPreviousMark(ref Word.Range CurrentRange, Word.WdViewType CurrentView, bool WrapAtStart) { if (FindPreviousMarkInCurrentStory(ref CurrentRange) || FindPreviousMarkInOtherStory(ref CurrentRange)) { //extend it out and select it ExtendPreviousMark(ref CurrentRange, false); CurrentRange.Select(); return(true); } else { if (WrapAtStart) { if (GenericMessageBox.Show(Resources.SearchFromEnd, Resources.AppName, MessageBoxButtons.OKCancel, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, (MessageBoxOptions)0) == DialogResult.OK) { //they want to wrap around, so find the last story and start again CurrentRange = GetLastStory(ref CurrentRange); CurrentRange.Collapse(ref CollapseEnd); return(FindPreviousMark(ref CurrentRange, CurrentView, false)); } else { return(false); } } else { GenericMessageBox.Show(Resources.NoRedactionMarks, Resources.AppName, MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, (MessageBoxOptions)0); return(false); } } }
/// <summary> /// Redact the current document. /// </summary> private void RedactDocument() { object Story = Word.WdUnits.wdStory; Word.WdViewType OriginalView = Application.ActiveWindow.View.Type; Application.ScreenUpdating = false; Word.Document SourceFile = Application.ActiveDocument; //cache the document's saved and update styles from template states bool Saved = SourceFile.Saved; bool UpdateStyles = SourceFile.UpdateStylesOnOpen; //BUG 5819: need to make sure this is off so that Normal's properties don't get demoted into redacted file Application.ActiveDocument.UpdateStylesOnOpen = false; Word.Document FileToRedact = RedactCommon.CloneDocument(Application.ActiveDocument); //reset those states to their cached values SourceFile.UpdateStylesOnOpen = UpdateStyles; SourceFile.Saved = Saved; //get the window for the document surface (_WwG) NativeWindow WordWindow = RedactCommon.GetDocumentSurfaceWindow(); //show progress UI using (FormDoRedaction ProgressUI = new FormDoRedaction(FileToRedact, this)) { ProgressUI.ResetProgress(); DialogResult RedactResult = ProgressUI.ShowDialog(); //fix the view Application.ActiveWindow.View.SeekView = Word.WdSeekView.wdSeekMainDocument; Application.ActiveWindow.View.Type = OriginalView; Application.Selection.HomeKey(ref Story, ref Missing); if (RedactResult != DialogResult.OK) { GenericMessageBox.Show(Resources.RedactionFailed, Resources.AppName, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, (MessageBoxOptions)0); } else { //dialog telling the user we're done using (FormSuccess Success = new FormSuccess(FileToRedact.Application)) Success.ShowDialog(WordWindow); } } //set focus back onto the document NativeMethods.SetFocus(WordWindow.Handle); //we need to release our handle, or Word will crash on close as the CLR tries to do it for us WordWindow.ReleaseHandle(); }