+ Reply to Thread
Results 1 to 14 of 14

Macro to insert object script error help

  1. #1
    Registered User
    Join Date
    09-02-2012
    Location
    Bulgaria
    MS-Off Ver
    Excel 2010
    Posts
    19

    Macro to insert object script error help

    Hello,

    i'm using this code for insert an object but when you click cancel instead of insert it will give Run time error 1004 cannot insert an object.

    Sub Macro1()
    Dim FName As String
    FName$ = Application.GetOpenFilename
    ActiveSheet.OLEObjects.Add(Filename:=FName, _
    Link:=False, _
    DisplayAsIcon:=False).Select
    End Sub

    and how can i specify in which cell and sheet name i want to be added not at the active one ?
    also because this macro is assigned to a command button is it possible when the file has been attached the text of the command button to change to "ok"


    Thanks for the help in advance.
    Last edited by archangel9999; 08-28-2014 at 08:39 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Macro to insert object script error help

    if fname<>"" then
    ActiveSheet.OLEObjects.Add(Filename:=FName, _
    Link:=False, _
    DisplayAsIcon:=False).Select

    end if
    Hope this helps

    Sometimes its best to start at the beginning and learn VBA & Excel.

    Please dont ask me to do your work for you, I learnt from Reading books, Recording, F1 and Google and like having all of this knowledge in my head for the next time i wish to do it, or wish to tweak it.
    Available for remote consultancy work PM me

  3. #3
    Registered User
    Join Date
    09-02-2012
    Location
    Bulgaria
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: Macro to insert object script error help

    Hello,

    thank you very much. Maybe i'm making something bad because i cannot make it working.


    Thank you

  4. #4
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Macro to insert object script error help

    Sub Macro1()
    Dim FName As String
    FName$ = Application.GetOpenFilename
    If Not FName = "False" Then
    ActiveSheet.OLEObjects.Add(Filename:=FName, _
    Link:=False, _
    DisplayAsIcon:=False).Select
    End If
    End Sub

    When the error comes, if you hover over your variables you can see the contents, this gave the fix, its not a null string, its "False"

  5. #5
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Macro to insert object script error help

    Sub Macro1()
    Dim FName As String
    FName$ = Application.GetOpenFilename
    If Not FName = "False" Then
    ActiveSheet.OLEObjects.Add(Filename:=FName, _
    Link:=False, _
    DisplayAsIcon:=False).Select
    End If
    End Sub

    When the error comes, if you hover over your variables you can see the contents, this gave the fix, its not a null string, its "False"

  6. #6
    Registered User
    Join Date
    09-02-2012
    Location
    Bulgaria
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: Macro to insert object script error help

    Quote Originally Posted by nathansav View Post
    Sub Macro1()
    Dim FName As String
    FName$ = Application.GetOpenFilename
    If Not FName = "False" Then
    ActiveSheet.OLEObjects.Add(Filename:=FName, _
    Link:=False, _
    DisplayAsIcon:=False).Select
    End If
    End Sub

    When the error comes, if you hover over your variables you can see the contents, this gave the fix, its not a null string, its "False"
    Thank you very much it is working perfectly, but i still need a little more help with the following.

    and how can i specify in which cell and sheet name i want to be added the objects and not at the active one ?
    also because this macro is assigned to a command button is it possible when the file has been attached successfully the text of the command button to change to "ok"

  7. #7
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Macro to insert object script error help

    change activesheet, to a sheet reference, come on, these are the basics

  8. #8
    Registered User
    Join Date
    09-02-2012
    Location
    Bulgaria
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: Macro to insert object script error help

    Quote Originally Posted by nathansav View Post
    change activesheet, to a sheet reference, come on, these are the basics
    Hello Thank you very much for your help already !!!

    i already had done that with the sheet before your reply but i still cannot make working the following

    how to specify to which cell and row i want to add the object based on a specific value
    for example
    if a1 = 1 then insert the object in cell b1
    if a2 = 2 then insert the object in cell c1
    and so one.

    If you can help me with that i will be grateful.

    Thank you very much in advance.

  9. #9
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Macro to insert object script error help

    I dont think you can put it in a cell, i think you just need to try

    range("b1").select
    then your OLE adding code, read the help in Excel, no mention of ranges only sheets.

  10. #10
    Registered User
    Join Date
    09-02-2012
    Location
    Bulgaria
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: Macro to insert object script error help

    Hello, i'm using this code now but i cannot understand why i cannot add Wigth:=10, Height:=5 correctly always is given some error. If i add "if" to the script like A1=1 A1=2 and so one, then set Left:=50 +5, Top:=50 +5 Wigth:=10 +5, Height:=15 +5 and so on in a loop. Can someone help me with that script ??
    Please Login or Register  to view this content.
    Thank you very much in advance.

  11. #11
    Registered User
    Join Date
    09-02-2012
    Location
    Bulgaria
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: Macro to insert object script error help

    Hello,

    can someone help me with that script problem please ?

    Thank you very much in advance.

  12. #12
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Macro to insert object script error help

    Just run it, and its fine, do you have a sheet called 1?

  13. #13
    Registered User
    Join Date
    09-02-2012
    Location
    Bulgaria
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: Macro to insert object script error help

    Hello nathansav,

    yes i add all the details and the script is working but when i try to add Wigth, and Height to this script and it is giving me error no matter i try. And is it possible to add If statement into the script like if A1 = 1 then for example set Left, Top,Wigth, Height parameters + 50 for example, if A2 = 2 then set Left, Top,Wigth, Height parameters + 100 and so on on a loop ?

    Thanks for your help in advance.

  14. #14
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Macro to insert object script error help

    Can you answer the question though? Your code is looking for a sheet called 1, the MS help file you looked at for this, is using a sheet index of 1, i.e. sheets(1) you're using sheets("1") so its looking for a sheet called 1, is that present?

  15. #15
    Registered User
    Join Date
    09-02-2012
    Location
    Bulgaria
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: Macro to insert object script error help

    yes to your question.

+ 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. Excel ActiveX ControlBox Cannot Insert Object error
    By exceltim in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-27-2014, 09:53 AM
  2. Replies: 0
    Last Post: 06-19-2014, 02:09 PM
  3. "Method 'Range' of object '_Worksheet' failed" error in Windows Script Host
    By jikenj in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-16-2010, 02:18 PM
  4. Date Time Picker Control - Cannot Insert Object error
    By Jose77 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-26-2009, 03:12 AM
  5. Replies: 5
    Last Post: 06-08-2005, 11:05 AM

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