[RESOLVED] import a "*.bas" file into vb project
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
Re: import a "*.bas" file into vb project
I started to import the .bas file to my resources.
the problem is now how do I declare it as an object I guess?
Code:
Dim myfile As String
myfile = My.Resources.test.bas
Re: import a "*.bas" file into vb project
I'd say that you can't, but I'd be interested in seeing whether I'm wrong.
It sounds like you want to import that file and use it as valid code. That would be like a dll. If it was a dll, that would be easy. Since it isn't a dll, I would guess that it is either impossible or really difficult. I believe that a .bas file contains uncompiled source code, so it would appear to be nothing more than a text file. You could run that through a compiler to create executable code, except that the .bas file probably doesn't contain .NET code, so the compiler would have to be specific to the language in the .bas file.
Re: import a "*.bas" file into vb project
Quote:
Originally Posted by
Shaggy Hiker
I'd say that you can't, but I'd be interested in seeing whether I'm wrong.
It sounds like you want to import that file and use it as valid code. That would be like a dll. If it was a dll, that would be easy. Since it isn't a dll, I would guess that it is either impossible or really difficult. I believe that a .bas file contains uncompiled source code, so it would appear to be nothing more than a text file. You could run that through a compiler to create executable code, except that the .bas file probably doesn't contain .NET code, so the compiler would have to be specific to the language in the .bas file.
I think .bas files were used in VB6, and they are certainly implemented in VBA, so correct in that they are uncompiled. I think what OP is trying to do is to import the .bas as a project resource in VB.NET then attach it into his XLS sheet and run the macro. So the .bas file code is never being run in VB.NET, its being executed through Excel Interop in the VBA engine. All VB.NET is doing is executing the process to obtain the .bas file from resources, add it to the project, then execute.
Normally, I would think we would look at it, and say "why not just do it all from .NET", but that is the requirement he has created for himself.
So OP, I am assuming your coding works fine locally and you want to extend the .bas file to be found and work on other systems than your own. Have you tried adding this to project resources then calling it when you need it?
Re: [RESOLVED] import a "*.bas" file into vb project
I solved it..
Code:
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
Dim workingmodule As String
Const vbext_ct_StdModule = 1
excelApp.Visible = True
workingmodule = excelApp.VBE.ActiveVBProject.VBComponents.Add(vbext_ct_StdModule).Name
excelApp.VBE.ActiveVBProject.VBComponents.Item("Module1").CodeModule.AddFromString(My.Resources.Export_Blue_Cells)
excelApp.Run("Run_One")
excelApp.VBE.ActiveVBProject.VBComponents.Remove(excelApp.VBE.ActiveVBProject.VBComponents.Item(workingmodule))
' 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