+ Reply to Thread
Results 1 to 6 of 6

Macro to include a file as object

  1. #1
    Registered User
    Join Date
    11-25-2006
    Posts
    1

    Macro to include a file as object

    Hello,
    I want to create a macro to prompt user (browse) for a file in the computer. When the file is selected, this macro include it in excel, embedded as a complete file (not just linked).
    The same when we select the menu Insert => Object => Create from file => Browse.
    Thank you for any help!

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,602
    if you turn on the macro recorder and go through the steps, you get this
    PHP Code: 
    Sub Macro1()
    ActiveSheet.OLEObjects.Add(Filename:="C:\help.txt"Link:=False _
            
    DisplayAsIcon:=False).Select
    End Sub 
    So, all you need is the input box to get the file name (which should include the drive and path)
    PHP Code: 
    Sub Macro1()
         
    'declare a variable to hold the file name:'
         
    Dim FName As String
         
         
    'load the variable via an input box:'
         
    FName InputBox("Enter a file name with complete path")
         
         
    'get the file:
         ActiveSheet.OLEObjects.Add(Filename:=FName, _
         Link:=False, _
         DisplayAsIcon:=False).Select
    End Sub 
    Ben Van Johnson

  3. #3
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    Instead of using an input box I recommend using

    FName$ = Application.GetOpenFilename

    This allows the user to browse the various drives & folders and then select a file without having to manually type in the full path

  4. #4
    Registered User
    Join Date
    09-26-2012
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Macro to include a file as object

    Hi,

    In the same macro (given above) if we want to auto fit the attachment in the cell and the cell will be to the left of the macro button. How can we do this?

    Looking forward to some replies.

    Thanks,
    Vik

  5. #5
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,602

    Re: Macro to include a file as object

    @vikash200418,
    Unfortunately your post does not comply with Rule 2 of our Forum RULES. Do not post a question in the thread of another member -- start your own thread.

    If you feel an existing thread is particularly relevant to your need, provide a link to the other thread in your new thread.

    Old threads are often only monitored by the original participants. New threads not only open you up to all possible participants again, they typically get faster response, too.

  6. #6
    Registered User
    Join Date
    09-26-2012
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Macro to include a file as object

    Okay ProtonLeah, as I am new was not aware of the rules. I have posted a new thread

    http://www.excelforum.com/excel-prog...ml#post4165314

    If possible can you provide a solution to it

    Thanks in Advance

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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