Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. replace a Text in winword within a range

replace a Text in winword within a range

Scheduled Pinned Locked Moved C#
question
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Cyrus IRA
    wrote on last edited by
    #1

    ... [START] blaaa blaa blaa T h i s (this is an empty line) {END] ... another line -------------------------------------- I have been trying to remove empty lines appearing between [START] and {END] within a word document. Code seems to work if I did not have a textbox at top of my document. It seems that the textbox is also cleared once such operation takes place.

                wordapp = new Word.Application();
                wordapp.Visible = false;
                doc = wordapp.Documents.Open(wordPath);
                paragraphs = doc.Paragraphs;
                bool flag = false;
    
                foreach (Word.Paragraph paragraph in paragraphs)
                {
                    string s = paragraph.Range.Text;
                    string y = paragraph.Range.Text.Trim();
    
                    if (paragraph.Range.Text == "\\r")
                    {
                        flag = true;
                        paragraph.Range.Select();
                        wordapp.Selection.Delete();
                        continue;
    
                    }
                    else if (paragraph.Range.Text == "\\r")
                    {
                        flag = false;
                        paragraph.Range.Select();
                        wordapp.Selection.Delete();
                        continue;
                    }
                    else if (paragraph.Range.Text.Trim() == string.Empty )
                    {
                        if (flag)
                        {
                            paragraph.Range.Select();
                            wordapp.Selection.Delete();
                        }
                        continue;
                    }
    
                }
    
                // Save the document and close document 
                doc.Save();
                // Quit the word application 
                wordapp.Quit();
    

    is there any other way to achieve what I do using Range in Word or dealing with a textbox in Word?

    Student of life

    B 1 Reply Last reply
    0
    • C Cyrus IRA

      ... [START] blaaa blaa blaa T h i s (this is an empty line) {END] ... another line -------------------------------------- I have been trying to remove empty lines appearing between [START] and {END] within a word document. Code seems to work if I did not have a textbox at top of my document. It seems that the textbox is also cleared once such operation takes place.

                  wordapp = new Word.Application();
                  wordapp.Visible = false;
                  doc = wordapp.Documents.Open(wordPath);
                  paragraphs = doc.Paragraphs;
                  bool flag = false;
      
                  foreach (Word.Paragraph paragraph in paragraphs)
                  {
                      string s = paragraph.Range.Text;
                      string y = paragraph.Range.Text.Trim();
      
                      if (paragraph.Range.Text == "\\r")
                      {
                          flag = true;
                          paragraph.Range.Select();
                          wordapp.Selection.Delete();
                          continue;
      
                      }
                      else if (paragraph.Range.Text == "\\r")
                      {
                          flag = false;
                          paragraph.Range.Select();
                          wordapp.Selection.Delete();
                          continue;
                      }
                      else if (paragraph.Range.Text.Trim() == string.Empty )
                      {
                          if (flag)
                          {
                              paragraph.Range.Select();
                              wordapp.Selection.Delete();
                          }
                          continue;
                      }
      
                  }
      
                  // Save the document and close document 
                  doc.Save();
                  // Quit the word application 
                  wordapp.Quit();
      

      is there any other way to achieve what I do using Range in Word or dealing with a textbox in Word?

      Student of life

      B Offline
      B Offline
      BillWoodruff
      wrote on last edited by
      #2

      Hi Saeed, Could you clarify exactly what you mean by "textbox at top of my document" ? I note the variable 'y you define in your code is never used. It appears to me your code deletes the tags "<NE>" and "</NE>," as well as white-space between them: is that correct ? yours, Bill

      string test = "\r";

      string afterTest = test.Trim();

      // true
      MessageBox.Show(string.IsNullOrEmpty(afterTest).ToString());

      // true
      MessageBox.Show(string.IsNullOrWhiteSpace(afterTest).ToString());

      // true
      MessageBox.Show((afterTest == string.Empty).ToString());

      “Humans are amphibians: half spirit, half animal; as spirits they belong to the eternal world; as animals they inhabit time. While their spirit can be directed to an eternal object, their bodies, passions, and imagination are in continual change, for to be in time, means to change. Their nearest approach to constancy is undulation: repeated return to a level from which they repeatedly fall back, a series of troughs and peaks.” C.S. Lewis


      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups