VB.NET ActiveDocument.Hyperlink Command failed when adding hyperlinks

Viewed 50

When i try to add an hyperlink to the activedocument I get the exception "Command failed" every single time. I added word.ActiveDocument.Hyperlinks.Add(Anchor:=word.Selection, Address:="www.google.com", TextToDisplay:="TEST") to add an hyperlink to the existing document with an alias. But it doesn't seem to work and the exception, inner exception and stacktrace ar not of any help at all.

 Dim word As Microsoft.Office.Interop.Word.Application = CreateObject("Word.Application")
                word.Documents.Add(documentProperty.Filename)

                If Not IO.Directory.Exists(documentProperty.SaveAsFilename.Substring(0, documentProperty.SaveAsFilename.LastIndexOf("\"))) Then
                    IO.Directory.CreateDirectory(documentProperty.SaveAsFilename.Substring(0, documentProperty.SaveAsFilename.LastIndexOf("\")))
                End If

                word.Visible = False
                word.PrintPreview = False
                word.ScreenUpdating = False
                word.ActiveDocument.Hyperlinks.Add(Anchor:=word.Selection, Address:="www.google.com", TextToDisplay:="TEST")
                word.ActiveDocument.ExportAsFixedFormat(OutputFileName:=documentProperty.SaveAsFilename,
                                                        ExportFormat:=Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF,
                                                        OpenAfterExport:=False,
                                                        OptimizeFor:=Microsoft.Office.Interop.Word.WdExportOptimizeFor.wdExportOptimizeForPrint,
                                                        Range:=Microsoft.Office.Interop.Word.WdExportRange.wdExportAllDocument,
                                                        From:=1,
                                                        To:=1,
                                                        Item:=Microsoft.Office.Interop.Word.WdExportItem.wdExportDocumentContent,
                                                        IncludeDocProps:=True,
                                                        KeepIRM:=True,
                                                        CreateBookmarks:=Microsoft.Office.Interop.Word.WdExportCreateBookmarks.wdExportCreateNoBookmarks,
                                                        DocStructureTags:=True,
                                                        BitmapMissingFonts:=True,
                                                        UseISO19005_1:=False)
1 Answers

The error is due to the anchor parameter. You want to be using word.Selection.Range instead of word.Selection.

Related