Mike Walsted's Visual Basic .NET Helpful Notes

This page is here to store notes for myself and others who are trying to use Visual Basic .NET (part of Microsoft Visual Studio,) to help automate some office tasks.

I am not the best at commenting code. The samples below are provided without comments, but should be pretty understandable and straight forward. The examples probably do not show good programming technique, format, or the like, and may make computer science instructors nauseous. I have also not included all of the variable declaration statements, figuring that people generally have that figured out.

Here is some sample code for using Excel:

    Dim xlapp As New Microsoft.Office.Interop.Excel.Application
    Dim xlworkbook As Microsoft.Office.Interop.Excel.Workbook
    Dim xlworksheet As Microsoft.Office.Interop.Excel.Worksheet

    xlworkbook = xlapp.Workbooks.Open("C:/SomePath/Filename.xlsx")
    xlworksheet = xlworkbook.Worksheets("Sheet1")

    SomeVariable = xlworksheet.Cells(RowCount, ColCount).Value

    xlworksheet.Cells(RowCount, ColCount).Value = SomeVariable

    xlapp = Nothing
    xlworkbook = Nothing

This code creates controls that can be handled similar to the dynamic controls of Visual Basic 5 and Visual Basic 6:
For counter=0 to 5
    SomeControlBox(counter) = New TextBox
    SomeControlBox(counter).Parent = Me
    SomeControlBox(counter).Left = 20 + (counter * 140)
    SomeControlBox(counter).Top = 50
    SomeControlBox(counter).Width = 105
    SomeControlBox(counter).Tag = Format(counter, "00")
    AddHandler SomeControlBox(counter).Leave, AddressOf SomeControlBox_LostFocus
    AddHandler SomeControlBox(counter).Enter, AddressOf SomeControlBox_Enter
Next Counter

Private Sub SomeControlBox_LostFocus(sender As Object, e As EventArgs)        
    ItemSelected = CInt(CStr(sender.Tag))
    SomeVariable(ItemSelected) = Trim(SomeControlBox(ItemSelected).Text)
End Sub

The code below will automaticallt generate (but not send) an email. Program execution will be halted until the email is closed or sent using this method:
    Dim MailApp As New Microsoft.Office.Interop.Outlook.Application
    Dim MailMsg As Outlook.MailItem = Nothing
    MailMsg = MailApp.CreateItem(Outlook.OlItemType.olMailItem)
    MailMsg.BodyFormat = Outlook.OlBodyFormat.olFormatPlain
    MailMsg.Subject = "Canned Pork Product"
    MailMsg.To = "PotentialCustomer@SomeRetailer.com"
    MailMsg.CC = "YourBoss@YourOffice.com"
    MailMsg.Body = BodyMessageStringVariable
    MailApp = Nothing

The code to play an audio file is pretty simple, assuming that the file is in the one format that Visual Basic will play directly (.wav):
    My.Computer.Audio.Play("C:/SomePath/Departure.wav", AudioPlayMode.Background)
If you are writing code that could use the NATO (phoenetic) alphabet and number .wav files, you are welcome to use the set of audio files recorded by Casey Walsted. This set contains the NATO alphabet, numbers, and some other words that may be useful if you are writing an application for aviation monitoring purposes. These files ARE compatble with Visual Basic.NET using the code above. If you use these files, please visibly credit her as the source. A sample file is here.

If you have any questions or comments about this page, or need to contact me for any other reason, please email me at mike@mikewalsted.com. If you have questions about programming, I will try to answer, but you would probably be much better off asking someone more knowledgeable, such as Google. Please remember, this code is not in a format that will get you a passing grade in your CS courses, but it works for me.