locked
VBA code to insert an object into excel RRS feed

  • Question

  • Hi everyone,

    please would someone help me how to make a VBA code to insert an object into excel sheet and attach it in a certain cell not anywhere?

    the same when we make the following actions ....insert>object>create from file >browse ....


    thank you.

    Omar 


    Saturday, July 20, 2019 10:17 AM

All replies

  • Omar,
    Re:  Insert object from file

    From the VBA help file; a couple of examples at bottom...

    Shapes.AddOLEObject Method
    Creates an OLE object. Returns a Shape object that represents the new OLE object.
    Syntax:
    expression.AddOLEObject(ClassType, Filename, Link, DisplayAsIcon, IconFileName, IconIndex, IconLabel, _
                    Left, Top, Width, Height)
    expression   A variable that represents a Shapes object.

    Parameters:
    ClassType Optional Variant (you must specify either ClassType or FileName).
    A string that contains the programmatic identifier for the object to be created.
    If ClassType is specified, FileName and Link are ignored.

    Filename Optional Variant The file from which the object is to be created.
     If the path isn’t specified, the current working folder is used.
    You must specify either the ClassType or FileName argument for the object, but not both.

    Link Optional Variant True to link the OLE object to the file from which it was created.
    False to make the OLE object an independent copy of the file.
    If you specified a value for ClassType, this argument must be False. The default value is False.

    DisplayAsIcon Optional Variant True to display the OLE object as an icon. The default value is False.

    IconFileName Optional Variant The file that contains the icon to be displayed.

    IconIndex Optional Variant The index of the icon within IconFileName.
    The order of icons in the specified file corresponds to the order in which the icons appear in the Change Icon dialog box
    (accessed from the Object dialog box when the Display as icon check box is selected).
    The first icon in the file has the index number 0 (zero). If an icon with the given index number doesn't exist in IconFileName,
     the icon with the index number 1 (the second icon in the file) is used. The default value is 0 (zero).

    IconLabel Optional Variant A label (caption) to be displayed beneath the icon.

    Left Optional Variant The position (in points) of the upper-left corner of the new object relative to the upper-left corner of the document.
    The default value is 0 (zero).

    Top Optional Variant The position (in points) of the upper-left corner of the new object relative to the upper-left corner of the document.
    The default value is 0 (zero).

    Width Optional Variant The initial dimensions of the OLE object, in points.
    Height Optional Variant The initial dimensions of the OLE object, in points.

    Return Value
    Shape
    '---------------

    This example adds a linked Word document to myDocument.
      Set myDocument = Worksheets(1)
      myDocument.Shapes.AddOLEObject Left:=100, Top:=100, _
      Width:=200, Height:=300, _
      FileName:="c:\my documents\testing.doc", link:=True

    This example adds a new command button to myDocument.
      Set myDocument = Worksheets(1)
      myDocument.Shapes.AddOLEObject Left:=100, Top:=100, _
      Width:=100, Height:=200, _
      ClassType:="Forms.CommandButton.1"
    © 2010 Microsoft Corporation. All rights reserved.


    '---
    Special Sort Excel add-in (20+ new ways to sort)
    (also other workbooks and add-ins
    Download from MediaFire...
    http://www.mediafire.com/folder/lto3hbhyq0hcf/Documents


    Saturday, July 20, 2019 1:05 PM