protected void ProcessButton(FormButtons eFormButton)
 {
     try
     {
         if (backgroundWorker.IsBusy)
         {
             throw new ApplicationException("Click the 'Stop' button to cancel the current search");
         }
         ProcessButtonEx(eFormButton);
     }
     catch (Exception ex)
     {
         OfficeApp.DisplayException(ex);
     }
 }
예제 #2
0
        protected void DoWork(BackgroundWorker worker, OfficeDocument doc)
        {
            int nWordCount = doc.WordCount;

            /*
             * if (doc.GetType() == typeof(PubDocument))   // for publisher, we step when we've found a font
             * {
             *      progressBarFontsInUse.Maximum = nWordCount;
             *      progressBarFontsInUse.Step = 1;
             *      nWordCount = 0;
             * }
             */
            FontNamesInUseProcessor aDocumentProcess = new FontNamesInUseProcessor(nWordCount, worker);

            try
            {
                doc.ProcessWordByWord(aDocumentProcess);
            }
            catch (Exception ex)
            {
                OfficeApp.DisplayException(ex);
            }
        }
        private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            bool bReplace  = (m_aWordByWordProcessor.FormButton == FormButtons.ReplaceOnce) || (m_aWordByWordProcessor.FormButton == FormButtons.ReplaceAll);
            bool bContinue = false;  // start again pessimistic

            if (e.Error != null)
            {
                OfficeApp.DisplayException(e.Error);
            }

            // if nothing was found in the search area, then see if they want to start over again
            else if (!e.Cancelled && !m_aWordByWordProcessor.IsFound)
            {
                bool   bSelection = (m_eSearchAreaType == SearchAreaType.eSelection);
                string strPrompt  = null;
                if (bSelection)
                {
                    if (bReplace)
                    {
                        strPrompt = "Finished searching the selection. {0} replacement{1} made. Would you like to search the rest of the document?";
                    }
                    else
                    {
                        strPrompt = "Finished searching the selection. Would you like to search the rest of the document?";
                    }
                }
                else if (m_doc.theRangeToSearch.Start == 0)  // otherwise, whole document
                {
                    if (bReplace)
                    {
                        strPrompt = String.Format("Finished searching the whole document. {0} replacement{1} made in all.",
                                                  m_aWordByWordProcessor.NumOfReplacements,
                                                  (m_aWordByWordProcessor.NumOfReplacements == 1) ? " was" : "s were");
                    }
                    else
                    {
                        strPrompt = "Finished searching the whole document. The search item was not found.";
                    }

                    MessageBox.Show(strPrompt, OfficeApp.cstrCaption, MessageBoxButtons.OK);

                    // reset the state so we start over if the user clicks it again.
                    m_doc.ResetPosition();
                    goto ExitRoutine;
                }
                else    // otherwise, rest of the document
                {
                    if (bReplace)
                    {
                        strPrompt = "Finished searching the rest of the document. {0} replacement{1} made. Would you like to continue searching at the beginning of the document?";
                    }
                    else
                    {
                        strPrompt = "Finished searching the rest of the document. Would you like to continue searching at the beginning of the document?";
                    }
                }

                if (bReplace)
                {
                    strPrompt = String.Format(strPrompt, m_aWordByWordProcessor.NumOfReplacements,
                                              (m_aWordByWordProcessor.NumOfReplacements == 1) ? " was" : "s were");
                }

                DialogResult res = MessageBox.Show(strPrompt, OfficeApp.cstrCaption, MessageBoxButtons.YesNoCancel);

                bool bReset = true; // most paths want us to reset.
                if (res == DialogResult.Cancel)
                {
                    this.Close();
                }
                else if (res == DialogResult.Yes)
                {
                    if (bSelection)
                    {
                        // start after where we just finished up (i.e. the end of the selection)
                        m_doc.SearchAreaStart = m_doc.theRangeToSearch.End + 1;
                        m_doc.ChangeToRestOfDocument(ref m_doc.theRangeToSearch, out m_eSearchAreaType);
                        bReset = false;
                    }
                    else    // doing ReplaceAll from beginning
                    {
                        // make the end of the search go up to the beginning of the original search
                        //  (if a) selection, b) rest, and c) beginning, then rangeToSearch.Start will
                        //  be the beginnin go of the selection (which is what we want).
                        m_doc.theRangeToSearch.End   = m_doc.theRangeToSearch.Start;
                        m_doc.theRangeToSearch.Start = 0;
                    }

                    bContinue = true;
                }

                if (bReset)
                {
                    m_doc.ResetPosition();
                }
            }

            // do it again!
            if (bContinue)
            {
                CallWorkerBee();
                return;
            }

ExitRoutine:
            this.buttonCancel.Text = cstrClose;
            progressBar.Visible    = false;
            m_doc.Document.Application.System.Cursor = Microsoft.Office.Interop.Word.WdCursorType.wdCursorNormal;
        }