3

我尝试将超链接插入到 ms word 文件中,并且超链接到另一个 word 文件中的书签。因为我已经知道包含书签的 word 文件的路径。所以我想将路径和书签名称(即:“path”+“#”+“bookmark_name”)组合为word文件中的超链接。因为在 ms word 中,超链接加上“#”符号并后跟书签名称将创建到书签的链接。

我的问题是,当我在代码中将“#”写为字符串时,运行我的代码。“#”符号不会正确写入word文件,它会变成一个条形符号“-”。我该如何应对?

这是代码:

这里的“#”符号将被更改,

string test_file_Path = created_folder + "\\test2.docx" + "#testsbookmark";

MessageBox.Show(linkAddr.ToString());它显示仍然正确。

public void AddContent(string filePath)
{
        try
        {

            Object oMissing = System.Reflection.Missing.Value;
            // Word Interface
            Microsoft.Office.Interop.Word._Application WordApp = new Word.Application();
            WordApp.Visible = true;
            object filename = filePath;
            Microsoft.Office.Interop.Word._Document WordDoc = WordApp.Documents.Open(ref filename, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

            //
            WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;

            //
            WordApp.Selection.ParagraphFormat.LineSpacing = 15f;
            //
            //WordApp.Selection.TypeParagraph();
            Microsoft.Office.Interop.Word.Paragraph para;
            para = WordDoc.Content.Paragraphs.Add(ref oMissing);
            //
            para.Range.Text = "This is paragraph 1";
            //para.Range.Font.Bold = 2;
            //para.Range.Font.Color = WdColor.wdColorRed;
            //para.Range.Font.Italic = 2;
            para.Range.InsertParagraphAfter();

            para.Range.Text = "This is paragraph 2";
            para.Range.InsertParagraphAfter();

            //insert Hyperlink
            Microsoft.Office.Interop.Word.Selection mySelection = WordApp.ActiveWindow.Selection;
            mySelection.Start = 9999;
            mySelection.End = 9999;
            Microsoft.Office.Interop.Word.Range myRange = mySelection.Range;

            Microsoft.Office.Interop.Word.Hyperlinks myLinks = WordDoc.Hyperlinks;
            string test_file_Path = created_folder + "\\test2.docx" + "#testsbookmark";
            object linkAddr = test_file_Path;
            MessageBox.Show(linkAddr.ToString());
            Microsoft.Office.Interop.Word.Hyperlink myLink = myLinks.Add(myRange, ref linkAddr,
                ref oMissing);
            WordApp.ActiveWindow.Selection.InsertAfter("\n");

            //
            WordDoc.Paragraphs.Last.Range.Text = "Created:" + DateTime.Now.ToString();
            WordDoc.Paragraphs.Last.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;

            //
            WordDoc.Save();
            WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
            WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
            //return true;
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
            Console.WriteLine(e.StackTrace);
            //return false;
        }
}
4

2 回答 2

2

我也在 MSDN 中问了同样的问题,并找到了答案,解决了我的问题。所以我在这里重新发布。Peter Jamieson 回答了这个问题。

http://social.msdn.microsoft.com/Forums/office/en-US/7df1f959-cda5-42ab-9ede-de058ceafb2d/insert-a-hyperlink-with-named-anchor-into-ms-word

“据我所知,在 Word 中执行此操作的方法是插入一个如下所示的超链接:

{ HYPERLINK "文档的 URL" \l "书签名称" }

你应该能够使用这样的东西来做到这一点:“

Microsoft.Office.Interop.Word.Hyperlinks myLinks = WordDoc.Hyperlinks;
string test_file_Path = created_folder + "\\test2.docx";
object linkAddr = test_file_Path;
string test_bookmark = "testsbookmark";
object linkSubAddr = test_bookmark;
// you may need more parameters here
Microsoft.Office.Interop.Word.Hyperlink myLink = myLinks.Add(myRange, ref linkAddr, ref linkSubAddr);
WordApp.ActiveWindow.Selection.InsertAfter("\n");
于 2013-06-22T18:45:21.853 回答
0

您可以尝试将哈希替换%23 为 url 编码#

像这样:

string test_file_Path = created_folder + "\test2.docx" + "%23testsbookmark";
于 2013-06-19T18:14:39.367 回答