+ Reply to Thread
Results 1 to 6 of 6

Specify OLEObject Icon & File Names

  1. #1
    Registered User
    Join Date
    03-20-2018
    Location
    Bath, England
    MS-Off Ver
    Microsoft Office Professional Plus 2013
    Posts
    11

    Specify OLEObject Icon & File Names

    Hello all,

    I have the following code:

    Private Sub CommandButton1_Click()

    Selection.GoTo What:=wdGoToLine, Which:=wdGoToNext, Count:=2
    Call InsertWordObject

    End Sub

    Sub InsertWordObject()

    Dim FoundFile As Variant
    Dim vName As Variant
    With Application.FileDialog(FileDialogType:=msoFileDialogFilePicker)
    If .Show = -1 Then
    For Each FoundFile In .SelectedItems
    vName = FoundFile
    On Error GoTo Alternate
    Selection.InlineShapes.AddOLEObject FileName:=vName, LinkToFile:=True, _
    DisplayAsIcon:=True, IconFileName:=vName, IconIndex:=(0), IconLabel:="Document 1"
    GoTo NextShape
    Alternate:
    Selection.InlineShapes.AddOLEObject ClassType:="Package", FileName:=vName, _
    LinkToFile:=True, DisplayAsIcon:=True, IconFileName:=vName, IconIndex:=(0), IconLabel:="Document 1"
    NextShape:
    Next
    End If
    End With


    End Sub

    When manually creating an attached OLEObject, word automatically selects the relevant icon for each program, and uses the file name.
    I am struggling to figure out how to create the same outcome with VBA.

    Any help would be greatly appreciated.

    Thank you,

    Jack.

  2. #2
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Specify OLEObject Icon & File Names

    The icon is chosen via the IconIndex variable; you code has it set to 0, which is the default for whatever file type you're inserting. Achieving the same default result as you get when working through the GUI, all you need is:
    Selection.InlineShapes.AddOLEObject FileName:=vName, LinkToFile:=True, DisplayAsIcon:=True
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Registered User
    Join Date
    03-20-2018
    Location
    Bath, England
    MS-Off Ver
    Microsoft Office Professional Plus 2013
    Posts
    11

    Re: Specify OLEObject Icon & File Names

    Thank you for the reply Paul.

    I have amended the code as suggested, however the file will still display like so:

    File attach.PNG

    My code is now:

    Sub InsertWordObject()

    Dim FoundFile As Variant
    Dim vName As Variant
    With Application.FileDialog(FileDialogType:=msoFileDialogFilePicker)
    If .Show = -1 Then
    For Each FoundFile In .SelectedItems
    vName = FoundFile
    On Error GoTo Alternate
    Selection.InlineShapes.AddOLEObject FileName:=vName, LinkToFile:=False, DisplayAsIcon:=True
    GoTo NextShape
    Alternate:
    Selection.InlineShapes.AddOLEObject ClassType:="Package", FileName:=vName, LinkToFile:=False, DisplayAsIcon:=True
    NextShape:
    Next
    End If
    End With


    End Sub


    Please let me know if I've misunderstood, I'm still relatively new to VBA.

    Thanks again,

    Jack.

  4. #4
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Specify OLEObject Icon & File Names

    Try something along the lines of:
    Please Login or Register  to view this content.
    You may need to add more cases (e.g. for Access, PowerPoint, etc.). The macro recorder will give you the correct variables for strClass & strIco. The recorded value for strClass may include a period followed by a number at the end; you can omit those to give the code more flexibility with different application versions.

    PS: When posting code, please use the code tags, indicated by the # button on the posting menu. Without them, your code loses much of whatever structure it had.

  5. #5
    Registered User
    Join Date
    03-20-2018
    Location
    Bath, England
    MS-Off Ver
    Microsoft Office Professional Plus 2013
    Posts
    11

    Re: Specify OLEObject Icon & File Names

    Thank you, the code works really well for the file name. However, the file image still returns as blank.
    If I use:

    Please Login or Register  to view this content.
    Which are the locations returned from when I record the macro, it works perfectly.

    Is there a way to set the VBA code to work on any machine?

    Thanks for the help,

    Jack.

  6. #6
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Specify OLEObject Icon & File Names

    Yes, it's possible, but doing so seems to be quite complicated, since you have to create functions that will retrieve the relevant data from the Windows registry. See, for example: https://www.developerfusion.com/arti...-default-icon/

+ 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. Unable to get Icon file when embedding a file in Excel
    By wilster98 in forum Excel General
    Replies: 1
    Last Post: 12-25-2017, 08:26 PM
  2. Insert file as OLEobject by referencing a cell
    By dklein11 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-31-2015, 10:36 PM
  3. Printing single sheet to .pdf then embedding the file. (renaming OLEObject?)
    By Canuckle777 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-14-2013, 12:56 PM
  4. Unable to add PDF file as OLEObject
    By eduque in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-14-2013, 09:44 PM
  5. Change File Icon and/or Title Bar Icon
    By Casedias in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 09-17-2011, 07:35 PM
  6. [SOLVED] xcel file icon opens application but not file.
    By RayG in forum Excel General
    Replies: 1
    Last Post: 06-07-2006, 02:25 PM
  7. [SOLVED] Each file has own icon
    By Craig in forum Excel General
    Replies: 2
    Last Post: 04-22-2006, 03:50 PM

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