home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic 4 Unleashed / Visual_Basic_4_Unleashed_SAMS_Publishing_1995.iso / source / chap24 / wordapp.bas < prev   
Encoding:
BASIC Source File  |  1995-08-23  |  1.9 KB  |  58 lines

  1. Private Sub CreateWordDocument()
  2.     'sFilename is the file that we are going to create.
  3.     Dim sFileName As String
  4.     sFileName = "D:\test.doc"
  5.     'sText is what we will place in the file (the contents of the text box)
  6.     Dim sText As String
  7.     'Create an instance of Word
  8.     Dim objWord As Object
  9.     Set objWord = CreateObject("word.basic")
  10.     'Prevents errors when spelling checker completes OK.
  11.     On Error GoTo ErrorHandler
  12.     sText = Text1.TEXT
  13.     'Clear the clipboard
  14.     Clipboard.Clear
  15.     'Copy the text in Text1 to the clipboard
  16.     Clipboard.SetText sText, 1
  17.     'Check the clipboard to ensure that the text is there
  18.     If Clipboard.GetFormat(vbCFText) Then
  19.         'Using the word object
  20.         With objWord
  21.             'Select File | New from the menu to create a new document
  22.             .FileNew
  23.             'Paste the contents of the clipboard into the document
  24.             .EditPaste
  25.             'Spell check the document. If the spell check is successful, error 51 occurs.
  26.             .ToolsSpelling
  27.             'Select the text
  28.             .EditSelectAll
  29.             'Copy it to the clipboard
  30.             .EditCopy
  31.             'Save the word document (this creates a file)
  32.             .FileSaveAs sFileName
  33.             'Close the word document
  34.             .FileClose 2
  35.         End With
  36.     Else
  37.         'No text was in the text box
  38.         MsgBox "There is nothing to save."
  39.     End If
  40.     'Destroy the Word object
  41.     Set objWord = Nothing
  42.     'Reset the text box to blank
  43.     Text1.TEXT = ""
  44.     Exit Sub
  45. ErrorHandler:
  46.         Select Case Err
  47.             Case 51
  48.                 Resume Next
  49.             Case 5003
  50.                 MsgBox "That Command is not available at this time (disabled)"
  51.             Case Else
  52.                 MsgBox Error$
  53.         End Select
  54. End Sub
  55. Private Sub GetTextFromClipboard()
  56.     Text1.TEXT = Clipboard.GetText(vbCFText)
  57. End Sub
  58.