Private Sub frmLoader_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Constant for the project's name
Const sProjectName As String = "Project Name Here"
'Constants for the Excel startup file name and components folder
Const sStartupFile As String = "\StartupApp.xls"
Const sComponentFolder As String = "\Components"
'Late-binding for Excel application
Dim objExcelApp As Object
Dim sProjectPath As String
On Error GoTo ErrHandle
'Location for the project file startup path
sProjectPath = Application.StartupPath & sComponentFolder
'Create the excel instance
objExcelApp = CreateObject("Excel.Application")
'Open the startup file
With objExcelApp
.Visible = True
.Workbooks.Open(sProjectPath & sStartupFile)
.UserControl = True
End With
ErrorExit:
objExcelApp = Nothing
Me.Close()
Exit Sub
ErrHandle:
Dim sMsg As String
Dim sStyle As String
Dim sTitle As String
sMsg = "Application could not be started due to a missing component"
sStyle = 0 + 16
sTitle = sProjectName
MsgBox(sMsg, sStyle, sTitle)
GoTo ErrorExit
End Sub
If either of you would like to post an Excel macro that you would like to run from Visual Basic, I would be happy to post the converted code. Please, though, do not make it too long, say, not more than about 20 executable lines of code.
Option Explicit
Dim a(2) As String
Sub test()
Dim title As String
Dim i As Integer
For i = 1 To 2
If i = 1 Then title = "FIRST NAME" Else title = "LAST NAME"
Do
a(i) = Trim(Application.InputBox("What's your " & LCase(title) & "?", title, , Type:=2))
If a(i) = "" Then
If MsgBox("Do you want to end the programm?", 36, "QUIT?") = vbYes Then Exit Sub
End If
If Len(a(i)) = 1 Then MsgBox "Please your ENTIRE " & LCase(title) & "!", 48, title
Loop While a(i) = "" Or Len(a(i)) = 1
Next i
MsgBox "Hello, " & a(1) & " " & a(2) & "!", 48, "FULL NAME"
create_txt_file
End Sub
Private Sub create_txt_file()
Dim fs As Object
Dim txtFile As Object
Set fs = CreateObject("Scripting.FileSystemObject")
Set txtFile = fs.CreateTextFile("c:\windows\desktop\testfile.doc", True)
txtFile.WriteLine (a(1) & " " & a(2) & vbTab & Date)
'txtFile.Close
End Sub
Public Class frmForErik
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Dim a(2) As String
Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click
Dim title As String
Dim i As Integer
For i = 1 To 2
If i = 1 Then title = "FIRST NAME" Else title = "LAST NAME"
Do
a(i) = Trim(InputBox("What's your " & LCase(title) & "?", title))
If a(i) = "" Then
If MsgBox("Do you want to end the programm?", , "QUIT?") = vbYes Then Exit Sub
End If
If Len(a(i)) = 1 Then MsgBox("Please your ENTIRE " & LCase(title) & "!", , title)
Loop While a(i) = "" Or Len(a(i)) = 1
Next i
MsgBox("Hello, " & a(1) & " " & a(2) & "!", , "FULL NAME")
CreateFile()
End Sub
Sub CreateFile()
Dim fs As IO.StreamWriter = IO.File.CreateText("c:\testfile.doc")
Dim sFullName As String
sFullName = a(1) & " " & a(2) & " " & Now
With fs
.WriteLine(sFullName)
.Close()
End With
Dim sDocFile As String
OpenFileDialog1.ShowDialog()
sDocFile = OpenFileDialog1.FileName
Dim sr As IO.StreamReader = IO.File.OpenText(sDocFile)
With lstOutput
.Items.Clear()
Do While sr.Peek <> -1
.Items.Add(sr.ReadLine)
Loop
End With
sr.Close()
End Sub
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
IO.File.Delete("c:\testfile.doc")
Me.Close()
End Sub
End Class