Presentation is loading. Please wait.

Presentation is loading. Please wait.

ActiveX Introduction using Word and Excel Please use speaker notes for additional information!

Similar presentations


Presentation on theme: "ActiveX Introduction using Word and Excel Please use speaker notes for additional information!"— Presentation transcript:

1 ActiveX Introduction using Word and Excel Please use speaker notes for additional information!

2

3 PrWordx.vbp

4 mwkText is defined as Word.Application. It is use to open and close Word documents. wkFileName is set up to hold the name of the file. A default of mytext.doc is used if no name is entered into the text box. This calls fro a document which is blank and visible. The texture can be set a variety of ways. The index of 1 here is because this is the first document being edited. SaveAs saves the file name under the name entered or the default mytext.doc.

5 WordLinkProj04.vbp This is a DriveListBox. This is a DirListBox. This is a FileListBox.

6 WordLinkProj04.vbp

7 Option Explicit Dim wdAppl As Word.Application Dim wdCmdBar As Object Dim wdDoc1 As Word.Document Dim DocumentOpen As Boolean Dim TargetDoc, Response, PathMsg, ErrMsg As String This code established a Word.Application, an Object, a Word.Document, a Boolean indicator and several string variables. Private Sub Form_Load() FileBox1.Pattern = "*.doc" cmdOpenDoc.Enabled = True cmdCloseDoc.Enabled = False Call Open_Word End Sub Pattern establishes the pattern of the filenames to be displayed in the FileListBox control at run time. I only want to see files with the extension.doc. Open_Word is on the next slide. Private Sub DirBox1_Change() FileBox1.Path = DirBox1.Path End Sub Private Sub DriveBox1_Change() DirBox1.Path = DriveBox1.Drive FileBox1.Refresh End Sub Here I am taking the selected Drive and moving it to DirBox1.Path. This change in the path causes the directory change to execute. And then the file box gets filled according to the pattern established in form load.

8 Private Sub Open_Word() Set wdAppl = CreateObject("Word.Application") ErrMsg = "Word version " & wdAppl.Version & " is open" Response = MsgBox(ErrMsg, vbOKOnly) wdAppl.WindowState = wdWindowStateNormal wdAppl.Move Top:=0, Left:=350 wdAppl.Visible = False For Each wdCmdBar In wdAppl.CommandBars wdCmdBar.Enabled = False Next wdCmdBar End Sub Option Explicit Dim wdAppl As Word.Application Dim wdCmdBar As Object Dim wdDoc1 As Word.Document Dim DocumentOpen As Boolean Dim TargetDoc, Response, PathMsg, ErrMsg As String Shown on previous slide. Set up a Normal window (neither maximized or minimized) and move to a particular location. Set visible to false.

9 Private Sub cmdOpenDoc_Click() TargetDoc = FileBox1.Path & "\" & FileBox1.FileName On Error Resume Next Set wdDoc1 = wdAppl.Documents.Open(TargetDoc,, True) If Err = 0 Then For Each wdCmdBar In wdDoc1.CommandBars wdCmdBar.Enabled = False Next wdCmdBar wdDoc1.Application.Visible = True DocumentOpen = True cmdOpenDoc.Enabled = False cmdCloseDoc.Enabled = True ElseIf Err = 5121 Then ErrMsg = "'" & TargetDoc & "' is not a valid path." Response = MsgBox(ErrMsg, vbOKOnly) ElseIf Err = 462 Then ErrMsg = "Word has been closed. Reselect document to be displayed." Response = MsgBox(ErrMsg, vbOKOnly) Call Open_Word Else ErrMsg = "Error '" & Err & "' encountered." Response = MsgBox(ErrMsg, vbOKOnly) End If End Sub Option Explicit Dim wdAppl As Word.Application Dim wdCmdBar As Object Dim wdDoc1 As Word.Document Dim DocumentOpen As Boolean Dim TargetDoc, Response, PathMsg, ErrMsg As String Shown on previous slide.

