c# - Re-defne a Paragraph Range to Include a Subset of Words in the Paragraph -


I am using Microsoft. Interp. To create a Word document for the Word / C # program. In a given paragraph, I need to change the color of the words between "[" and "]"

The words are variable.

As I understand, I need to set a color to set the color I am using the following code, which does not work:

  // paragraph Add. Microsoft.Office.Interop.Word.Paragraph pgf = document.Paragraphs.Add (); // Enter the text in paragraph. Pgf.Range.InsertBefore ("JIRA Issue:" + jiraIssueId + "[" + statusName + "]"); // Get the starting and ending locations of brackets. // These will define the location of words on which I will apply color. Int startPos = pgf.Range.Text.IndexOf ("[") + 1; Int endPos = pgf.Range.Text.IndexOf ("]") - 1; / * Limit is an attempt to change the position of start and end, so there are only words between the brackets in the range. I have tried two ways * / // The following line does not work. Pgf.Range.SetRange (startPos, and Peace); // and the following lines pgf.Range.Start = startPos either do not work; Pgf.Range.End = endPos; // Colors change color changes the entire paragraph in the following line: pgf.Range.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorRed; / * The following always prints the entire contents of the paragraph, and not only the words between the bracket So my efforts to reduce the range were not successful * / console.lightline (pgf.range.Text);    

The satellite is not working because you can not start or end the paragraph. It just starts, where it starts. You need the "self" category that you can modify whether a fate there exists a duplicate of the properties of the boundary. As MSDN says: By duplicating the range object, you can change the starting or ending character status of the duplicate range without changing the original category. First I think that MS Word Namspace is included:

  using Microsoft.Office.Interop.Word;   

Therefore, after calculating Start Grind and Endos, you should change your code ( Renew ):

  Microsoft.Office .Interop Word.Range rng = pgf.Range.Duplicate; Rng.MoveEnd (WdUnits.wdCharacter, endPos - rng.Characters.Count); Rng.MoveStart (WdUnits.wdCharacter, startPos); Rng.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorRed;   

And it should work (there is no shortage of end, some lines above, but you will realize it)

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -