Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如果不更改下一段落的样式,则无法使用Word interop更改超链接样式_C#_Ms Word_Office Interop - Fatal编程技术网

C# 如果不更改下一段落的样式,则无法使用Word interop更改超链接样式

C# 如果不更改下一段落的样式,则无法使用Word interop更改超链接样式,c#,ms-word,office-interop,C#,Ms Word,Office Interop,我有一份格式与 Section Heading 1 Paragraph 1 ... Paragraph N Sub Heading 1 Paragraph 1 ... Paragraph N 我想做的是将标题中的超链接添加到参考文档中。我可以添加超链接并将样式应用于链接,但样式将应用于节的段落1以及超链接 注意:WordApp是围绕Microsoft.Office.Interop.Word.Application的单例包装。Hyperli

我有一份格式与

Section Heading 1
    Paragraph 1
    ...
    Paragraph N
    Sub Heading 1
    Paragraph 1
    ...
    Paragraph N
我想做的是将标题中的超链接添加到参考文档中。我可以添加超链接并将样式应用于链接,但样式将应用于节的段落1以及超链接

注意:WordApp是围绕Microsoft.Office.Interop.Word.Application的单例包装。HyperlinkDestination类只保存书签名称和包含书签的文件的路径

private void LinkHeadings(string file)
{
     Document doc = WordApp.Open(file);

     for (int i = 1; i <= proposal.Paragraphs.Count; i++)
     {
        HyperlinkDestination dest = null;

        Paragraph paragraph = proposal.Paragraphs[i];
        paragraph.Range.Select();
        Style style = (Style)paragraph.get_Style();
        string styleString = ((Style)paragraph.get_Style()).NameLocal;
        string headingText = paragraph.Range.Text.Split(' ')[0];
        if (styleString.Contains("Heading"))
        {
           dest = _hyperlinkDestinations.Find(x => x.HyperlinkText == headingText);
        }

        if (dest != null)
        {
           Hyperlink link = WordApp.ActiveWindow.Document.Hyperlinks.Add(WordApp.Selection.Range, Address: dest.FilePath, SubAddress: dest.bookmarkName, TextToDisplay: WordApp.Selection.Text);
           link.Range.set_Style(style);
        }
     }

     WordApp.Close(true);
  }
private void链接标题(字符串文件)
{
文档doc=WordApp.Open(文件);
for(int i=1;i x.HyperlinkText==headingText);
}
如果(dest!=null)
{
Hyperlink=WordApp.ActiveWindow.Document.Hyperlinks.Add(WordApp.Selection.Range,地址:dest.FilePath,子地址:dest.bookmarkName,文本显示:WordApp.Selection.Text);
link.Range.set_样式(样式);
}
}
WordApp.Close(true);
}

我的猜测是,这与超链接锚有关。我也尝试过先删除标题,然后插入超链接,但结果也一样。

基本问题是,在Word插入的超链接字段中包含段落标记。当显示超链接字段结果时,即节标题1段落,该pargraph标记将被隐藏。实际上将成为第1款的一部分。将样式应用于选定内容时,整个段落将受到影响

我不打算在这里提供C#,但这里有一些建议 A.通常,在Word中使用范围对象比在可能的情况下使用选择对象更好,您应该能够在此处这样做。 B如果将超链接应用到没有段落标记的段落,则段落样式将保持不变,因此不需要重新应用 C因此,与以“paragration.Range.Select();”开头的代码不同,您应该能够使用类似的代码(我留给您的是正确的C#语法-也许您可以编辑此消息)


如果您的代码需要在国际范围内运行,并且只需要检查带有内置样式类型标题1..标题9的段落,那么最好比较style.Type,看看它是否属于这9种样式类型之一。如果需要包括其他名为“Heading something”的样式类型,则可能需要同时检查style.Type和名称。

some。用靶场工作就成功了。我想这很简单,但我就是想不出范围的部分。
Range r = Paragraph.Range();
string headingText = r.Text.Split(' ')[0];
if (styleString.Contains("Heading"))
// you shoul probably also tst for an empty paragraph here before inserting anything (I leave it to you)
{
    dest = _hyperlinkDestinations.Find(x => x.HyperlinkText == headingText);
}

if (dest != null)
{
    // Move the end of the range one character towards the beginning
    r.MoveEnd(Word.WdUnits.WdCharacter,-1)
    Hyperlink link = WordApp.ActiveWindow.Document.Hyperlinks.Add(r, Address: dest.FilePath, SubAddress: dest.bookmarkName, TextToDisplay: r.Text);
}