User-defined type not defined vb6 - vb6

I am attempting to 'save' the context of a text box in vb6 to a *.ini file, so that it can be used in a later part of the program. (i.e. the user would enter something into the text box, then later in the program, a label would appear with the user-entered, saved information).
I used the following code which I copied from the source of someone else's program, however it hasn't worked:
Dim fsys As New FileSystemObject
Dim outstream As TextStream
Dim write1 As String
Dim val1 As String
val1 = Text1.Text
inisettings = App.Path & "\Variables.ini"
Set outstream = fsys.OpenTextFile(inisettings, ForWriting, False, TristateFalse)
outstream.WriteLine (val1)
Set outstream = Nothing
This is the result:
Does anyone have any way to save data for later?

FileSystemObject lives inside an external library, to use it click Project then References and tick Microsoft Scripting Runtime.
You don't actually need to do any of that, the code below uses VB's built-in functionality to write a file.
Dim hF As Integer
hF = FreeFile()
Open App.Path & "\Variables.ini" For Output As #hF
Print #hF, val1
Close #hF

You must declare TristateFalse and give it a value like 0, 1 or 2.
You can take a look at this link: https://msdn.microsoft.com/en-us/subscriptions/bxw6edd3(v=vs.84).aspx

The reason why you are getting this error is because you don't have a reference to the Microsoft Scripting Runtime library. Follow the below instructions while in your VB6 project:
From the top menu, click on Project > References.
From the list, check the item entitled "Microsoft Scripting Runtime".
Click OK.
This will resolve your immediate error however your code still has some other issues. First off, you forgot to declare the variable inisettings. I am going to assume that you will want to always overwrite the entire file each time you update the INI file so you want to use the method CreateTextFile instead of OpenTextFile.
Dim fsys As New FileSystemObject
Dim outstream As TextStream
Dim write1 As String
Dim val1 As String
Dim inisettings As String
val1 = Text1.Text
inisettings = App.Path & "\Variables.ini"
Set outstream = fsys.CreateTextFile(inisettings, True, False)
Call outstream.WriteLine(val1)
Set outstream = Nothing
Good luck!

Related

How to create exe file using VBS

How to make executable file with VBS. There is a code for txt but how to change it to make from this exe
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const Append = 8
Dim FDir
FDir = ("Area where file will be saved")
Const FName_Ext = "Title of document.txt"
Dim Final
Final = FDir + FName_Ext
Dim objtxt
set objtxt = objFSO.CreateTextFile(Final, True)
Set objtxt = Nothing
Dim FWrite
Set FWrite = objFSO.OpenTextFile(Final, Append)
FWrite.WriteLine("this is the text in the file.!!! hahaha lol wohoo yada yada yada. okay done!")
FWrite.Close()
Set FWrite = Nothing
Set objFSO = Nothing
Get familiar with PE Headers . Short, this is a lot of binary data that tells windows where to find what when it starts to execute your file. Although vbs was not designed for binary magic, the simple text way does the trick if you have saved your binary data into a variable, at least according to this microsoft doc.

Is Run command in Windows executable file?

Suddenly i got one thing in my mind that most of the win applications like windwos version(winver), calc and all are executable files which will be there in Windows or System32 folder.
Like that, Run Command which we are using Win+R shortcut are also executable file? is this available in anywhere in windows system folder as executable file ?
When i tried to click Open file location, it is opening Desktop. actually Where it is starting from when we click on shortcut ?
No, it's not an exe, it's a shell dialog window that you find in the dynamic link library C:\Windows\System32\shell32.dll
You can call it from VBScript like this:
dim objShell
set objShell = CreateObject("shell.application")
objShell.FileRun
From JScript like this:
var objShell = new ActiveXObject("shell.application");
objShell.FileRun();
From VB6 like this:
Private Sub fnShellFileRunVB()
Dim objShell As Shell
Set objShell = New Shell
objShell.FileRun
Set objShell = Nothing
End Sub
With modern VB.NET, this becomes:
Dim t2 As Type = Type.GetTypeFromProgID("Shell.Application")
Dim obj2 = Activator.CreateInstance(t2) ' dynamic
obj2.FileRun()
If option strict is "ON", then the way to go is this:
Dim t As Type = Type.GetTypeFromProgID("Shell.Application")
Dim obj As Object = Activator.CreateInstance(t)
t.InvokeMember("FileRun", System.Reflection.BindingFlags.InvokeMethod, Nothing, obj, Nothing)
C# Variant:
Type t = Type.GetTypeFromProgID("Shell.Application");
object obj = Activator.CreateInstance(t);
t.InvokeMember("FileRun", System.Reflection.BindingFlags.InvokeMethod, null, obj, null);
//If the C # 4.0, the Dynamic Lookup presence of, it can be:
Type t2 = Type.GetTypeFromProgID("Shell.Application");
dynamic obj2 = Activator.CreateInstance(t2);
obj2.FileRun();
But you can also call it from a batch-file, if you want to:
C:\WINDOWS\system32\rundll32.exe shell32.dll,#61
or via the Explorer command-line:
explorer.exe Shell:::{2559a1f3-21d7-11d4-bdaf-00c04f60b9f0}

