Results 1 to 13 of 13

Thread: [RESOLVED] Adding code to workbook through macro

Threaded View

  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.

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