O
Oli
Thank you, Jean-Guy! Believe it or not I was able to change the formulas
(which are links) with the "Replace" Method in embedded excel objects. The
macro worked in a short sample document I created. The formula in the first
cell of the object, for example is: A1 = ='C:\Documents and
Settings\Owner\Desktop\[trial1.xls]Table 2'!A1. I asked in the macro:
Replace "C:\Documents and Settings\Owner\Desktop\[trial1.xls]" with
"C:\Documents and Settings\Owner\Desktop\[trial2.xls]" And, it worked! When I
returned back to my original monster document though, I faced with another
problem. I saw that one of the sheets called "Table10" does not exist in the
replacement file. If I can make the macro to skip to replace when the sheet
doesn't exist or ideallly to put zeros instead, I will have the solution. Do
you think I can do that? Here is what I did (and appears to be working if
the original and the replacement workbooks have the same number of worksheets
and the worksheet names are the same):
Sub ChangeObject()
Dim k As Long
Dim objEXL As Object
Dim boolExcel As Boolean
boolExcel = False
With ActiveDocument
' Loop through all the floating shapes in document.
For k = 1 To .Shapes.Count
With .Shapes(k)
' If the shape's type is an OLE object then...
If .Type = msoEmbeddedOLEObject Then
If .OLEFormat.ProgID = "Excel.Sheet.8" Then
boolExcel = True
.OLEFormat.Activate
Set objEXL = .OLEFormat.Object
With objEXL.ActiveSheet
.Cells.Replace What:="C:\Documents and
Settings\Owner\Desktop\[trial1.xls]", _
Replacement:="C:\Documents and
Settings\Owner\Desktop\[trial3.xls]", LookAt:=xlPart, MatchCase:=False
.Cells.Replace What:="C:\Documents and
Settings\Owner\Desktop\[trial2.xls]", _
Replacement:="C:\Documents and
Settings\Owner\Desktop\[trial4.xls]", LookAt:=xlPart, MatchCase:=False
End With
End If
End If
End With
Next k
End With
If boolExcel Then
SendKeys "{ESC}"
End If
End Sub
(which are links) with the "Replace" Method in embedded excel objects. The
macro worked in a short sample document I created. The formula in the first
cell of the object, for example is: A1 = ='C:\Documents and
Settings\Owner\Desktop\[trial1.xls]Table 2'!A1. I asked in the macro:
Replace "C:\Documents and Settings\Owner\Desktop\[trial1.xls]" with
"C:\Documents and Settings\Owner\Desktop\[trial2.xls]" And, it worked! When I
returned back to my original monster document though, I faced with another
problem. I saw that one of the sheets called "Table10" does not exist in the
replacement file. If I can make the macro to skip to replace when the sheet
doesn't exist or ideallly to put zeros instead, I will have the solution. Do
you think I can do that? Here is what I did (and appears to be working if
the original and the replacement workbooks have the same number of worksheets
and the worksheet names are the same):
Sub ChangeObject()
Dim k As Long
Dim objEXL As Object
Dim boolExcel As Boolean
boolExcel = False
With ActiveDocument
' Loop through all the floating shapes in document.
For k = 1 To .Shapes.Count
With .Shapes(k)
' If the shape's type is an OLE object then...
If .Type = msoEmbeddedOLEObject Then
If .OLEFormat.ProgID = "Excel.Sheet.8" Then
boolExcel = True
.OLEFormat.Activate
Set objEXL = .OLEFormat.Object
With objEXL.ActiveSheet
.Cells.Replace What:="C:\Documents and
Settings\Owner\Desktop\[trial1.xls]", _
Replacement:="C:\Documents and
Settings\Owner\Desktop\[trial3.xls]", LookAt:=xlPart, MatchCase:=False
.Cells.Replace What:="C:\Documents and
Settings\Owner\Desktop\[trial2.xls]", _
Replacement:="C:\Documents and
Settings\Owner\Desktop\[trial4.xls]", LookAt:=xlPart, MatchCase:=False
End With
End If
End If
End With
Next k
End With
If boolExcel Then
SendKeys "{ESC}"
End If
End Sub