C
ca1358
I have Word 2003
I am trying to take a block of Emails in word and put them into new Word
Document Format, so then I can Export to Excel and have each Email address in
a cell. I found this code, that I thought would take the block emails put
into new Word doc and Format. I have never programed in Word, and I am just
learning in Excel and Access.
It stops at this line- runtime error 9 subscript out range.
For i = 1 To UBound(HyperlinkArray()) ' Looping through our array
Any help would greatly be appreciated.
'//////////////////////////////////
Option Explicit
Option Base 1
Dim HyperlinkArray() ' Dimension array to contain hyperlinks
Public Sub Main()
Dim btn
Dim pos
Dim pos2
Dim mytemp
Dim i
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
On Error GoTo BYE
Dim Count As Integer
Count = 0
btn = MsgBox("This macro will copy the hyperlinks from the current." &
vbCrLf & _
" document into a new document." & vbCrLf & vbCrLf & _
" Do you WISH TO PROCEED?", vbYesNo + vbQuestion, _
" Startup Message")
If btn = vbNo Then Exit Sub
Selection.HomeKey Unit:=wdStory ' Go to the top of the document
OUTERLOOP:
With Selection.Find
..Text = "<a href" ' Find the start of the hyperlink
..Replacement.Text = ""
..MatchWildcards = False
..Format = False
..Wrap = wdFindStop
..Forward = True
End With
Selection.Find.Execute
If Selection.Find.Found Then
Selection.ExtendMode = True ' Set extension mode to true
With Selection.Find
..Text = ">" ' Finding the end of the hyperlink.
..Replacement.Text = ""
..MatchWildcards = False
..Format = False
..Wrap = wdFindStop
..Forward = True
End With
Selection.Find.Execute
Selection.ExtendMode = False ' Turn the extension mode off
pos = InStr(Selection.Text, Chr(34))
pos2 = InStr(pos + 1, Selection.Text, Chr(34))
mytemp = Mid(Selection.Text, pos + 1, pos2 - (pos + 1))
Count = Count + 1
ReDim Preserve HyperlinkArray(Count) ' Dynamically resizing the array
HyperlinkArray(Count) = mytemp
Selection.Start = Selection.End
GoTo OUTERLOOP
Else
GoTo ARRAY_FEED
End If
ARRAY_FEED:
Documents.Add Template:="", NewTemplate:=False ' Adding a new document
For i = 1 To UBound(HyperlinkArray()) ' Looping through our array
Selection.InsertAfter Text:=HyperlinkArray(i) & Chr(13) ' Inserting the
hyperlinks
Selection.Start = Selection.End
Next
BYE:
Selection.ExtendMode = False
Selection.HomeKey Unit:=wdStory ' Returning to the top of the document.
btn = MsgBox("There were" & Str(Count) & " hyperlinks extracted to the 2nd
document.", _
vbOKOnly + vbInformation, _
" Final Results")
End Sub
I am trying to take a block of Emails in word and put them into new Word
Document Format, so then I can Export to Excel and have each Email address in
a cell. I found this code, that I thought would take the block emails put
into new Word doc and Format. I have never programed in Word, and I am just
learning in Excel and Access.
It stops at this line- runtime error 9 subscript out range.
For i = 1 To UBound(HyperlinkArray()) ' Looping through our array
Any help would greatly be appreciated.
'//////////////////////////////////
Option Explicit
Option Base 1
Dim HyperlinkArray() ' Dimension array to contain hyperlinks
Public Sub Main()
Dim btn
Dim pos
Dim pos2
Dim mytemp
Dim i
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
On Error GoTo BYE
Dim Count As Integer
Count = 0
btn = MsgBox("This macro will copy the hyperlinks from the current." &
vbCrLf & _
" document into a new document." & vbCrLf & vbCrLf & _
" Do you WISH TO PROCEED?", vbYesNo + vbQuestion, _
" Startup Message")
If btn = vbNo Then Exit Sub
Selection.HomeKey Unit:=wdStory ' Go to the top of the document
OUTERLOOP:
With Selection.Find
..Text = "<a href" ' Find the start of the hyperlink
..Replacement.Text = ""
..MatchWildcards = False
..Format = False
..Wrap = wdFindStop
..Forward = True
End With
Selection.Find.Execute
If Selection.Find.Found Then
Selection.ExtendMode = True ' Set extension mode to true
With Selection.Find
..Text = ">" ' Finding the end of the hyperlink.
..Replacement.Text = ""
..MatchWildcards = False
..Format = False
..Wrap = wdFindStop
..Forward = True
End With
Selection.Find.Execute
Selection.ExtendMode = False ' Turn the extension mode off
pos = InStr(Selection.Text, Chr(34))
pos2 = InStr(pos + 1, Selection.Text, Chr(34))
mytemp = Mid(Selection.Text, pos + 1, pos2 - (pos + 1))
Count = Count + 1
ReDim Preserve HyperlinkArray(Count) ' Dynamically resizing the array
HyperlinkArray(Count) = mytemp
Selection.Start = Selection.End
GoTo OUTERLOOP
Else
GoTo ARRAY_FEED
End If
ARRAY_FEED:
Documents.Add Template:="", NewTemplate:=False ' Adding a new document
For i = 1 To UBound(HyperlinkArray()) ' Looping through our array
Selection.InsertAfter Text:=HyperlinkArray(i) & Chr(13) ' Inserting the
hyperlinks
Selection.Start = Selection.End
Next
BYE:
Selection.ExtendMode = False
Selection.HomeKey Unit:=wdStory ' Returning to the top of the document.
btn = MsgBox("There were" & Str(Count) & " hyperlinks extracted to the 2nd
document.", _
vbOKOnly + vbInformation, _
" Final Results")
End Sub