Answered by:
Word automation insert hyperlink into paragraph

Question
-
I want to append a hyperlink to the end of a paragraph using VS2008 as shown in the code below between the segment marked begin here and end here but get an exception Command Failed. Any ideas on how to make this work.
The paragraph should appear as "More text http://www.google.com" (this is sample text) and if possible overlay the hyperlink with text. Thanks for any assistance.
Public Sub CreateRequest() Dim RowCount As Integer = frmMainForm.ProcessRows.Count Dim oWord As Microsoft.Office.Interop.Word.Application = _ CType(CreateObject("Word.Application"), _ Microsoft.Office.Interop.Word.Application) ' Create new word document Dim oDoc As Microsoft.Office.Interop.Word.Document = oWord.Documents.Add() Dim InitialParagraph As Microsoft.Office.Interop.Word.Paragraph Try oWord.Visible = True ' ' BEGIN HERE ' InitialParagraph = oDoc.Content.Paragraphs.Add InitialParagraph.Range.Text = "More text...." InitialParagraph.Range.Font.Bold = CInt(False) InitialParagraph.Format.SpaceAfter = 24 InitialParagraph.Range.InsertParagraphAfter() Try Dim h As Microsoft.Office.Interop.Word.Hyperlink = _ oDoc.Hyperlinks.Add(InitialParagraph, _ "http://www.google.com") Catch ex As Exception Console.WriteLine(ex.Message) End Try ' ' END HERE ' Dim oTable As Microsoft.Office.Interop.Word.Table = _ oDoc.Tables.Add(oDoc.Bookmarks.Item("\endofdoc").Range, RowCount + 1, 2) oTable.Range.ParagraphFormat.SpaceAfter = 6 oTable.Cell(1, 1).Range.Text = "User ID" oTable.Cell(1, 2).Range.Text = "Role" For Row As Integer = 0 To RowCount - 1 oTable.Cell(Row + 2, 1).Range.Text = frmMainForm.ProcessRows(Row).Cells("Identifier").Value.ToString If frmMainForm.ProcessRows(Row).Cells("Admin").Value.ToString.Equals("True") Then oTable.Cell(Row + 2, 2).Range.Text = "ADMIN" Else oTable.Cell(Row + 2, 2).Range.Text = "USER" End If Next oTable.Rows.Item(1).Range.Font.Bold = CInt(True) oDoc.SaveAs(frmMainForm.DocumentName, True) Catch ex As Exception MsgBox(String.Format("Failed to create '{0}'{1}Error message below{1}{2}", _ frmMainForm.DocumentName, Environment.NewLine, ex.Message)) Exit Sub Finally oDoc.Close() oWord.Application.Quit() Application.DoEvents() Process.Start(frmMainForm.DocumentName) End Try End Sub
KSGWednesday, May 5, 2010 1:51 PM
Answers
-
Here is an example of what works for adding a hyperlink. In this case to the end of a sentence.
Private Sub cmdTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdTest.Click Dim Word As Microsoft.Office.Interop.Word.Application = CType(CreateObject("Word.Application"), _ Microsoft.Office.Interop.Word.Application) Dim Doc As Microsoft.Office.Interop.Word.Document = Word.Documents.Add() Dim Para As Microsoft.Office.Interop.Word.Paragraph Word.Visible = True Dim Text As String = "Please create an RCA as follows for each user and use the information from ." Para = Doc.Content.Paragraphs.Add Para.Range.Text = Text Para.Range.Font.Bold = CInt(False) Dim range As Microsoft.Office.Interop.Word.Range = Para.Range range.SetRange(Text.Length - 1, Text.Length - 1) Dim objLink = Doc.Hyperlinks.Add(range, "http://www.google", , , "MEDS Wiki on Rocket") Para.Format.SpaceAfter = 24 Para.Range.InsertParagraphAfter() Dim MoreText As String = _ "Lorem ipsum dolor sit amet, consectetuer adipiscing elit, " & _ "sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat." Para = Doc.Content.Paragraphs.Add Para.Range.Text = MoreText range.SetRange(MoreText.Length - 1, MoreText.Length - 1) Para.Range.InsertParagraphAfter() Doc.SaveAs(Application.StartupPath & "\Test.doc", True) If Doc IsNot Nothing Then Doc.Close() End If If Word IsNot Nothing Then Word.Application.Quit() If Not IsNothing(Word) Then System.Runtime.InteropServices.Marshal.ReleaseComObject(Word) End If System.Runtime.InteropServices.Marshal.ReleaseComObject(Word) End If Application.DoEvents() End Sub
KSG- Marked as answer by KareninstructorMVP Wednesday, May 19, 2010 8:27 PM
Wednesday, May 19, 2010 8:25 PM
All replies
-
Here are some reference for you to check.
Inserting hyperlink programmatically into Word document using C#.NET
http://social.msdn.microsoft.com/Forums/en-SG/vsto/thread/05e096af-f9a4-4379-a3d5-e5db585d6402
KB: How to automate Word from Visual Basic .NET to create a new document
http://support.microsoft.com/kb/316383/
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.Tuesday, May 11, 2010 10:22 AMModerator -
Here are some reference for you to check.
Inserting hyperlink programmatically into Word document using C#.NET
http://social.msdn.microsoft.com/Forums/en-SG/vsto/thread/05e096af-f9a4-4379-a3d5-e5db585d6402
KB: How to automate Word from Visual Basic .NET to create a new document
http://support.microsoft.com/kb/316383/
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Thanks, the kb article is where I started prior to posting here which assisted me with other things but not the hyperlink. Any ways since posting here I did find a method to do the hyperlink and will share the code here on Monday as I am working from home and do not have the code on my teleworking computer. On a side note I have learned how valuable Aspose for Excel is and wish I had their Word library.
KSGFriday, May 14, 2010 3:51 PM -
Hi Kevin,
I'm glad to hear that you find the solution by yourself. Cheers!
If you can share your solution here and mark it as Answer, it will be very beneficial for other community members having the similar questions.
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.Monday, May 17, 2010 2:30 AMModerator -
Here is an example of what works for adding a hyperlink. In this case to the end of a sentence.
Private Sub cmdTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdTest.Click Dim Word As Microsoft.Office.Interop.Word.Application = CType(CreateObject("Word.Application"), _ Microsoft.Office.Interop.Word.Application) Dim Doc As Microsoft.Office.Interop.Word.Document = Word.Documents.Add() Dim Para As Microsoft.Office.Interop.Word.Paragraph Word.Visible = True Dim Text As String = "Please create an RCA as follows for each user and use the information from ." Para = Doc.Content.Paragraphs.Add Para.Range.Text = Text Para.Range.Font.Bold = CInt(False) Dim range As Microsoft.Office.Interop.Word.Range = Para.Range range.SetRange(Text.Length - 1, Text.Length - 1) Dim objLink = Doc.Hyperlinks.Add(range, "http://www.google", , , "MEDS Wiki on Rocket") Para.Format.SpaceAfter = 24 Para.Range.InsertParagraphAfter() Dim MoreText As String = _ "Lorem ipsum dolor sit amet, consectetuer adipiscing elit, " & _ "sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat." Para = Doc.Content.Paragraphs.Add Para.Range.Text = MoreText range.SetRange(MoreText.Length - 1, MoreText.Length - 1) Para.Range.InsertParagraphAfter() Doc.SaveAs(Application.StartupPath & "\Test.doc", True) If Doc IsNot Nothing Then Doc.Close() End If If Word IsNot Nothing Then Word.Application.Quit() If Not IsNothing(Word) Then System.Runtime.InteropServices.Marshal.ReleaseComObject(Word) End If System.Runtime.InteropServices.Marshal.ReleaseComObject(Word) End If Application.DoEvents() End Sub
KSG- Marked as answer by KareninstructorMVP Wednesday, May 19, 2010 8:27 PM
Wednesday, May 19, 2010 8:25 PM