How can I programmatically relink OLE's in a Word document?

O

Oli

I created a Microsoft Word document that contains dozens of linked objects
(OLE's). I am searching for a method to programmatically search and replace
all these links.

In searching online, I found code to perform this task for both Access and
PPT ... but not for Word.

I have also found a program called "ReplaceMagic," but I need this solution
for work and they'll never let me download third-party software for this. I
need a macro.

Here are the solutions I discovered for Access and PPT:

MS Access: "ACC2000: How to Programmatically Link or Embed an Object on a
Form (Article ID 209990)" http://support.microsoft.com/kb/209990

MS PowerPoint: "PPT2000: Sample Code to Change Source of Linked Excel
Worksheet (Article ID 222708)" http://support.microsoft.com/kb/222708

If anyone has a solution for Word, I'd really appreciate it!
 
J

Jean-Guy Marcil

Oli was telling us:
Oli nous racontait que :
I created a Microsoft Word document that contains dozens of linked
objects (OLE's). I am searching for a method to programmatically
search and replace all these links.

In searching online, I found code to perform this task for both
Access and PPT ... but not for Word.

I have also found a program called "ReplaceMagic," but I need this
solution for work and they'll never let me download third-party
software for this. I need a macro.

Here are the solutions I discovered for Access and PPT:

MS Access: "ACC2000: How to Programmatically Link or Embed an Object
on a Form (Article ID 209990)" http://support.microsoft.com/kb/209990

MS PowerPoint: "PPT2000: Sample Code to Change Source of Linked Excel
Worksheet (Article ID 222708)" http://support.microsoft.com/kb/222708

If anyone has a solution for Word, I'd really appreciate it!

Here's a little something ton get you going. You have to figure out what
changes need to be done to the OLE source name in order to apply the changes
you want.

'_______________________________________
Sub ChangeSource()

Dim k As Long
'Create a variable to store the object reference string.
Dim strLink As String

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 = msoLinkedOLEObject Then
' Change the path to new source and set the update
' type to Automatic.
With .LinkFormat
' Get the source path in a string
strLink = .SourceFullName
' Do something to strLink to modify it as you wish:

'Code to modify strLink

.SourceFullName = strLink
.Update
End With
End If
End With
Next k

End With

End Sub
'_______________________________________

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
O

Oli

Thank you very much for the response, Jean-Guy! I tried to complete the
sample code you gave me but I was not succesful. This is my first attempt to
wtite a code in my life, I don't quite know how to make it work. First of
all, I was told that the sample code in your response applies to shapes and I
might need a second loop,for accessing inlineshapes. (Quite frankly I don't
even know what "inlineshape" is.) For generating from the old sourcefullname
"C:\folder\documents\file1.xls" the new sourcefullname
"C:\folder\documents\file2.xls", I was told that I need some string
processing. The slot for string processing is below (in capital letters):

[snip]

With .LinkFormat
strLink = .SourceFullName ' e.g. "c:\test\excel\book2.xls"
' YOUR STRING PROCESSING
.SourceFullName = "c:\test\excel\book2.xls"
.Update
End With

[snip]

Can anybody help with this string processing? Thanks in advance for the help!
 
J

Jean-Guy Marcil

Oli was telling us:
Oli nous racontait que :
Thank you very much for the response, Jean-Guy! I tried to complete
the sample code you gave me but I was not succesful. This is my
first attempt to wtite a code in my life, I don't quite know how to
make it work. First of all, I was told that the sample code in your
response applies to shapes and I might need a second loop,for
accessing inlineshapes. (Quite frankly I don't even know what

Inline shapes are objects that are aligned with the text, as if they were a
character, as opposed to being a floating objects above the text with the
text wrapping around it or flowing under or above the shape.

True, if you have both kind of shapes, you will need two loops. Let us know
exactly what kind of shapes you re dealing with.

"inlineshape" is.) For generating from the old sourcefullname
"C:\folder\documents\file1.xls" the new sourcefullname
"C:\folder\documents\file2.xls", I was told that I need some string
processing. The slot for string processing is below (in capital
letters):

I cannot offer any advice on string processing since I have no idea what
type of changes are necessary..

Show us some example of the starting sourcename and the new one you want to
apply.

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
O

Oli

Thank you, Jean-Guy! I created all my embedded objects by:
Insert\Object\Microsoft Excel Worksheet in the Word menu. They are all
free-floating excel OLE's and they can be positioned anywhere on the page.
All of these objects are linked to the same Excel worksheet. The path name
of this excel workbook is: P:\Folder1\FolderA\Book1.xls. I would like all my
embedded objects be linked to a different Excel worksheet with the new path
name of Q:\Folder2\FolderB\Book2.xls. I am only worried about changing the
path name in every embedded object from P:\Folder1\FolderA\Book1.xls to
Q:\Folder2\FolderB\Book2.xls to sQ:\Folder2\FolderB\Book2.xls since these two
excel sheets store the same type of information in same cells and identical
tabs. The new excel file (Book2.xls) is stored in a different drive and a
different folder. I hope this is clear enough. Thanks in advance for your
help!!
 
J

Jean-Guy Marcil

Oli was telling us:
Oli nous racontait que :
Thank you, Jean-Guy! I created all my embedded objects by:
Insert\Object\Microsoft Excel Worksheet in the Word menu. They are
all free-floating excel OLE's and they can be positioned anywhere on
the page. All of these objects are linked to the same Excel
worksheet. The path name of this excel workbook is:
P:\Folder1\FolderA\Book1.xls. I would like all my embedded objects
be linked to a different Excel worksheet with the new path name of
Q:\Folder2\FolderB\Book2.xls. I am only worried about changing the
path name in every embedded object from P:\Folder1\FolderA\Book1.xls
to Q:\Folder2\FolderB\Book2.xls to sQ:\Folder2\FolderB\Book2.xls

Stuttering? ;-) or Double CTRL-V?
Why "to Q:\Folder2\FolderB\Book2.xls to sQ:\Folder2\FolderB\Book2.xls"
I will assume you meant to write
"I am only worried about changing the path name in every embedded object
from P:\Folder1\FolderA\Book1.xls to Q:\Folder2\FolderB\Book2.xls since
these two..."
since these two excel sheets store the same type of information in
same cells and identical tabs. The new excel file (Book2.xls) is
stored in a different drive and a different folder. I hope this is
clear enough. Thanks in advance for your help!!

Then if you have only floating objects, you will not need a second loop.

Try this (untested):

'_______________________________________
Sub ChangeSource()

Dim k As Long
'Create a variable to store the object reference string.
Dim strLink As String
Dim lngPos As Long

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 = msoLinkedOLEObject Then
' Change the path to new source
With .LinkFormat
' Get the source path in a string

' Find where in the source path string the
' character "!" occurs, and assign the position
' to the variable lngPos.
lngPos = InStr(1, .SourceFullName, "!", _
vbTextCompare)
' Assign linkname to worksheet reference at the
' end of the source file path.
strLink = Right(.SourceFullName, _
Len(.SourceFullName) - lngPos)
.SourceFullName = "Q:\Folder2\FolderB\Book2.xls!" &
strLink
.Update
End With
End If
End With
Next k

End With

End Sub
'_______________________________________

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
O

Oli

Unfortunately, it doesn't seem to be working. When I use VBA debug in the
menu, it points out the following commands (please see the arrows below) as
errors. It is a little weird that whatever I put at the beginning of the
code for "Sub --------", it says that it's not valid. Any ideas what might
be happening? Thank you!!

--> Sub ChangeSource()

Dim k As Long
'Create a variable to store the object reference string.
Dim strLink As String
Dim lngPos As Long

--> 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 = msoLinkedOLEObject Then
' Change the path to new source
With .LinkFormat
' Get the source path in a string

' Find where in the source path string the
' character "!" occurs, and assign the position
' to the variable lngPos.
lngPos = InStr(1, .SourceFullName, "!", _
vbTextCompare)
' Assign linkname to worksheet reference at the
' end of the source file path.
strLink = Right(.SourceFullName, _
Len(.SourceFullName) - lngPos)
.SourceFullName = "Q:\Folder2\FolderB\Book2.xls!" &
strLink
.Update
End With
--> End If
--> End With
--> Next k

End With

End Sub
 
J

Jean-Guy Marcil

Oli was telling us:
Oli nous racontait que :
Unfortunately, it doesn't seem to be working. When I use VBA debug in
the menu, it points out the following commands (please see the arrows
below) as errors. It is a little weird that whatever I put at the
beginning of the code for "Sub --------", it says that it's not
valid. Any ideas what might be happening? Thank you!!

--> Sub ChangeSource()

Dim k As Long
'Create a variable to store the object reference string.
Dim strLink As String
Dim lngPos As Long

--> 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 = msoLinkedOLEObject Then
' Change the path to new source
With .LinkFormat
' Get the source path in a string

' Find where in the source path string the
' character "!" occurs, and assign the position
' to the variable lngPos.
lngPos = InStr(1, .SourceFullName, "!", _
vbTextCompare)
' Assign linkname to worksheet reference at the
' end of the source file path.
strLink = Right(.SourceFullName, _
Len(.SourceFullName) - lngPos)
.SourceFullName = "Q:\Folder2\FolderB\Book2.xls!"
& strLink
.Update
End With
--> End If
--> End With
--> Next k

End With

End Sub

Where is your code? (What module name?)
What Word version?

As for the error on the Sub line, this usually happens when you use an
ambiguous word as a Sub name. Try calling a macro
Sub End()
....

As far as I know, "ChangeSource" should be OK as a name.

I compiled the code as you posted it (after removing the -->, of course) and
I did not get any error.

Are you sure you did not forget any periods or that you do not have any
weird characters that followed from a copy/paste?

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
T

Tony Strazzeri

Hi Oli,

Jean-Guy's procedure is a selfcontained subroutine i.e begins with
"Sub ChangeSource" (where ChangeSource is the procedure name) and ends
with "End Sub"

Are you by any chance inserting it within another procedure
definition?
i.e. within another "Sub xxx"/Sub End" pair.

Cheers
TonyS.
 
O

Oli

Thank you again, Jean-Guy! And, sorry for the length of my reply below.

First of all, My MS Word Version is 2002 and I am trying to add the code in
MS Visual Basic 6.3.

Secondly, I realize now that I failed to explain clearly what I am trying to
accomplish. I believe I know why your code is not working for me: My Word
document is linked to TWO different Excel documents, not one. Moreover, both
Excel sheets have 196 different tabs. What I want to accomplish is this:

1) Find the first Excel source: P:\Folder1\FolderA\Book1.xls and replace it
with Q:\Folder3\FolderC\Book3.xls
2) Then find the second Excel source P:Folder2\FolderB\Book2.xls and replace
it with Q:\Folder4\FolderD\Book4.xls

