Solved

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

Posted on 2013-05-15
2
425 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

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

623 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