10 Private Sub cmdCloseDoc_Click() On Error Resume Next wdDoc1.Close (wdDoNotSaveChanges) If Err <> 0 Then Response = MsgBox("Document is already closed.", vbOKOnly) End If DocumentOpen = False wdAppl.Visible = False For Each wdCmdBar In wdDoc1.CommandBars wdCmdBar.Enabled = True Next wdCmdBar Set wdDoc1 = Nothing cmdCloseDoc.Enabled = False cmdOpenDoc.Enabled = True End Sub The document is closed and the command bar is enabled. The set wdDoc1 = Nothing is given and the command buttons are dealt with. Private Sub Form_Terminate() If DocumentOpen = True Then cmdCloseDoc_Click End If On Error Resume Next For Each wdCmdBar In wdAppl.CommandBars wdCmdBar.Enabled = True Next wdCmdBar wdAppl.Quit (wdDoNotSaveChanges) End Sub

11 PrPaySpx.vbp

12

13 The form load makes the application visible and sets the cells on the top row with the column names. It then changes visible to false. Note that rowct is initialized at 2 to handle the headers and a blank row following the headers. Net is calculated and then the data is put in as the values of the cells using rowct to determine the number to determine the cell within that row. The application is quit and the object is set to nothing. PrPaySpx. vbp

14 xlPay2.vbp

15

16

17 Option Explicit Dim xlapp As Excel.Application Dim xlbook As Excel.Workbook Dim xlsheet As Excel.Worksheet Dim xlrange As Excel.Range Dim wkPay As Single Dim nameArray(1 To 5) Dim payHourArray(1 To 5) Dim hoursArray(1 To 5) Dim x As Integer Dim ptr As Integer Dim SprdShtNam As String Private Sub Form_Load() On Error GoTo Err_Rtn ptr = 0 Set xlapp = CreateObject("Excel.Application") If Right(App.Path, 1) = "\" Then SprdShtNam = App.Path & "Pay2.xls" Else SprdShtNam = App.Path & "\Pay2.xls" End If xlapp.Workbooks.Open SprdShtNam Set xlbook = xlapp.ActiveWorkbook Set xlsheet = xlbook.Worksheets(1) x = xlsheet.Range("X1").Value Exit Sub Err_Rtn: Select Case Err.Number Case 1004 'file not found xlapp.Workbooks.Add xlapp.Workbooks(1).SaveAs SprdShtNam xlapp.Workbooks(1).Close Case Else MsgBox "Error: " & Err.Number & " - " & Err.Description End End Select Resume End Sub Private Sub Form_Activate() ptr = 0 End Sub Ptr is set to 0 everytime the form is activated so that a new group of 5 can be started.

18 Private Sub Pay_Click() xlbook.Worksheets(1).Delete xlbook.Worksheets.Add Set xlsheet = xlbook.Worksheets(1) xlsheet.Columns("B").ColumnWidth = 15 x = 0 Call Display_Data End Sub Private Sub Add_Click() Call Display_Data End Sub Private Sub Exit_Click() xlsheet.Range("X1").Value = x xlbook.Save Set xlrange = Nothing Set xlsheet = Nothing Set xlbook = Nothing xlapp.Quit Set xlapp = Nothing End End Sub When Exist is clicked, one of the key things is to save the value of x in the spreadsheet at X1. This tells how many elements are currently written to the spreadsheet. This value will be used when you add to the spreadsheet to determine where the add should begin. Private Sub Form_Unload(Cancel As Integer) Call Exit_Click End Sub When the form is unloaded, Exit_Click is called to exit the project.

19 X is used to determine where to start putting the elements of the array. The i is the number of the current group in the array and x tells how many are already in the spreadsheet. Private Sub Display_Data() Dim i As Integer Dim FormulaStr As String xlapp.Visible = True xlapp.WindowState = xlMaximized With xlsheet.Cells(x + 2, 5).Clear.Cells(x + 3, 5).Clear For i = 1 To 5.Cells(i + x, 1).Value = "Emp # " & i + x.Cells(i + x, 2).Value = nameArray(i).Cells(i + x, 3).Value = Format(payHourArray(i), "$##,##0.00").Cells(i + x, 4).Value = hoursArray(i).Cells(i + x, 5).Font.Bold = True.Cells(i + x, 5).NumberFormat = "$#,##0.00" FormulaStr = "=C" & i + x & "*" & "D" & i + x.Cells(i + x, 5).Formula = FormulaStr Next x = i + x - 1 Set xlrange =.Range(.Cells(1, 5),.Cells(x, 5)) xlrange.Name = "grossRange".Cells(x + 2, 3).Value = " Total Gross Pay".Cells(x + 2, 5).Font.Bold = True.Cells(x + 2, 5).NumberFormat = "$##,##0.00".Cells(x + 2, 5).Formula = "=Sum(grossRange)".Cells(x + 3, 4).Value = "Average".Cells(x + 3, 5).Font.Bold = True.Cells(x + 3, 5).NumberFormat = "$##,##0.00".Cells(x + 3, 5).Formula = "=Average(grossRange)" End With Me.SetFocus txtName.Enabled = True txtName.Text = "" txtPayHr.Enabled = True txtPayHr.Text = "" txtHours.Enabled = True txtHours.Text = "" ptr = 0 End Sub