If you click on any embedded Excel object and go to any cell, the Excel
workbook reference will be shown with brackets and we will see the tab name
and cell references. For example the first cell of the embedded sheet will
say: A1='P:\Folder1\FolderA\[Book1.xls]Table1'!A2
And, another cell will say: B1= A1='P:\Folder1\FolderA\[Book1.xls]Table2'!A2

Please note that tab names and cell references are identical in replacement
Excel sheets. Meaning, the linked Excel workbooks are saved under different
directories and different folders with different names. However, the tab
names are the same.

Given my problem, I tried a different solution. I attempted to change your
code as in the following. Naturally, it didn't work. (I would have fainted
if it did.......)

Since you know now what is going on, I hope you can correct the mess I put
below. When I run the following macro, it gives me an error of "Compile
error: Method or data member not found" for the first "If .SourceFullName"
and it stops.

By the way, if it helps, I was inspired by the macro I found at:
http://pptfaq.com/FAQ00759.htm

Thank you so much for bearing with me!!!

-------------

Sub ChangeSource()

Dim k As Long
'Create a variable to store the object reference string.
Dim s As Shape
Dim StrSource1 As String
Dim StrSource2 As String
Dim StrLink1 As String
Dim SrtLink2 As String


'Edit this to reflect the paths you want to change
'Include just the portion of the path you want to change
'For example, to change links to reflect that files have moved from
'\\boss\P-drive\temp\*.* to
'\\boss\Q-drive\temp\*.*

