Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-05-15
2
Medium Priority
?
434 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
2 Comments
 
LVL 14

Accepted Solution

by:
Faustulus earned 1500 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

885 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