小弟学生一个,刚接触c#,遇到了一个问题,向各位大侠请教。
我写的一个小程序是向一个word文档中插入一个超链接,超链接里面,包含了一个书签(#号隔开)。点击这个超链接可以跳转到链接所指向的另一个word文档当中的指定书签,也就是"#"后跟的书签名字。
因为我已经知道目标文档的路径,和指定书签的名字,所以我想申明一个字符串 "目标文档路径"+"#"+"书签名",这样就能跳转到目的地了。但是我把上面这个字符串写进程序,并运行后,这个字符串里的"#"号会被变成"-"号(从井号变为横杠)...我不知道该怎么处理这个问题,请各位大侠帮忙...折腾好些天了...谢谢各位大侠...
 
下面是代码:
string test_file_Path = created_folder + "\test2.docx" + "#testsbook";
问题就是上面这个字符串里的"#"变"-"。
但是在  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" + "#testsbook";
            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;
        }
}Wordc#书签超链接String

解决方案 »

  1.   

    插入超链接 地址直接给 1.docx#test 就行了你可以手动试一下没问题的 也不会改变符号#
      

  2.   

    来结贴,答案我参照的是以下代码。谢谢楼上回答的大侠。Microsoft.Office.Interop.Word.Hyperlinks myLinks = WordDoc.Hyperlinks;
    string test_file_Path = created_folder + "\\test2.docx";
    object linkAddr = test_file_Path;
    string test_book = "testsbook";
    object linkSubAddr = test_book;
    // you may need more parameters here
    Microsoft.Office.Interop.Word.Hyperlink myLink = myLinks.Add(myRange, ref linkAddr, ref linkSubAddr);
    WordApp.ActiveWindow.Selection.InsertAfter("\n");