StrSource1 = "P:\Folder1\FolderA\[Book1.xls]"
StrSource2 = "P:Folder2\FolderB\[Book2.xls]"
StrLink1 = "Q:\Folder3\FolderC\[Book3.xls]"
StrLink2 = "Q:\Folder4\FolderD\[Book4.xls]"

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 = msoLinkedOLEObject Then
' Change Source1 to Link1
If .SourceFullName = StrSource1 Then
' Verify that file exists
If Len(Dir$(Replace(s.LinkFormat.SourceFullName,
StrSource1, StrLink1))) > 0 Then
s.LinkFormat.SourceFullName =
Replace(s.LinkFormat.SourceFullName, StrSource1, StrLink1)
End If
Else
' Change Source2 to Link2
If .SourceFullName = StrSource2 Then
' Verify that file exists
If Len(Dir$(Replace(s.LinkFormat.SourceFullName,
StrSource2, StrLink2))) > 0 Then
s.LinkFormat.SourceFullName =
Replace(s.LinkFormat.SourceFullName, StrSource2, StrLink2)
End If
End If
End If
.Update
End With
End If
End With
Next k

MsgBox ("Done!")

End With

End Sub
 
J

Jean-Guy Marcil

Oli was telling us:
Oli nous racontait que :
Thank you again, Jean-Guy! And, sorry for the length of my reply
below.

