我如何更改此功能,以便我选择字符"E"和"F"之间的word文档中的字符范围,如果我有; xasdasdEcdscasdcFvfvsdfv向我强调了范围 - > cdscasdc
private void Rango()
{
Word.Range rng;
Word.Document document = this.Application.ActiveDocument;
object startLocation = "E";
object endLocation = "F";
// Supply a Start and End value for the Range.
rng = document.Range(ref startLocation, ref endLocation);
// Select the Range.
rng.Select();
}
Run Code Online (Sandbox Code Playgroud)
这个函数不会让我通过引用传递两个字符串类型的对象.......
谢谢
您需要传递要包含范围的文档中的位置,请参阅: 如何:在文档中定义和选择范围
我在下面添加了一些示例代码:
var word = new Microsoft.Office.Interop.Word.Application();
string document = null;
using (OpenFileDialog dia = new OpenFileDialog())
{
dia.Filter = "MS Word (*.docx)|*.docx";
if (dia.ShowDialog() == DialogResult.OK)
{
document = dia.FileName;
}
}
if (document != null)
{
Document doc = word.Documents.Open(document, ReadOnly: false, Visible: true);
doc.Activate();
string text = doc.Content.Text;
int start = text.IndexOf('E') + 1;
int end = text.IndexOf('F');
if (start >= 0 && end >= 0 && end > start)
{
Range range = doc.Range(Start: start, End: end);
range.Select();
}
}
Run Code Online (Sandbox Code Playgroud)
不要忘记关闭文档和Word等.
| 归档时间: |
|
| 查看次数: |
6666 次 |
| 最近记录: |