PDA

View Full Version : Finding all web links in a Word document



asad111
01-26-2018, 03:47 AM
I have a Word document with 4123 pages and 6.08MB size. It has a lot of weblinks.

I want to find them and copy in a text-box on VBA form.

I tried following codes that worked with a Document containing only 6 weblinks. But it is very slow and even could not complete 100 counts of For-Next loop with above mentioned big Document.

Please suggest some faster VBA codes that could work with this big file.


Dim a, b as Long

a = 0


For n = 0 To 20000


a = InStr(a + 1, ActiveDocument.Content, "http")
If a = 0 Then Exit For
b = InStr(a + 1, ActiveDocument.Content, vbCr)
If b = 0 Then Exit For


Set Rng = ActiveDocument.Range(start:=a - 1, End:=b)
Text1.Text = Text1.Text + Rng.Text
'Text1.Text = Text1.Text + vbNewLine
a = b
Next


a = 0


For n = 0 To 20000


a = InStr(a + 1, ActiveDocument.Content, "www")
If a = 0 Then Exit For
b = InStr(a + 1, ActiveDocument.Content, vbCr)
If b = 0 Then Exit For


Set Rng = ActiveDocument.Range(start:=a - 1, End:=b)
a = InStr(1, Text1.Text, Rng.Text)
If a = 0 Then Text1.Text = Text1.Text + Rng.Text
a = b
Next


Thanks :)