+ Reply to Thread
Results 1 to 5 of 5

How to Insert OLEObject via User Defined Formula

Hybrid View

  1. #1
    Registered User
    Join Date
    03-01-2012
    Location
    Suffolk, UK
    MS-Off Ver
    Excel 2010
    Posts
    7

    How to Insert OLEObject via User Defined Formula

    Hi

    I need to insert an OLEObject (in this case a pdf file) into a worksheet via a formula entered into a cell.

    I have a sub InsertPDF that does the job fine when run by itself. I have a function PDF_Added that calls that sub with no problem if the function is in turn run from another sub. However, I need to make the PDF_Added function run when entered into a cell as =PDF_Added(). This results in this error message: Unable to get the Add property of the OLEObjects class. Can anyone see a way to add an OLEObject to the worksheet via user defined formula?

    Any help much appreciated, thanks.
    Sam

    Sub InsertPDF()
        Dim pdf As OLEObject
        
        Set pdf = ActiveSheet.OLEObjects.Add( _
                fileName:="C:\Documents and Settings\spetty\Desktop\pdf1.pdf", _
                Link:=False, _
                DisplayAsIcon:=True, _
                IconFileName:="C:\WINDOWS\Installer\{AC76BA86-7AD7-1033-7B44-A71000000002}\PDFFile.ico", _
                IconIndex:=0, _
                IconLabel:="myCaption")
            
    End Sub
    
    
    Function PDF_Added() As Boolean
    
        PDF_Added = False
        
        On Error Resume Next
        
        Call InsertPDF
        
        If Err = 0 Then
            PDF_Added = True
            Err = 0
        End If
    
    End Function

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: How to Insert OLEObject via User Defined Formula

    Sam

    It isn't possible to change a worksheet using a UDF on a worksheet, and I kind of think inserting an OLE object might just come under change.

  3. #3
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: How to Insert OLEObject via User Defined Formula

    I doubt that you can, Excel won't let you do a lot of things through a worksheet function mainly relating to adding to/altering the structure of the workbook or other cells

  4. #4
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: How to Insert OLEObject via User Defined Formula

    Cell formulas cannot change the formatting of worksheets or insert objects.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  5. #5
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: How to Insert OLEObject via User Defined Formula

    Well I guess that gives you a universal consensus then

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1