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 xlworkbook.Save() xlworkbook.Close() xlapp.Quit() xlapp = Nothing xlworkbook = Nothing
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
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 MailMsg.Display(True) MailApp.Quit() MailApp = Nothing
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.