Consulting

Results 1 to 5 of 5

Thread: Solved: How do you name an imported module via code?

  1. #1
    VBAX Regular
    Joined
    Jun 2006
    Posts
    26
    Location

    Solved: How do you name an imported module via code?

    My VBA project imports in *.bas files into a workbook and I need a way to check to see that functions with the same name don't already exist. I plan to name the modules when they are imported (preferably to the same file name), and just search for the same module name. I will then give the user the choice to replace the file.

    I have not yet found out how to name an imported module. It always takes the next in the sequence Module1, Module2..... ModuleX.

    I have figured out this code to search for all components, including modules. I don't know how the module1 is selected without knowing what the name is?

    Therefore, I don't know how to name the module, without already knowing its current name. (how do you name it during creation?).

    Is there a way to "pick-off" the modules from the worksheets, class modules, etc?

    Thanks!

    [vba]
    Private Function listComponents()
    Dim ref As Object
    Dim msg As String
    msg = ""
    For Each ref In ActiveWorkbook.VBProject.VBComponents
    msg = msg & ref.Name & vbCrLf
    If (ref.Name = "Module1") Then
    'Renames module, once selected.
    ref.Name = "Module2"
    End If
    Next ref
    MsgBox msg
    'deletes module that you know the name
    With ActiveWorkbook.VBProject.VBComponents
    .Remove ActiveWorkbook.VBProject.VBComponents("why")
    End With
    End Function[/vba]

    While on the same subject, is there a way to isolate all of the functions listed on a given module? Can I do a similar listFunction sub to list all of the subs and functions on a given module?
    Last edited by nitt1995; 06-19-2006 at 01:05 PM.

  2. #2
    Administrator
    2nd VP-Knowledge Base
    VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Quote Originally Posted by nitt1995
    Therefore, I don't know how to name the module, without already knowing its current name. (how do you name it during creation?).
    If you import it during runtime you could just set it to an object and rename it after setting it to the object.

    [vba]Option Explicit

    Sub Test()
    On Error GoTo This

    Dim objModule As Object
    Set objModule = ThisWorkbook.VBProject.VBComponents.Import("C:\Sort_Sheets.bas")

    objModule.Name = "ThisName"

    Set objModule = Nothing
    Exit Sub

    This:
    Debug.Print Err.Number
    Set objModule = Nothing
    End Sub[/vba]

    The reason for the error checking is because if you try to rename it to "ThisName" again, you get an application-defined or object-defined error.




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  3. #3
    VBAX Regular
    Joined
    Jun 2006
    Posts
    26
    Location
    Thanks! It works great.

  4. #4
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Please be sure to mark your thread solved.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  5. #5
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Glad to help. And yes, please mark the thread solved if it is so. Thanks.




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

Posting Permissions

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