First of all, My MS Word Version is 2002 and I am trying to add the
code in MS Visual Basic 6.3.

Secondly, I realize now that I failed to explain clearly what I am
trying to accomplish. I believe I know why your code is not working
for me: My Word document is linked to TWO different Excel documents,
not one. Moreover, both Excel sheets have 196 different tabs. What
I want to accomplish is this:

By tabs I guess you mean Worksheets, each having a tab at the bottom with
its name.
1) Find the first Excel source: P:\Folder1\FolderA\Book1.xls and
replace it with Q:\Folder3\FolderC\Book3.xls
2) Then find the second Excel source P:Folder2\FolderB\Book2.xls and
replace it with Q:\Folder4\FolderD\Book4.xls

If you click on any embedded Excel object and go to any cell, the
Excel workbook reference will be shown with brackets and we will see

Where will it be shown?
In Word, If I simply click on an embedded linked Excel cell range, it just
gets selected. If I double click on it, it opens the Excel workbook.
I could not see what you are seeing.
the tab name and cell references. For example the first cell of the
embedded sheet will say: A1='P:\Folder1\FolderA\[Book1.xls]Table1'!A2
And, another cell will say: B1=
A1='P:\Folder1\FolderA\[Book1.xls]Table2'!A2

Please note that tab names and cell references are identical in
replacement Excel sheets. Meaning, the linked Excel workbooks are
saved under different directories and different folders with
different names. However, the tab names are the same.

Given my problem, I tried a different solution. I attempted to
change your code as in the following. Naturally, it didn't work. (I
would have fainted if it did.......)

Since you know now what is going on, I hope you can correct the mess
I put below. When I run the following macro, it gives me an error of
"Compile error: Method or data member not found" for the first "If
.SourceFullName" and it stops.


This is because you removed the
With .LinkFormat
line.
Also, there is an error in your variable declaration and variable use (Dim
SrtLink2 As String vs StrLink2)
It is a good habit to use
Option Explicit
at the top of the code. If you go in the VBA options and preferences, you
can get it to add it automatically whenever you create a module.
I do. It saves grief on debugging by highlighting undeclared variables,
objects and other syntactic errors.
Also, you are using
s.LinkFormat.SourceFullName
You do declare what s is, but you do not set it in code, so if the code had
reached that line, you would have gotten a 91 error type, meaning that an
object has not been set.
You would have needed
Set s = something
in your code.
Finally, your With/End With and If/End If blocks were out of whack...
missing a few End or having too many of them... I did not analyse this too
much as I was rewriting the code.

Here is code that I finally tested. (I was writing code "blind" before..)
The SourceFullName does not contain information about worksheets and cell
ranges...
Since you seem to be saying that those do not change, the code can be kept
simple.

Try this:

'_______________________________________
Option Explicit
'_______________________________________
Sub ChangeSource()

Dim k As Long
'Create a variable to store the object reference string.
Dim strSource1 As String
Dim strSource2 As String
Dim strLink1 As String
Dim strLink2 As String

'Edit this to reflect the paths you want to change
'Include just the portion of the path you want to change
'For example, to change links to reflect that files have moved from
'\\boss\P-drive\temp\*.* to
'\\boss\Q-drive\temp\*.*

strSource1 = "P:\Folder1\FolderA\Book1.xls"
strSource2 = "P:Folder2\FolderB\Book2.xls"
strLink1 = "Q:\Folder3\FolderC\Book3.xls"
strLink2 = "Q:\Folder4\FolderD\Book4.xls"

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 = msoLinkedOLEObject Then
' Change Source1 to Link1
With .LinkFormat
If .SourceFullName = strSource1 Then
' Verify that file exists
If Len(Dir$(Replace(.SourceFullName, _
strSource1, strLink1))) > 0 Then
.SourceFullName = strLink1
.Update
End If
ElseIf .SourceFullName = strSource2 Then
' Change Source2 to Link2
' Verify that file exists
If Len(Dir$(Replace(.SourceFullName, _
strSource2, strLink2))) > 0 Then
.SourceFullName = strLink2
.Update
End If
End If
End With
End If
End With
Next k
End With

End Sub
'_______________________________________

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
O

Oli

Thank you once again for your response, Jean-Guy!! I was so excited that
your latest code might have been the one which would change all my Excel
links… However, when I ran the macro, absolutely nothing happened. Because
I am so VBA illiterate, I can’t figure out why. There’s something not
working quite right in my computer. I don’t think the code sees the objects
in my Word document (not in my computer anyway). Therefore, I don’t get any
kind of an error message. Nothing changes, because the change is supposed to
happen if there are OLE’s in the document. I am wondering if there is a
checkmark that I need to activate in Visual Basic to make it see my embedded
objects. In Visual Basic Editor, under Tools\References, I checked “MS Excel
11.0 Object Library†which was previously unchecked. But, it didn’t change
anything.

