Step 4 - Loading a File

Now that we know how to display a Text Control and access its properties, we can take care of some of the functions required in most word processing applications, which are file handling and printing.

To load a file, we first need to display a Common Dialog box which lets the user select the file to be opened. The common dialog box is added to the HTML page as an ActiveX object, just like the Text Control in Step 1:

<OBJECT
  classid="clsid:F9043C85-F6F2-101A-A3C9-08002B2F49FB"
  id=objComDlg
  >
</OBJECT>

In our button's OnClick event, we call objComDlg.ShowOpen to show the dialog box, and then, after the user has selected a filename, open the file in Text Control.

Sub Button1_OnClick
   on error resume next
   objComDlg.Filename = ""
   objComDlg.Filter = "Microsoft Word Format " & _
         "(*.doc)|*.doc|" & _
         "Rich Text Format (*.rtf)|*.rtf|" & _
         "Hypertext Markup Language " & _
         "(*.htm, *.html)|*.htm;*html|" & _
         "Text Documents (*.txt)|*.txt"
   objComDlg.ShowOpen
   if err.number = 0 then 
      If UCase(Right(objComDlg.FileName, 3)) = "DOC" Then
         objTX.Load objComDlg.Filename,0,9,0
      elseIf UCase(Right(objComDlg.FileName, 3)) = "RTF" _
      Then
         objTX.Load objComDlg.Filename,0,5,0
      elseIf UCase(Right(objComDlg.FileName, 3)) = "HTM" _
      or UCase(Right(objComDlg.FileName, 4)) = "HTML" Then
         objTX.Load objComDlg.Filename,0,4,0
      else
         objTX.Load objComDlg.Filename,0,1,0
      end if
   end if
End Sub

The code shown here is contained in Step4.htm in the IE\HTML sample source directory.