按字符串选择范围

发布于 2024-09-04 18:08:09 字数 479 浏览 9 评论 0原文

我如何更改此功能,以便选择 Word 文档中字符“E”和“F”之间的字符范围(如果有); 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();

}

这个函数不会让我通过引用传递两个字符串类型的对象.......

谢谢

How I can change this feature so I select the range of characters in a word document between the characters "E" and "F", if I have; xasdasdEcdscasdcFvfvsdfv is underlined to me the range -> 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();

}

This function will not let me pass by reference two objects of string type.......

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魄砕の薆 2024-09-11 18:08:10

您需要传递文档中您想要范围覆盖的位置,请参阅:
如何:定义和选择文档中的范围

我添加了一些示例代码如下:

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();
    }
}

不要忘记关闭文档和Word等。

You need to pass the position in the document you want the range to cover, see:
How to: Define and Select Ranges in Documents

I have added some example code below:

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();
    }
}

Do not forget to close the document and Word etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文