Solved

How to recreate OLEobject (linked word doc) after link is broken (invalid link has been set)

Posted on 2013-05-15
2
420 Views
Last Modified: 2013-05-22
Hi,

I have created an Excel dashboard that displays the content of an appropriate linked word document in the form of an OLEobject. The links (path & filename) are stored in a list and when an icon (representing a piece of equipment) is clicked, my macro looks up the link and changes the SourceName property  of the OLEobject appropriately. This works fine until an invalid link is set (and this is a possibility as I will not be maintaining the list).

When this happens the OLEobject changes into a simple Shape i.e. it ceases to be an OLEobject.

If another icon is clicked which does have a valid path, I would like to programmatically "reset" the shape back to being an OLEobject but can't find a way to do this. I can test to see if the link has been broken by cycling through the OLEobjects collection but that is all.

My OLE object has been named "LinkedWordDoc1". The path & file name of the word document are contained in the string sWordDoc.

Here is the relevant bit of my code:

 
   'test for valid OLEobject - linked word doc
    bFound = False
    For Each oOLEobj In ActiveSheet.OLEObjects
        If oOLEobj.Name = "LinkedWordDoc1" Then
            bFound = True
            Exit For
        End If
    Next
    
    'display associated word doc
    If bFound Then
        ActiveSheet.OLEObjects("LinkedWordDoc1").SourceName = "Word.Document.12|" & sWordDoc & "!'"
    Else
    'recreate OLE object
        With ActiveSheet.Shapes.Range(Array("LinkedWordDoc1"))
        
              'THIS IS WHERE THE PROBLEM IS!!!!!!!

        End With
           
    End If

Open in new window

   

Thanks,

Neil
0
Comment
Question by:younglight
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 14

Accepted Solution

by:
Faustulus earned 500 total points
ID: 39174214
Once the link is broken the OLEobject ceases to exist. You need to recreate it from scratch. This code will do the job.
    Else
    'recreate OLE object
        Set oOLEobj = ActiveSheet.OLEObjects.Add(Filename:=sWordDoc, _
                      Link:=True, DisplayAsIcon:=False)
        With oOLEobj
            .Name = "LinkedWordDoc1"
            .Top = 100
            .Left = 50
        End With
    End If

Open in new window

The values of Top and Left define the upper left corner of the object in points. You may have to experiment a little or read the values from an object that already exists.
0
 

Author Closing Comment

by:younglight
ID: 39188695
Ok thanks. I'll just need to delete the "shape" left when the link was broken.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question