重新定义段落范围以在段落中包含单词的子集 [英] Re-defne a Paragraph Range to Include a Subset of Words in the Paragraph

查看:107
本文介绍了重新定义段落范围以在段落中包含单词的子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Microsoft.Interop.Word/C#以编程方式创建Word文档.在给定的段落中,我需要更改括号"["和]"之间出现的单词的颜色

I'm using Microsoft.Interop.Word / C# to programmatically create a Word document. In a given paragraph, I need to change the color of words that occur between brackets "[" and "]"

单词是可变的.

据我了解,我需要一个Range对象来设置颜色.我正在使用以下代码,该代码不起作用:

As I understand it, I need a Range object in order to set colors. I'm using the following code, which does not work:

// Add the paragraph.
Microsoft.Office.Interop.Word.Paragraph pgf = document.Paragraphs.Add();

// Insert text into the paragraph.
pgf.Range.InsertBefore("JIRA Issue: " + jiraIssueId + " [" + statusName + "]");

//Get start and end locations of the brackets.
//These will define the location of the words to which I will apply colors.            
int startPos = pgf.Range.Text.IndexOf("[") + 1;
int endPos = pgf.Range.Text.IndexOf("]") - 1;

/* Attempt to change range start and end positions,
 so the range contains just the words between brackets.
 I've tried two methods.                  
*/
// The following line does not work.
pgf.Range.SetRange(startPos, endPos);

// And the following lines do not work either
pgf.Range.Start = startPos;
pgf.Range.End = endPos;

// Attempt to Change color. The following line changes the color of entire paragraph:
pgf.Range.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorRed;

/* The following always prints the entire contents of the paragraph, 
  and not just the words between the brackets. So my attempts at narrowing
  the range were not successful.
*/
Console.WriteLine(pgf.Range.Text);

推荐答案

SetRange不起作用,因为您无法更改段落的开头或结尾.它只是从它开始的地方开始.您需要一个可以修改的自有"范围.幸运的是,存在属性范围重复".正如MSDN所说:By duplicating a Range object, you can change the starting or ending character position of the duplicate range without changing the original range.

The SetRange doesn't work because you can't change beginning or ending of a paragraph. It just begins where it begins. You need an "own" range that you can modify. What a luck there exists the property Duplicate of a Range. As MSDN says: By duplicating a Range object, you can change the starting or ending character position of the duplicate range without changing the original range.

首先,我假定包含MS Word命名空间:

First I assume that the MS Word namspace is included:

using Microsoft.Office.Interop.Word;

因此,在计算startPos和endPos之后,应将代码更改为( UPDATED ):

So after calculating startPos and endPos you should change your code to (UPDATED):

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;

,它应该可以工作. (只是不要将endPos递减到上面的几行;但是您会意识到的)

and it should work. (Just don't decrement the endPos some lines above; but you will realize it)

这篇关于重新定义段落范围以在段落中包含单词的子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