Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Sat Nov 25, 2017 3:47 pm

Dear support team,
Kindly provide VB.Net script for removing all hyperlinks from Word doc and saving changes to same file.

Thank you!

kdr13
 
Posts: 72
Joined: Fri Apr 15, 2016 4:35 pm

Mon Nov 27, 2017 3:47 am

Hello,

Thanks for your post. Please refer to the below code snippet to remove the hyperlinks. If you encounter any problem, please get it back to us.
Code: Select all
         Dim document As New Document()
         document.LoadFromFile("test.docx")
         Dim hyperlinks As New List(Of Field)()

         For Each section As Section In document.Sections

            For Each sec As DocumentObject In section.Body.ChildObjects
               If sec.DocumentObjectType = DocumentObjectType.Paragraph Then
                  Dim objs = (TryCast(sec, Paragraph)).ChildObjects
                  Dim i As Integer = 0
                  Do While i < objs.Count
                     If objs(i).DocumentObjectType = DocumentObjectType.Field Then
                        Dim field As Field = TryCast(objs(i), Field)

                        If field.Type = FieldType.FieldHyperlink Then
                           'remove hyperlink and keep text
                           Dim txt = field.FieldText
                           Dim tr As New TextRange(document)
                           tr.Text = txt
                           objs.RemoveAt(i)
                           objs.Insert(i,tr)
                        End If
                     End If
                     i += 1
                  Loop
               End If
            Next sec
         Next section
         document.SaveToFile("removelink.docx",FileFormat.Docx2010)


Best regards,
Simon
E-iceblue support team
User avatar

Simon.yang
 
Posts: 620
Joined: Wed Jan 11, 2017 2:03 am

Mon Nov 27, 2017 6:13 pm

Thanks a lot - I will check if it works properly and let you know

kdr13
 
Posts: 72
Joined: Fri Apr 15, 2016 4:35 pm

Mon Nov 27, 2017 6:55 pm

This code does not work - Please re-check / fix and also provide a code that supports both DOCX and DOC.

Thank you!

kdr13
 
Posts: 72
Joined: Fri Apr 15, 2016 4:35 pm

Tue Nov 28, 2017 9:19 am

Hello,

Thanks for your feedback. Please refer to the attached demo and try again. If it still can't full fill your needs, please share your input document and expected result.

Best regards,
Simon
E-iceblue support team
User avatar

Simon.yang
 
Posts: 620
Joined: Wed Jan 11, 2017 2:03 am

Tue Nov 28, 2017 3:19 pm

I tweaked the piece of code provided and it works fine now.

Thank you!

kdr13
 
Posts: 72
Joined: Fri Apr 15, 2016 4:35 pm

Wed Nov 29, 2017 1:28 am

Hello,

Thanks for your feedback. If there is any other question, please feel free to contact us.

Best regards,
Simon
E-iceblue support team
User avatar

Simon.yang
 
Posts: 620
Joined: Wed Jan 11, 2017 2:03 am

Return to Spire.Doc