checking format to a text box

I need a method to check the contents of the text entered to make sure they are correctly entering a folder path. So it needs to be in the format of:
Drive Letter :\ Folder
e.g. C:\My Documents
If they haven't typed in that format I need to stop and show a message telling them to double check.
I have tried the Filter function but I haven't quite got it to work. Any help would be awesome. I don't have any code to show because I am nto sure where to start.
I also tried the common dialog, but the user jsut needs the type the path, not select the file. All I want to check is if the text type is within that format DRIVE:\FOLDER, that is it. So if the type "BLAH" in the text bax a message says Hey you type a correct path.
In VB6, to test whether your text contains a valid folder:
If Len(Dir("c:\My Documents", vbDirectory))>0 Then
'it's a folder
End If
Have you thought of implemeting the common dialog control to allow the selection of a correct folder instead - it'll be much more likely to be accurate.
Some example code of folder browsing from here:
Private Sub Command1_Click()
On Error Resume Next
Const WINDOW_HANDLE = 0
Const NO_OPTIONS = 0
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder(WINDOW_HANDLE, "select folder:", NO_OPTIONS, "C:Scripts")
Set objFolderItem = objFolder.Self
objPath = objFolderItem.Path
objPath = Replace(objPath, "", "\")
Print objPath
End Sub
Alternatively you could validate the folder first you could check for ":\" using eith instr or mid
then you could validate the folder and even include an option to create it if not present with the filesystemobject (needs a reference set) here it is in function form, you can pass the contents of the textbox for validation.
Function DirExists(pFile As String, Optional pCreate As Boolean = False)
'
Dim fso As New FileSystemObject
Dim vPath As Variant
Dim sPath As String
Dim y As Variant
DirExists = False
If fso.FolderExists(pFile) Then
DirExists = True
Else
If pCreate Then
vPath = Split(pFile, "\")
For Each y In vPath
sPath = sPath & y & "\"
If Not fso.FolderExists(sPath) Then
fso.CreateFolder (sPath)
If fso.FolderExists(pFile) Then
DirExists = True
Exit Function
End If
End If
Next
End If
End If
End Function

Get cursor location when running Macro in visual Studio

I've got a Macro that I run that writes a copyright header to my document. Currently when the header is written, the cursor is left at the end of the header.
What I'd like to be able to do is capture the current location, write the header, and then return the cursor to the original location.
Does anyone know how this can be accomplished?
I think I've got it.
Dim selection As TextSelection = DTE.ActiveDocument.Selection
''# store the original selection and cursor position
Dim topPoint As TextPoint = selection.TopPoint
Dim bottomPoint As TextPoint = selection.BottomPoint
Dim lTopLine As Long = topPoint.Line
Dim lTopColumn As Long = topPoint.LineCharOffset
Dim lBottomLine As Long = bottomPoint.Line
Dim lBottomColumn As Long = bottomPoint.LineCharOffset()
Dim verticalOffset As Integer = 0
''# do a bunch of stuff that adds text to the page
''# Restore cursor to previous position
selection.MoveToLineAndOffset(lBottomLine + verticalOffset, lBottomColumn)
selection.MoveToLineAndOffset(lTopLine + verticalOffset, lTopColumn, True)
This is all nested within a Macro I wrote to automatically add a copyright header to each code file.

How do I save an entire VB6 project to a new folder? Modules and all

How do I save an entire VB6 project to a new folder? Modules and all. I'm in a position where I need to work with some old VB6 projects. I'd like to save them to a new folder but when I save the project, all that is saved is the vbp file. No modules, no frm files. I want to be able to save all the info to a single folder without moving each BAS file one at a time. Is this even possible?
Addition: The first 2 replies make good sense. But my problem is that the BAS modules seem to be scattered all over the place. Making Windows Explorer do the work a bit tricky. If I have to I will but was looking for an easier way.
Thanks
Given the new "addition" to the question:
Move the VBP and the files in Windows Explorer to a completely new directory.
Open the VBP in a text editor and change any absolute paths to relative paths. VBP files are simple text files, and the format is even documented in the VB6 manual.
Here's an example. This evil VBP below has many absolute paths
Type=Exe
Form=c:\who\knows\where\B_Form.frm
Module=CModule; z:\magic\mapped\network\drive\heehee\C_Module.bas
Class=DClass; x:\personal\usb\stick\D_Class.cls
It would be changed to this benign VBP, which references local copies of the files. You can use relative paths for subdirectories.
Type=Exe
Form=B_Form.frm
Module=CModule; C_Module.bas
Class=DClass; subdirectory\D_Class.cls
If you mean from within Visual Studio, I don't think you can except by doing Save As for each file...
But the simpler approach is to just use Windows Explorer and copy the whole folder structure for the solution into another folder, (or do a recursive "Get" from your source code repository to a different local destination), and then open the solution or project file in the new location... The pointers in the project file that tell Visual Studio where 5all the individual source code and other files are located are generally all stored as relative paths, relative to the folder that the project file is in...
It's been a while since I used VB6, but I'd be tempted to move them using Windows Explorer, then manually edit the VBP file to point to the new locations afterwards. If I remember right, relative paths are fine in the VBP, so you may not even need to manke any changes.
Unbind from source control, if capable/appropriate.
Check into source control as a brand new solution/project
Recursive 'get' from your SCM into a new directory.
There's your new copy.
Create a VB6 Add-in. You can download it from: http://pan.baidu.com/s/1CXO3k
Or you can use below code to create your own.
Option Explicit
Public VBInstance As VBIDE.VBE
Public Connect As Connect
Private Sub CancelButton_Click()
Connect.Hide
End Sub
Private Sub OKButton_Click()
On Error Resume Next
Dim strProject As String
Dim strPath As String
Dim strPath2 As String
Dim strFile As String
Dim strPrjFile As String
Dim rst As VbMsgBoxResult
Dim m, n As Long
Dim col2 As Collection, col As Collection
Dim vbCom As VBComponent
Dim fso As FileSystemObject
Dim ts As TextStream
Dim f1 As String, f2 As String
strProject = Me.VBInstance.ActiveVBProject.FileName
strPath = ParseFileName(strProject, strPrjFile)
strPath2 = setFolder
If strPath = "" Or strPath = strPath2 Then
MsgBox "target folder is invalid or same as the project folder. Can't copy."
Exit Sub
End If
Set col2 = New Collection
Set col = New Collection
Set fso = New FileSystemObject
Set ts = fso.CreateTextFile(strPath2 & "\wemeet.log", False)
For m = Me.VBInstance.ActiveVBProject.VBComponents.Count To 1 Step -1
Set vbCom = Me.VBInstance.ActiveVBProject.VBComponents(m)
For n = 1 To vbCom.FileCount
f1 = vbCom.FileNames(n)
ParseFileName f1, strFile
f2 = strPath2 & "\" & strFile
fso.CopyFile f1, f2
col.Add f1
col2.Add f2
ts.WriteLine "" & Now() & " [Move]: " & f1
ts.WriteLine "" & Now() & " [To ]: " & f2
ts.WriteBlankLines 1
Next
Me.VBInstance.ActiveVBProject.VBComponents.Remove vbCom
Next
For m = 1 To col2.Count
Me.VBInstance.ActiveVBProject.VBComponents.AddFile col2.Item(m)
ts.WriteLine "" & Now() & " [Add]: " & col2.Item(m)
ts.WriteBlankLines 1
Next
Me.VBInstance.ActiveVBProject.SaveAs strPath2 & "\" & strPrjFile
ts.WriteLine "" & Now() & " [SaveAs]: " & strPath2 & "\" & strPrjFile
ts.WriteBlankLines 1
ts.Close
fso.OpenTextFile strPath2 & "\wemeet.log"
Set fso = Nothing
Set col = Nothing
Set col2 = Nothing
Set vbCom = Nothing
Connect.Hide
End Sub
Private Function ParseFileName(ByVal sPath As String, ByRef sFile As String) As String
Dim fso As New FileSystemObject
If fso.FileExists(sPath) Then
ParseFileName = fso.GetParentFolderName(sPath)
sFile = fso.GetFileName(sPath)
Else
ParseFileName = ""
sFile = ""
End If
Set fso = Nothing
End Function
Private Function setFolder() As String
Dim objDlg As Object
Dim objStartFolder As Object
Set objDlg = CreateObject("Shell.Application")
Set objStartFolder = objDlg.BrowseForFolder(&H0, "Select a folder", &H10 + &H1)
If InStr(1, TypeName(objStartFolder), "Folder") > 0 Then
setFolder = objStartFolder.ParentFolder.ParseName(objStartFolder.Title).Path
End If
Set objDlg = Nothing
End Function

Resources