The example below is supposed to open (for editing) the first embedded OLE
object (defined as a shape) on the active document. (Source:
http://msdn2.microsoft.com/en us/library/Bb214380.aspx) When I run it,
nothing happens:
-----------
Sub OpenFirstSource ()
Dim shapesAll As Shapes

Set shapesAll = ActiveDocument.Shapes
If shapesAll.Count >= 1 Then
If shapesAll(1).Type = mso EmbeddedOLEObject Then
shapesAll(1).OLEFormat.Edit
End If
End If

End Sub
-----------
Once again, here is how I put the Excel objects in my document: I inserted
all the objects by “Insert\Object\Microsoft Excel Worksheet.†I copied the
reference cells in the Excel workbooks, and then I returned back to the
embedded Excel objects in Word document and Pasted Special (Paste Link). I
believe this type of object is called an OLE object, correct? If so, why can
I not make the VB see them? It’s quite frustrating. I hate to make you work
Oli was telling us:
Oli nous racontait que :
Thank you again, Jean-Guy! And, sorry for the length of my reply
below.

First of all, My MS Word Version is 2002 and I am trying to add the
code in MS Visual Basic 6.3.

Secondly, I realize now that I failed to explain clearly what I am
trying to accomplish. I believe I know why your code is not working
for me: My Word document is linked to TWO different Excel documents,
not one. Moreover, both Excel sheets have 196 different tabs. What
I want to accomplish is this:

By tabs I guess you mean Worksheets, each having a tab at the bottom with
its name.
1) Find the first Excel source: P:\Folder1\FolderA\Book1.xls and
replace it with Q:\Folder3\FolderC\Book3.xls
2) Then find the second Excel source P:Folder2\FolderB\Book2.xls and
replace it with Q:\Folder4\FolderD\Book4.xls

If you click on any embedded Excel object and go to any cell, the
Excel workbook reference will be shown with brackets and we will see

Where will it be shown?
In Word, If I simply click on an embedded linked Excel cell range, it just
gets selected. If I double click on it, it opens the Excel workbook.
I could not see what you are seeing.
the tab name and cell references. For example the first cell of the
embedded sheet will say: A1='P:\Folder1\FolderA\[Book1.xls]Table1'!A2
And, another cell will say: B1=
A1='P:\Folder1\FolderA\[Book1.xls]Table2'!A2

Please note that tab names and cell references are identical in
replacement Excel sheets. Meaning, the linked Excel workbooks are
saved under different directories and different folders with
different names. However, the tab names are the same.

Given my problem, I tried a different solution. I attempted to
change your code as in the following. Naturally, it didn't work. (I
would have fainted if it did.......)

Since you know now what is going on, I hope you can correct the mess
I put below. When I run the following macro, it gives me an error of
"Compile error: Method or data member not found" for the first "If
.SourceFullName" and it stops.


This is because you removed the
With .LinkFormat
line.
Also, there is an error in your variable declaration and variable use (Dim
SrtLink2 As String vs StrLink2)
It is a good habit to use
Option Explicit
at the top of the code. If you go in the VBA options and preferences, you
can get it to add it automatically whenever you create a module.
I do. It saves grief on debugging by highlighting undeclared variables,
objects and other syntactic errors.
Also, you are using
s.LinkFormat.SourceFullName
You do declare what s is, but you do not set it in code, so if the code had
reached that line, you would have gotten a 91 error type, meaning that an
object has not been set.
You would have needed
Set s = something
in your code.
Finally, your With/End With and If/End If blocks were out of whack...
missing a few End or having too many of them... I did not analyse this too
much as I was rewriting the code.

Here is code that I finally tested. (I was writing code "blind" before..)
The SourceFullName does not contain information about worksheets and cell
ranges...
Since you seem to be saying that those do not change, the code can be kept
simple.

Try this:

'_______________________________________
Option Explicit
'_______________________________________
Sub ChangeSource()

Dim k As Long
'Create a variable to store the object reference string.
Dim strSource1 As String
Dim strSource2 As String
Dim strLink1 As String
Dim strLink2 As String

'Edit this to reflect the paths you want to change
'Include just the portion of the path you want to change
'For example, to change links to reflect that files have moved from
'\\boss\P-drive\temp\*.* to
'\\boss\Q-drive\temp\*.*

