+ Reply to Thread
Results 1 to 5 of 5

Check if Object is embedded in Excel

Hybrid View

  1. #1
    Registered User
    Join Date
    02-04-2019
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    41

    Check if Object is embedded in Excel

    Hi all,

    I have a requirement to check if an object (can be any format -.docx, .pdf, etc) has been embedded to a spreadsheet.
    The code I have will embed the file to cell P7 as the location.

    There is a 'Check Document' Sub that checks if certain criteria have been completed (e.g. specific cells) in the spreadsheet before uploading to our SharePoint, if not all criteria is satisfied, it will exit the Sub and not post to SP, with a message box advising the user what is missing.
    The code I use to check if cells are populated or not is (should noted that there is another bit of code to create the message box):

    EInv = Worksheets("Registration Form").Range("E101").Value
        If IsEmpty(EInv) Then
            str = str + "Please complete 'E-Invoicing fields'" & vbCrLf
        End If
    I have searched these forums and google but haven't been able to determine how (or if its possible) to check if a file is embedded at cell P7.

    2 Questions

    1st - How can I check if a file (Object) is located in cell P7?
    2nd - Can I amend the above code to so that it will exit the sub if an object is not in the nominated cell?

    Hoping someone can help me with this.
    Thank you in advance,
    Chris

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Check if Object is embedded in Excel

    Hi,

    Is the file likely to be so small you can't see it?

    One way would be

    Dim shp As Shape
        
        For Each shp In ActiveSheet.Shapes
            If Not Intersect(Range("P7"), shp.TopLeftCell) Is Nothing Then
                MsgBox shp.Name & " is over cell P7"
            End If
        Next
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    02-04-2019
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    41

    Re: Check if Object is embedded in Excel

    Hi Richard,

    Thank you for your reply.

    The icon size isn't the problem, if embedded, it is visible.

    AML.jpg

    it's more adding the object/shape check to the existing automation of checking completed worksheet cell criteria (~20 cells checked), when clicking a 'Submit' macro button - with the following outcomes:
    - If all criteria is satisfied, then the 'Submit' SUB will save the Excel file to our SharePoint.
    - If all criteria is not satisfied, then exit the 'Submit' SUB and gives a message box alert to the user to fill in the missing info. (to clarify, the embedded object not being in P7 does not meet criteria)

    If I am understanding your suggested code, it will provide a message box if there is a Object embedded in P7.
    I would like to include, with the other mandatory field checks, and exit the Submit Sub if not present but proceed if the Object/Shape is present in P7.

    Apologies if this is a bit confusing

    I tried to amend your code and integrate it into mine, but it doesn't return an error or add a line in the msgbox, when there is no file embedded (with Shp defined as Shape).

    ' Below are just a few of the Cell checks
    
    CGrp4 = Worksheets("Registration Form").Range("E178").Value
        If IsEmpty(CGrp4) Then
            str = str + "Please select 'Customer Group 4'" & vbCrLf
        End If
    CGrp5 = Worksheets("Registration Form").Range("E179").Value
        If IsEmpty(CGrp5) Then
            str = str + "Please select 'Customer Group 5'" & vbCrLf
        End If
    For Each Shp In ActiveSheet.Shapes
        If Not Intersect(Range("P7"), Shp.TopLeftCell) Is Nothing Then
            str = str + "Please attached AML form" & vbCrLf
    End If
        Next
     
    ' End of cell checks and how the alert msgbox is generated for each cell that is not populated
    
    If Len(str) > 0 Then
      MsgBox str & " " & vbCrLf & "Once completed, submit your request again"
    Exit Sub
    End If
    thanks,
    Chris
    Last edited by ChrisMP; 08-03-2022 at 09:08 AM.

  4. #4
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,244

    Re: Check if Object is embedded in Excel

    One problem with identifying objects embedded in a sheet is that the OLEObject is not associated with a specific cell. Scenario: for example, using a macro, an object is embedded in cell P7 (more precisely, the upper-left corner of the object is in cell P7). An unruly user moved the object (top left corner) to cell P6 (by 1-2 pixels at most). Searching the addresses of the "source" cells with the macro, we are not able to check whether the required object has been added to the sheet. We see that it is there, while the macro "does not see".
    There are at least two solutions to this problem. Both require giving a proper name to the embedded object.
    1 Either the name of the object will contain the address of the cell with which we will bind it. Then, by searching OLE objects, we can see if the object has been associated with a specific cell. Example of the name P7_Object1 or E178_Object2
    2. or we give each OLE object a specific (unique) name, e.g. CreditAppForm, AMLForm, VATCert, VATExCert, etc. Then we check that the sheet has all the objects with the required names.

    Assuming that the first form of object naming was chosen, the macro to check for the presence of OLE objects could look like this:
    Sub Test_Art()
        Dim objEmbed      As OLEObject
        Dim rngCellsOLE As Range
        Dim rngCell     As Range
        Dim rngOLE      As Range
        Dim strMsg      As String
        Dim rngUnionOLECells As Range
        Dim ActvSh      As Worksheet
    
        Set ActvSh = ActiveSheet
    
        'addresses of required cells associated with OLE objects
        Set rngCellsOLE = ActvSh.Range("C9,I9,C15,I15")
    
        On Error Resume Next
    
        'try to associate OLE objects with cells (based on the object name)
        For Each objEmbed In ActvSh.OLEObjects
            Set rngOLE = Nothing
    
            If objEmbed.OLEType = xlOLEEmbed Then
                Set rngOLE = ActvSh.Range(Split(objEmbed.Name, "_")(0))
    
                If Not rngOLE Is Nothing Then
                    If rngUnionOLECells Is Nothing Then
                        Set rngUnionOLECells = rngOLE
                    Else
                        Set rngUnionOLECells = Union(rngUnionOLECells, rngOLE)
                    End If
                End If
            End If
        Next objEmbed
    
        'check if any of the objects are missing
        For Each rngCell In rngCellsOLE
            If Intersect(rngCell, rngUnionOLECells) Is Nothing Then
                strMsg = GenerateMsg(strMsg, rngCell.Address(0, 0))
            End If
        Next rngCell
    
        On Error GoTo 0
    
    
        If Len(strMsg) Then
            MsgBox strMsg
            Exit Sub
        End If
    
    
        'let the macro go on
        '...
    End Sub
    
    
    Function GenerateMsg(strMsg As String, strAddress As String) As String
      Dim strTmp As String
        Select Case strAddress
          Case "C9"
            strTmp = "Credit Application Form"
          Case "I9"
            strTmp = "AML Form"
          Case "C15"
            strTmp = "VAT Certificate"
          Case "I15"
            strTmp = "VAT Exempt Certificate"
        End Select
        
        If Len(strTmp) > 0 Then
          GenerateMsg = strMsg & "Please attached " & strTmp & vbLf
        End If
    End Function
    You should also add a line of code in the macros that insert an OLE object into the sheet, which will appropriately name each of the inserted objects.

    Artik

  5. #5
    Registered User
    Join Date
    02-04-2019
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    41

    Re: Check if Object is embedded in Excel

    Thank you Artik, very much appreciate the time taken to reply and to explain,
    I will play around and see if I can get this to work.

    Again, thank you very much.
    Chris

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Embedded word object in excel as TEXT
    By chaohung1115 in forum Excel General
    Replies: 0
    Last Post: 10-13-2019, 12:38 PM
  2. [SOLVED] Embedded Excel Object - Updating Formulas
    By bradlehoux in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 11-02-2018, 04:11 PM
  3. Excel Object embedded in PPT resizes
    By SUPPO_USN in forum Excel General
    Replies: 1
    Last Post: 06-02-2017, 09:49 AM
  4. Macro to access embedded excel object
    By SUPPO_USN in forum PowerPoint Formatting & General
    Replies: 1
    Last Post: 04-27-2017, 09:19 AM
  5. Help with VBA Excel Range to Embedded Word Object
    By cuchulainem in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-28-2013, 04:53 PM
  6. set calculation property in embedded excel object
    By erickd in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 11-17-2010, 03:55 PM
  7. Embedded Excel Object - Containers Path and Name
    By GerryO in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-21-2006, 10:40 AM

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