Results 1 to 13 of 13

Thread: [RESOLVED] Adding code to workbook through macro

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Location
    Bloomingdale, IL USA
    Posts
    284

    Resolved [RESOLVED] Adding code to workbook through macro

    I'm writing a macro that takes a text file containing code and copies it behind sheet1 of an existing workbook. It works fine on my computer but every time I run it on someone else's computer, Excel gives a fatal error on the following line of code it is importing.... Not sure why.

    Dim rsData as ADODB.Recordset

    Below is my vba code and I also attached the text file.

    Add Code Code:
    1. Sub UpdateThisWorkbook()
    2. On Error GoTo DownHere
    3.  
    4. Dim VBProj As VBIDE.VBProject
    5. Dim VBComp As VBIDE.VBComponent
    6. Dim CodeMod As VBIDE.CodeModule
    7. Dim LineNum As Long
    8.  
    9. Dim oFS
    10. Dim FsFile
    11.  
    12. Set FsFile = CreateObject("Scripting.FileSystemObject")
    13. Set oFS = FsFile.OpenTextFile(ThisWorkbook.Path + "\copy sheet1 code.txt")
    14.  
    15. Set VBProj = ActiveWorkbook.VBProject
    16. Set VBComp = VBProj.VBComponents("sheet1")
    17. Set CodeMod = VBComp.CodeModule
    18.  
    19.  
    20. 'This adds the ADO references.
    21. VBProj.References.AddFromFile "C:\Program Files\Common Files\system\ado\msado15.dll"
    22. VBProj.References.AddFromFile "C:\Program Files\Common Files\system\ado\msador15.dll"
    23.  
    24.  
    25. CodeMod.DeleteLines 1, CodeMod.CountOfLines
    26.        
    27. With CodeMod
    28.  
    29.     Do Until oFS.AtEndOfStream
    30.         LineNum = LineNum + 1
    31.         stext = oFS.ReadLine
    32.        
    33.         .InsertLines LineNum, stext
    34.     Loop
    35.  
    36. End With
    37.  
    38. Exit Sub
    39.  
    40. DownHere:
    41.     MsgBox Err.Description, vbOKOnly, Format(Err.Number)
    42.     Resume Next
    43.  
    44. End Sub
    Any help is appreciated.

    Thanks!
    Chrissy
    Attached Files Attached Files
    Last edited by Chrissy; Sep 24th, 2010 at 12:52 PM.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Adding code to workbook through macro

    they have no reference to ADO, when the project loads, so get error on that line change to
    Dim rsData as object
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Location
    Bloomingdale, IL USA
    Posts
    284

    Re: Adding code to workbook through macro

    The references are being added...

    Reference Code:
    1. VBProj.References.AddFromFile "C:\Program Files\Common Files\system\ado\msado15.dll"
    2. VBProj.References.AddFromFile "C:\Program Files\Common Files\system\ado\msador15.dll"

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Adding code to workbook through macro

    i saw that, but they are not there when the project starts
    also need to make sure the specific version is available on the target machine
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Re: Adding code to workbook through macro

    Try to put a break on the first line of your code then do the F8 "debug". That way you can see what line it crashes.

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Adding code to workbook through macro

    i got too many type mismatch errors, running the code to insert references
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Adding code to workbook through macro

    Chrissy, show me your excel file...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Location
    Bloomingdale, IL USA
    Posts
    284

    Re: Adding code to workbook through macro

    Here is the workbook and the two files containing the code. The macro also manipulates the files too.

    Maybe if i try adding the references and then saving the file before adding the code???

    It's just strange that it works fine on my computer but not anyone else's.
    Last edited by Chrissy; Sep 27th, 2010 at 09:44 AM.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Location
    Bloomingdale, IL USA
    Posts
    284

    Re: Adding code to workbook through macro

    asm... I already stepped through debugging... that is how i know it give a fatal error when importing that one line.

  10. #10
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Adding code to workbook through macro

    It works fine on my computer but every time I run it on someone else's computer, Excel gives a fatal error on the following line of code it is importing.... Not sure why.
    Sorry to add to your troubles but Application.Filesearch is not supported in Excel 2007...

    I will test your file when I get back home where I have access to Excel 2003...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Location
    Bloomingdale, IL USA
    Posts
    284

    Re: Adding code to workbook through macro

    Actually I got it to work now... If I add the references, save the file, then add the macro code it works fine without an error.

    Thanks for the help.

  12. #12
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: [RESOLVED] Adding code to workbook through macro

    But what about application.filesearch?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Location
    Bloomingdale, IL USA
    Posts
    284

    Re: [RESOLVED] Adding code to workbook through macro

    We don't have excel 2007 so it works fine for us right now.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width