VBE

LAS

Board Regular
Joined
Oct 29, 2002
Messages
215
Does anyone have code to import code sitting in a text file into a module?

Thanks

Lawrence
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
From the Visual Basic Editor (Alt-F11), click on VBA Project (file) you are working on, right click, and select Import. Then simply choose the file.

Alternatively, you could select Insert Module from right click, then just cut and paste the code.
 
Upvote 0
there may be a better way of doing this but here goes:

you could duplicate the manual keystrokes to import a file, via code:

Code:
Sub MyImport()
Application.ScreenUpdating = False
MyFile = InputBox("enter full path and filename of file you wish to import into VBE")
SendKeys "%{F11}"
SendKeys "^m"
SendKeys MyFile
SendKeys "~"
Application.ScreenUpdating = True
End Sub

like i said, there may be a better way of doing this, but this does take away the need of opening VBE and doing it manually - the only thing you need to type in this code is the path and filename of the file you want to import

hth
kevin
 
Upvote 0
Howdy y'all, might want to have a look at:

http://www.cpearson.com/excel/vbe.htm

The section on copying modules between projects. You won't want the export, but the import could be handy.

Another way to pluck a duck. E.g.,<pre>
Sub ImportModule()
Dim FName As String
FName = "c:tempcode.txt"
ThisWorkbook.VBProject.VBComponents.Import FName
End Sub</pre>

_________________
Cheers,<font size=+2><font color="red"> Nate<font color="blue"> Oliver</font></font></font>
This message was edited by NateO on 2002-12-17 17:45
 
Upvote 0
It could probably be streamlined a bit, but this is pretty close...


Sub Load_VBA_Text()

Application.VBE.VBProjects("MyProject").VBComponents("Module1").Activate
Application.VBE.ActiveCodePane.Show

import_textfile = "C:vba_code.txt"

Open import_textfile For Input As #1

While Not EOF(1)
n = n + 1
Line Input #1, vbline
Application.VBE.ActiveCodePane.CodeModule.InsertLines n, vbline
Wend

Close #1

End Sub
 
Upvote 0
You could also use: -

Code:
Sub Load_VBA_Text()
Application.VBE.ActiveVBProject.VBComponents("Module1").CodeModule.AddFromFile "C:YourTextFile.txt"
End Sub
 
Upvote 0
On 2002-12-18 06:08, dk wrote:
You could also use: -

Code:
Sub Load_VBA_Text()
Application.VBE.ActiveVBProject.VBComponents("Module1").CodeModule.AddFromFile "C:YourTextFile.txt"
End Sub

Yes, that's much better. Although, it seems to give me trouble with textfiles saved with notepad. Anyone else run into this?

The only way I could get it to pull in more than one line was with the OPEN file technique.

Maybe it's just my version of notepad or something.
This message was edited by Aaron Blood on 2002-12-18 10:01
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,606
Members
449,089
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top