Accessing linked object in Excel using office Add-In

C

C Duden

From an office Add-In, I am able to access a linked object (Picture, Text)
in Word via the LinkFormat object (see code to follow). I am looking for a
way to access this (or an analogous) collection in an excel Workbook (or
Worksheet).



Does anyone have any ideas?







Simplified Code used to access Links in a Word Document (this works):



Word.Field WordField;

Word.LinkFormat WordLinkFormat;



for(int i=1; i<doc.Fields.Count+1; i++)

{

WordField = doc.Fields;

WordLinkFormat = WordField.LinkFormat;

WordLinkFormat.SourceFullName = "<Some Value I want to set>";

}







Any help would be appreciated!
 
W

Wei-Dong Xu [MSFT]

Hi Duden,

Very good question!

In excel, pictures, the link object, can be accessed from shapes collections in Excel. The shape collections contains many shape object in one excel
file. The Shape object represents an object in the drawing layer, such as an AutoShape, freeform, OLE object, or picture. You can check the type
property of shape object to determine whether the shape object is a linked object. I list sample codes for you:

'Code begin
For Each s In Worksheets(1).Shapes
If s.Type = msoLinkedOLEObject Then s.LinkFormat.Update
Next
'Code end

The msoLinkedOLEObject constant is one member of MsoShapeType. In this way, you can get all the linked object from excel worksheet.
Furthermore, you can also use ShapeRange collection to get all the linked objects by specifying the type property.

Please feel free to let me know if you have any further questions.

Does this answer your question? Thank you for using Microsoft NewsGroup!

Wei-Dong Xu
Microsoft Product Support Services
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
C

C Duden

Wei-Dong Xu,

Thanks for the help on this, your response was right to the point!

One quick follow up:

When I use the WordLinkFormat object (Word), I can change the source of the
link via the "SourceFullName" property (see sample code below). Is there a
way to do this using the shape object (Excel)?

// sample code start
WordLinkFormat.SourceFullName = "C:\MyDirectory\MyImg.jpg";
// sample coe end
 
W

Wei-Dong Xu [MSFT]

Hi Duden,

Thank you for replying!

In excel, you can also specify a new path to the sourcename to change the source. Please read the codes below:
'Code begin ------
Dim objShapes As Shapes
Dim objShape As Shape
Dim sTemp As String

Set objShapes = Application.ActiveSheet.Shapes
'change all the linked file to the same source "c:\hello.txt"
For Each objShape In objShapes
If objShape.Type = msoLinkedOLEObject Then
sTemp = "Package|" & "c:\hello.txt" & "!'"
objShape.OLEFormat.Object.SourceName = sTemp
End If
Next
'Code end ------

Please note to put "Pacakage|" in the head of the string and append "|'" at the end of the source string.

Please feel free to let me know if you have any further questions.

Does this answer your question? Thank you for using Microsoft NewsGroup!

Wei-Dong Xu
Microsoft Product Support Services
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
C

C Duden

Wei-Dong Xu,



You definitely answered my question. Your help is much appreciated!



Do you know if it is possible to use this methodology to link to an object
provided by a web application? Where, for instance, my sTemp variable (in
the example you provided) would be something like:



sTemp = "Package|" +
@"http://localhost/WebApplication1/WebForm1.aspx?ImageId=2" + "!'"





The web app returns a streamed image (or other document).





Wei-Dong Xu said:
Hi Duden,

Thank you for replying!

In excel, you can also specify a new path to the sourcename to change the
source. Please read the codes below:
'Code begin ------
Dim objShapes As Shapes
Dim objShape As Shape
Dim sTemp As String

Set objShapes = Application.ActiveSheet.Shapes
'change all the linked file to the same source "c:\hello.txt"
For Each objShape In objShapes
If objShape.Type = msoLinkedOLEObject Then
sTemp = "Package|" & "c:\hello.txt" & "!'"
objShape.OLEFormat.Object.SourceName = sTemp
End If
Next
'Code end ------

Please note to put "Pacakage|" in the head of the string and append "|'"
at the end of the source string.
 
W

Wei-Dong Xu [MSFT]

Hi Duden,

Thank you for replying!

If you insert one object from http as file, excel will try to download one copy in the temp directory of your box and then link this file which is a copy.
For example, if you link to the file located at http://localhost:808/hello.txt, the file will be downloaded to "C:\Documents and Settings\v-wdxu\Local
Settings\Temporary Internet Files\Content.MSO\351955B0.txt" and then excel will link to this file. So far as I know, specifying the link in the
sourcename property can help little on this issue. You'd better retrieve the file from the server and then link to it.

Please feel free to let me know if you have any further questions.

Does this answer your question? Thank you for using Microsoft NewsGroup!

Wei-Dong Xu
Microsoft Product Support Services
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
C

C Duden

Wei-Dong Xu,

Your information has by very helpful. Thanks again for all your help!


Wei-Dong Xu said:
Hi Duden,

Thank you for replying!

If you insert one object from http as file, excel will try to download one
copy in the temp directory of your box and then link this file which is a
copy.
For example, if you link to the file located at
http://localhost:808/hello.txt, the file will be downloaded to "C:\Documents
and Settings\v-wdxu\Local
Settings\Temporary Internet Files\Content.MSO\351955B0.txt" and then excel
will link to this file. So far as I know, specifying the link in the
sourcename property can help little on this issue. You'd better retrieve
the file from the server and then link to it.
 
W

Wei-Dong Xu [MSFT]

Hi Duden,

Thank you for replying!

It is my pleasure to be some of service.

Thank you once more for using Microsoft NewsGroup!

Wei-Dong Xu
Microsoft Product Support Services
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

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