strSource1 = "P:\Folder1\FolderA\Book1.xls"
strSource2 = "P:Folder2\FolderB\Book2.xls"
strLink1 = "Q:\Folder3\FolderC\Book3.xls"
strLink2 = "Q:\Folder4\FolderD\Book4.xls"

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 = msoLinkedOLEObject Then
' Change Source1 to Link1
With .LinkFormat
If .SourceFullName = strSource1 Then
' Verify that file exists
If Len(Dir$(Replace(.SourceFullName, _
strSource1, strLink1))) > 0 Then
.SourceFullName = strLink1
.Update
End If
ElseIf .SourceFullName = strSource2 Then
' Change Source2 to Link2
' Verify that file exists
If Len(Dir$(Replace(.SourceFullName, _
strSource2, strLink2))) > 0 Then
.SourceFullName = strLink2
.Update
End If
End If
End With
End If
End With
Next k
End With

End Sub
'_______________________________________

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
O

Oli

Hi Jean-Guy, If you are not tired of responding my questions, I believe I
know why your code didn't work for me. I tried another short sample macro in
my document. This time, instead of .Type = msoLinkedOLEObject, I typed .Type
= msoEmbeddedOLEObject. And, the sample macro successfully ran. I believe
my objects are "embedded objects" rather than "linked objects". Therefore,
when I ran your code below in my document, nothing happened. However, when I
replace msoLinkedOLEObject with msoEmbeddedOLEObject in your code, I receive
an error message of "Object Variable or With Block variable not set" for "If
..SourceFullName = strSource1" line in the code. Is there a way of overcoming
this problem? Thanks!
----

Jean-Guy Marcil said:
Oli was telling us:
Oli nous racontait que :
Thank you again, Jean-Guy! And, sorry for the length of my reply
below.

First of all, My MS Word Version is 2002 and I am trying to add the
code in MS Visual Basic 6.3.

Secondly, I realize now that I failed to explain clearly what I am
trying to accomplish. I believe I know why your code is not working
for me: My Word document is linked to TWO different Excel documents,
not one. Moreover, both Excel sheets have 196 different tabs. What
I want to accomplish is this:

By tabs I guess you mean Worksheets, each having a tab at the bottom with
its name.
1) Find the first Excel source: P:\Folder1\FolderA\Book1.xls and
replace it with Q:\Folder3\FolderC\Book3.xls
2) Then find the second Excel source P:Folder2\FolderB\Book2.xls and
replace it with Q:\Folder4\FolderD\Book4.xls

If you click on any embedded Excel object and go to any cell, the
Excel workbook reference will be shown with brackets and we will see

Where will it be shown?
In Word, If I simply click on an embedded linked Excel cell range, it just
gets selected. If I double click on it, it opens the Excel workbook.
I could not see what you are seeing.
the tab name and cell references. For example the first cell of the
embedded sheet will say: A1='P:\Folder1\FolderA\[Book1.xls]Table1'!A2
And, another cell will say: B1=
A1='P:\Folder1\FolderA\[Book1.xls]Table2'!A2

Please note that tab names and cell references are identical in
replacement Excel sheets. Meaning, the linked Excel workbooks are
saved under different directories and different folders with
different names. However, the tab names are the same.

Given my problem, I tried a different solution. I attempted to
change your code as in the following. Naturally, it didn't work. (I
would have fainted if it did.......)

Since you know now what is going on, I hope you can correct the mess
I put below. When I run the following macro, it gives me an error of
"Compile error: Method or data member not found" for the first "If
.SourceFullName" and it stops.


This is because you removed the
With .LinkFormat
line.
Also, there is an error in your variable declaration and variable use (Dim
SrtLink2 As String vs StrLink2)
It is a good habit to use
Option Explicit
at the top of the code. If you go in the VBA options and preferences, you
can get it to add it automatically whenever you create a module.
I do. It saves grief on debugging by highlighting undeclared variables,
objects and other syntactic errors.
Also, you are using
s.LinkFormat.SourceFullName
You do declare what s is, but you do not set it in code, so if the code had
reached that line, you would have gotten a 91 error type, meaning that an
object has not been set.
You would have needed
Set s = something
in your code.
Finally, your With/End With and If/End If blocks were out of whack...
missing a few End or having too many of them... I did not analyse this too
much as I was rewriting the code.

Here is code that I finally tested. (I was writing code "blind" before..)
The SourceFullName does not contain information about worksheets and cell
ranges...
Since you seem to be saying that those do not change, the code can be kept
simple.

Try this:

