TimLundSE26
New Member
- Joined
- Apr 30, 2011
- Messages
- 11
I want to run any number of different bits of code, at different times for each cell in various different ranges, without every time having to write a separate For each ... next loop every time. So I wrote the following
and also with another macro more complex macro - that modified addresses of hyperlinks in cells.
But with some code to edit a reference number into the Word document that these hyperlinks refer to, I get a 1004 message
So that for this macro to work
I have to go back to writing an explicit For Each ... next loop.
What is different about my sub "EditHyperLinkFootnote" that causes this?
Public Sub ApplyToEach(strMacro As String, strHeading As String, ParamArray args())
Dim c As Excel.Range
End Sub
And successfully tested it as follows:Dim c As Excel.Range
For Each c In Range(strHeading)
Select Case UBound(args)
Case -1
Run strMacro, c
Case 0
Run strMacro, c, args(0)
Case 1
Run strMacro, c, args(0), args(1)
End Select
Next
Select Case UBound(args)
Case -1
Run strMacro, c
Case 0
Run strMacro, c, args(0)
Case 1
Run strMacro, c, args(0), args(1)
End Select
Next
End Sub
Public Sub TestFunctionals()
ApplyToEach "ShowAddress", "Booking_ref"
End Sub
ApplyToEach "ShowAddress", "Booking_ref"
End Sub
Public Sub ShowAddress(c As Excel.Range)
MsgBox c.Address
End Sub
MsgBox c.Address
End Sub
and also with another macro more complex macro - that modified addresses of hyperlinks in cells.
But with some code to edit a reference number into the Word document that these hyperlinks refer to, I get a 1004 message
Public Sub EditHyperLinkFootnote(c As Excel.Range, iRefOffset)
Dim appWord As Word.Application
Dim oWordDoc As Word.Document
If c.Hyperlinks.Count = 1 Then
On Error Resume Next
Set appWord = GetObject(, "Word.Application")
If Err.Number > 0 Then Set appWord = CreateObject("Word.Application")
Err.Clear
On Error GoTo 0
appWord.Visible = True
Application.DisplayAlerts = False
c.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
If Right(c.Hyperlinks(1).Address, 4) = ".doc" Then
Set oWordDoc = appWord.Documents(appWord.Documents.Count)
With oWordDoc
.Sections(1).Footers(wdHeaderFooterPrimary).Range.Text = "Bedlam Bunch ref" & vbTab & vbTab & c.Offset(0, iRefOffset).Value
.Close SaveChanges:=True
End With
End If
End If
End Sub
Dim appWord As Word.Application
Dim oWordDoc As Word.Document
If c.Hyperlinks.Count = 1 Then
On Error Resume Next
Set appWord = GetObject(, "Word.Application")
If Err.Number > 0 Then Set appWord = CreateObject("Word.Application")
Err.Clear
On Error GoTo 0
appWord.Visible = True
Application.DisplayAlerts = False
c.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
If Right(c.Hyperlinks(1).Address, 4) = ".doc" Then
Set oWordDoc = appWord.Documents(appWord.Documents.Count)
With oWordDoc
.Sections(1).Footers(wdHeaderFooterPrimary).Range.Text = "Bedlam Bunch ref" & vbTab & vbTab & c.Offset(0, iRefOffset).Value
.Close SaveChanges:=True
End With
End If
End If
End Sub
So that for this macro to work
Public Sub EditHyperLinkFootnotes()
Dim c As Excel.Range
' ApplyToEach "EditHyperLinkFootnote", "Link", Range("Booking_ref").Column - Range("Link").Column
For Each c In wsBookings.Range("Link")
EditHyperLinkFootnote c, -1
Next
End Sub
Dim c As Excel.Range
' ApplyToEach "EditHyperLinkFootnote", "Link", Range("Booking_ref").Column - Range("Link").Column
For Each c In wsBookings.Range("Link")
EditHyperLinkFootnote c, -1
Next
End Sub
I have to go back to writing an explicit For Each ... next loop.
What is different about my sub "EditHyperLinkFootnote" that causes this?