20 Private Sub txtHours_LostFocus() hoursArray(ptr) = txtHours.Text If ptr = 5 Then txtName.Enabled = False txtPayHr.Enabled = False txtHours.Enabled = False MsgBox "Array full" Else txtName.Text = "" txtPayHr.Text = "" txtHours.Text = "" txtName.SetFocus End If End Sub Private Sub txtName_LostFocus() ptr = ptr + 1 nameArray(ptr) = txtName.Text End Sub Private Sub txtPayHr_LostFocus() payHourArray(ptr) = txtPayHr.Text End Sub Checks to see if the array which holds elements of data is full. Initializes the ptr which is used to put data into the array at 1. Remember that when the focus leaves the spreadsheet and the form is activated, ptr is set to 0. Private Sub Form_Activate() ptr = 0 End Sub xlPay2.vbp

21 XLLink012.vbp These are MaskEdBox boxes which will edit the data.

22 XLLink012.vbp

23 Option Explicit Dim xlAppl As Excel.Application Dim xlWrkBk As Excel.Workbook Dim xlWrkSht As Excel.Worksheet Dim lasty, lastx As Integer Private Sub cmdCalc_Click() Dim Term, YrIntvl, Year, x, y As Integer Dim Amt, Intrst, IntIntvl, Rate As Single Dim Paymnts, strRate, xlFormula, xlValue As String cmdCalc.Enabled = False Amt = mebAmt.Text Term = Val(mebTerm.Text) Intrst = Val(mebInt.Text) If optbtnYr1.Value = True Then YrIntvl = 1 Else YrIntvl = 5 End If If optbtnInt14.Value = True Then IntIntvl = 0.25 Else IntIntvl = 0.5 End If y = 1 Rate = Intrst For x = 2 To 1 + (2 / IntIntvl) xlValue = Format(Rate, "00.00") & " %" xlWrkSht.Cells(y, x).Value = xlValue Rate = Rate + IntIntvl Next x y = y + 2 Year = Term

24 Do While Year <= 30 xlValue = Str(Year) & " years" xlWrkSht.Cells(y, 1).Value = xlValue Paymnts = Str(Year * 12) Rate = Intrst For x = 2 To 1 + (2 / IntIntvl) strRate = Str(Rate / 1200) xlFormula = "=(-1)*(PMT(" & strRate & "," & Paymnts & "," & Amt & "))" xlWrkSht.Cells(y, x).Formula = xlFormula xlWrkSht.Cells(y, x).NumberFormat = "###0.00" Rate = Rate + IntIntvl lastx = x Next x y = y + 1 lasty = y Year = Year + YrIntvl Loop xlWrkSht.Protect cmdReset.Enabled = True xlAppl.Visible = True End Sub

25 Private Sub cmdReset_Click() cmdReset.Enabled = False xlAppl.Visible = False With xlWrkSht.Unprotect.Range(.Cells(1, 1),.Cells(lasty, lastx)).Clear End With mebAmt.Mask = "" mebAmt.Text = "" mebAmt.Mask = "999999" mebInt.Mask = "" mebInt.Text = "" mebInt.Mask = "99.99" mebTerm.Mask = "" mebTerm.Text = "" mebTerm.Mask = "99" cmdCalc.Enabled = True mebAmt.SetFocus End Sub Private Sub Form_Load() Set xlAppl = CreateObject("Excel.Application") With xlAppl.WindowState = xlNormal.Top = 0.Left = 200.Width = 600.Visible = False End With Set xlWrkBk = xlAppl.Workbooks.Add Set xlWrkSht = xlWrkBk.Worksheets.Add xlWrkSht.Name = "Mnthly Mort Pymnt" cmdReset.Enabled = False cmdCalc.Enabled = True End Sub Private Sub Form_Terminate() xlWrkBk.Close SaveChanges:=False xlAppl.Quit End Sub


Download ppt "ActiveX Introduction using Word and Excel Please use speaker notes for additional information!"

Similar presentations


Ads by Google