'_______________________________________
Option Explicit
'_______________________________________
Sub ChangeSource()

Dim k As Long
'Create a variable to store the object reference string.
Dim strSource1 As String
Dim strSource2 As String
Dim strLink1 As String
Dim strLink2 As String

'Edit this to reflect the paths you want to change
'Include just the portion of the path you want to change
'For example, to change links to reflect that files have moved from
'\\boss\P-drive\temp\*.* to
'\\boss\Q-drive\temp\*.*

strSource1 = "P:\Folder1\FolderA\Book1.xls"
strSource2 = "P:Folder2\FolderB\Book2.xls"
strLink1 = "Q:\Folder3\FolderC\Book3.xls"
strLink2 = "Q:\Folder4\FolderD\Book4.xls"

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 = msoLinkedOLEObject Then
' Change Source1 to Link1
With .LinkFormat
If .SourceFullName = strSource1 Then
' Verify that file exists
If Len(Dir$(Replace(.SourceFullName, _
strSource1, strLink1))) > 0 Then
.SourceFullName = strLink1
.Update
End If
ElseIf .SourceFullName = strSource2 Then
' Change Source2 to Link2
' Verify that file exists
If Len(Dir$(Replace(.SourceFullName, _
strSource2, strLink2))) > 0 Then
.SourceFullName = strLink2
.Update
End If
End If
End With
End If
End With
Next k
End With

End Sub
'_______________________________________

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jean-Guy Marcil

Oli was telling us:
Oli nous racontait que :
Hi Jean-Guy, If you are not tired of responding my questions, I
believe I know why your code didn't work for me. I tried another
short sample macro in my document. This time, instead of .Type =
msoLinkedOLEObject, I typed .Type = msoEmbeddedOLEObject. And, the
sample macro successfully ran. I believe my objects are "embedded
objects" rather than "linked objects". Therefore, when I ran your
code below in my document, nothing happened. However, when I replace
msoLinkedOLEObject with msoEmbeddedOLEObject in your code, I receive
an error message of "Object Variable or With Block variable not set"
for "If .SourceFullName = strSource1" line in the code. Is there a
way of overcoming this problem? Thanks! ----

Embedded objects are not linked objects, therefore they do not have a an
external linked source (They are 100 percent part of the document), i.e nor
SourceFullName property.
You normally get embedded objects through the toolbar or with
Insert > Object > Microsoft Excel Chart or Worksheet.
Also, with embedded objects, we use the OLEFormat property, not the
LinkFormat one.

How did you end up with the Excel objects in your Word document (I mean, how
were they inserted there?)

Here is a sample code on how to manipulate Embedded Excel objects:

'_______________________________________
Sub ChangeSource()

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(1, 1).Value = "New Value"
End With
End If
End If
End With
Next k
End With

If boolExcel Then
SendKeys "{ESC}"
End If

End Sub
'_______________________________________
--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
O

Oli

You are right, Jean-Guy. As I explained in my earlier replies, I inserted
all the objects by “Insert>Object>Microsoft Excel Worksheet.†I had to
insert objects this way, because it was much easier to copy the links and the
formulas, down. I still need to change the data sources in each embedded
excel sheet, though. I edited your code as in the following. I know that it
doesn’t work (it gives an error message of “Object doesn’t support this
property or methodâ€). However, it will give you an idea of what I am trying
to accomplish. Is it possible to change the data sources of the Embedded
Excel Sheets in the Word Document? Thanks in advance for your help!!

Here is my attempted (but unfunctional) revision:

Sub ChangeSource()

Dim k As Long
Dim objEXL As Object
Dim boolExcel As Boolean
Dim strSource1 As String
Dim strSource2 As String
Dim strLink1 As String
Dim strLink2 As String

strSource1 = "C:\Documents and Settings\Desktop\Misc\Book1.xls"
strSource2 = "C:\Documents and Settings\Desktop\Misc\Book2.xls"
strLink1 = "C:\Documents and Settings\Desktop\Misc\Book3.xls"
strLink2 = "C:\Documents and Settings\Desktop\Misc\Book4.xls"

boolExcel = False

