Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Load a .bas file into a project

Status
Not open for further replies.

Xsi

Programmer
May 29, 2015
121
SE
Hello VB Members,

I posted in an old thread that was about "how to import a file to a vb project"

I think I was not clear enough to make people understand.

The thing is...

I have made a code that loops through "*.xls" files.
For each xls file the loop imports a "*.bas" file located here "C:\test\test.bas"

The function runs "RUNME" inside the "*.bas"

The actual problem

Instead of an absolute path to the "*.bas" file I want to import the "*.bas" file into my vb project.

The "*.bas" file should be imported to the vb project.

I want to import the "*.bas" file to the vb project, regardless of computer.

see code below:

Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim myfile = "C:\test\test.bas"

        FolderBrowserDialog1.SelectedPath = My.Computer.FileSystem.SpecialDirectories.MyDocuments
        FolderBrowserDialog1.ShowNewFolderButton = True 'folderdialog button
        If FolderBrowserDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim mySelFile As String = FolderBrowserDialog1.SelectedPath
            Dim intcount As Integer = Nothing
            Tb_FilePath.Text = mySelFile
            If Not IO.Directory.GetFiles(mySelFile, "*.xls").Any() Then
                MsgBox("there are no xls files here")
                Application.Restart()
            End If
            For Each filename As String In IO.Directory.GetFiles(mySelFile, "*.xls") 'I define the type of all files the loop go through


                excelApp.Workbooks.Open(filename)
                excelApp.Visible = True
                excelApp.VBE.ActiveVBProject.VBComponents.Import(myfile)
                excelApp.Run("Run_one")


                ' Create a new instance of Excel and make it visible.
                Next
            '     MsgBox("Modified (" + intcount.ToString + ") Excel files")
        Else

            'if the user has not selected a folder, it is a warning
            MsgBox("No Folder selected", MsgBoxStyle.Exclamation, "No selected folders")
        End If
End sub

I have imported the file as an resource test.bas


I have tested with this code but I guess I need to declare it but I don't know how:

see codesnippet:

Code:
       Dim myfile As String
        myfile = My.Resources.test.bas


Could someone help me?


Thank you in advance.
 
Are you saying that you have the filename as a string resource? Or the actual contents of the bas file as a string resource?
 
Well I guess my code could be very wrong I am newbeginner..


Code:
Dim myfile As String
myfile = My.Resources.test.bas



maybe something with this?
Code:
        Dim myfile As String
        myfile = My.Resources.ResourceManager.GetObject("test.bas")

the problem is I want just use call the "test.bas" file so excel import the resource file test.bas file.
 
You need to answer the question first. Is the resource the contents of the file, or is the resource the filename? If the former, then the following code snippet illustrates what you need to do. Note that it is illustrative only (e.g. it creates a new workbook, and a temporary new module):

Code:
[blue]    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim excelApp As Microsoft.Office.Interop.Excel.Application
        Dim workingmodule As String
        Const vbext_ct_StdModule = 1

        excelApp = New Microsoft.Office.Interop.Excel.Application

        excelApp.Workbooks.Add()
        workingmodule = excelApp.VBE.ActiveVBProject.VBComponents.Add(vbext_ct_StdModule).Name
        excelApp.VBE.ActiveVBProject.VBComponents.Item("Module1").CodeModule.AddFromString(My.Resources.RunMe)
        excelApp.Run("RunMe")
        excelApp.VBE.ActiveVBProject.VBComponents.Remove(excelApp.VBE.ActiveVBProject.VBComponents.Item(workingmodule))
    End Sub[/blue]

 
I dont understand your question

ewF6ZsL.png


inside the bas file is vba code. (Excel macro)
 
that is not the answer I am looking for :)
 
>I dont understand your question.

Fortunately your screenshot answers the question - the resource is the contents of the file, so my example code (which made that assumption) would work. Depending on the contents of that file, of course. And one assumes that you realise that for my example I happened to use a resource that I called [tt]RunMe[/tt], whilst yours is called [tt]Test[/tt], so you'd need to change:

[tt][blue]excelApp.VBE.ActiveVBProject.VBComponents.Item("Module1").CodeModule.AddFromString(My.Resources.RunMe)[/blue][/tt]

to

[tt][blue]excelApp.VBE.ActiveVBProject.VBComponents.Item("Module1").CodeModule.AddFromString(My.Resources.Test)[/blue][/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top