With ActiveDocument
' Loop through all the floating shapes in document.
For k = 1 To .Shapes.Count
With .Shapes(k)
' Check whether the shape is an Embedded OLE object.
If .Type = msoEmbeddedOLEObject Then
'Check whether the OLE object is a Excel Sheet 8 object.
If .OLEFormat.ProgID = "Excel.Sheet.8" Then
' Find obtain object reference, and
' then Change Source1 to Link1
boolExcel = True
.OLEFormat.Activate
Set objEXL = .OLEFormat.Object
With objEXL.ActiveSheet
If .SourceDoc = strSource1 Then
' Verify that file exists
If Len(Dir$(Replace(.SourceFullName, _
strSource1, strLink1))) > 0 Then
.SourceDoc = strLink1
.Update
End If
ElseIf .SourceDoc = strSource2 Then
' Change Source2 to Link2
' Verify that file exists
If Len(Dir$(Replace(.SourceFullName, _
strSource2, strLink2))) > 0 Then
.SourceDoc = strLink2
.Update
End If
End If
End With
End If
End If
End With
Next k
End With

If boolExcel Then
SendKeys "{ESC}"
End If

End Sub
 
O

Oli

I just wanted to let you know that I tried “Change Link Method†as in the
following, but this didn’t work either… It gave an error message of “Change
Link Method of Workbook Class failed†:-(
-----------

Sub ChangeObjSource()

Dim k As Long
Dim objEXL As Object
Dim boolExcel As Boolean
Dim strSource1 As String
Dim strSource2 As String
Dim strLink1 As String
Dim strLink2 As String

boolExcel = False

With ActiveDocument
' Loop through all the floating shapes in document.
For k = 1 To .Shapes.Count
With .Shapes(k)
' Check whether the shape is an Embedded OLE object.
If .Type = msoEmbeddedOLEObject Then
'Check whether the OLE object is a Excel Sheet 8 object.
If .OLEFormat.ProgID = "Excel.Sheet.8" Then
' Find object reference, and
' then Change Source1 to Link1
boolExcel = True
.OLEFormat.Activate
Set objEXL = .OLEFormat.Object
With objEXL.ActiveSheet
objEXL.ChangeLink "C:\Documents and
Settings\Desktop\Misc\Book1.xls", _
"C:\Documents and
Settings\Desktop\Misc\Book3.xls", xlExcelLinks
.Update
objEXL.ChangeLink "C:\Documents and
Settings\Desktop\Misc\Book2.xls", _
"C:\Documents and
Settings\Desktop\Misc\Book4.xls", xlExcelLinks
.Update
End With
End If
End If
End With
Next k
End With

If boolExcel Then
SendKeys "{ESC}"
End If

End Sub
 
D

Doug Robbins - Word MVP

See if anything in this helps you:

' Macro created 26/10/01 by Doug Robbins to update links in a document
'
Dim alink As Field, linktype As Range, linkfile As Range
Dim linklocation As Range, i As Integer, j As Integer, linkcode As Range
Dim Message, Title, Default, Newfile
Dim counter As Integer



counter = 0
For Each alink In ActiveDocument.Fields
If alink.Type = wdFieldLink Then

Set linkcode = alink.Code
i = InStr(linkcode, Chr(34))
Set linktype = alink.Code
linktype.End = linktype.Start + i
j = InStr(Mid(linkcode, i + 1), Chr(34))
Set linklocation = alink.Code
linklocation.Start = linklocation.Start + i + j - 1
If counter = 0 Then
Set linkfile = alink.Code
linkfile.End = linkfile.Start + i + j - 1
linkfile.Start = linkfile.Start + i
Message = "Enter the modified path and filename following this
Format " & linkfile
Title = "Update Link"
Default = linkfile
Newfile = InputBox(Message, Title, Default)
End If
linkcode.Text = linktype & Newfile & linklocation
counter = counter + 1
End If
Next alink


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
O

Oli

Thanks, Doug! But, your code wouldn't solve my problem. I am not worried
about updating the field links. I am trying to change the path names in the
embedded excel objects within the word document. Neverthleless, I appreciate
you posting your sample code!

-----
 
J

Jean-Guy Marcil

Oli was telling us:
Oli nous racontait que :
You are right, Jean-Guy. As I explained in my earlier replies, I
inserted all the objects by "Insert>Object>Microsoft Excel
Worksheet." I had to insert objects this way, because it was much
easier to copy the links and the formulas, down. I still need to
change the data sources in each embedded excel sheet, though. I
edited your code as in the following. I know that it doesn't work
(it gives an error message of "Object doesn't support this property
or method"). However, it will give you an idea of what I am trying
to accomplish. Is it possible to change the data sources of the
Embedded Excel Sheets in the Word Document? Thanks in advance for
your help!!

As I wrote before you cannot link an embedded object to an external source.
The object is either linked or embedded, it cannot be both.
So if you want to "link" your embedded objects, you will have to remove
them, and then paste the Excel date as links (Edit > Paste Special).

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
D

Doug Robbins - Word MVP

That is what the code does, allow you to change the path.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top