home *** CD-ROM | disk | FTP | other *** search
/ Mastering Visual Basic 6 / mastvb6.iso / ch_code / ch14 / wordvba / wordvba.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1996-05-16  |  5.7 KB  |  169 lines

  1. VERSION 5.00
  2. Begin VB.Form WordVBAForm 
  3.    Caption         =   "Word VBA Demo"
  4.    ClientHeight    =   3750
  5.    ClientLeft      =   60
  6.    ClientTop       =   345
  7.    ClientWidth     =   9015
  8.    LinkTopic       =   "Form1"
  9.    ScaleHeight     =   3750
  10.    ScaleWidth      =   9015
  11.    StartUpPosition =   3  'Windows Default
  12.    Begin VB.CommandButton Command3 
  13.       Caption         =   "E X I T"
  14.       Enabled         =   0   'False
  15.       BeginProperty Font 
  16.          Name            =   "Verdana"
  17.          Size            =   9
  18.          Charset         =   0
  19.          Weight          =   400
  20.          Underline       =   0   'False
  21.          Italic          =   0   'False
  22.          Strikethrough   =   0   'False
  23.       EndProperty
  24.       Height          =   495
  25.       Left            =   6735
  26.       TabIndex        =   4
  27.       Top             =   3120
  28.       Width           =   2175
  29.    End
  30.    Begin VB.TextBox Text1 
  31.       BeginProperty Font 
  32.          Name            =   "Verdana"
  33.          Size            =   9
  34.          Charset         =   0
  35.          Weight          =   400
  36.          Underline       =   0   'False
  37.          Italic          =   0   'False
  38.          Strikethrough   =   0   'False
  39.       EndProperty
  40.       Height          =   3285
  41.       Left            =   135
  42.       MultiLine       =   -1  'True
  43.       ScrollBars      =   2  'Vertical
  44.       TabIndex        =   2
  45.       Text            =   "WordVBA.frx":0000
  46.       Top             =   345
  47.       Width           =   6480
  48.    End
  49.    Begin VB.CommandButton Command2 
  50.       Caption         =   "Massage DOC file"
  51.       Enabled         =   0   'False
  52.       BeginProperty Font 
  53.          Name            =   "Verdana"
  54.          Size            =   9
  55.          Charset         =   0
  56.          Weight          =   400
  57.          Underline       =   0   'False
  58.          Italic          =   0   'False
  59.          Strikethrough   =   0   'False
  60.       EndProperty
  61.       Height          =   495
  62.       Left            =   6720
  63.       TabIndex        =   1
  64.       Top             =   2505
  65.       Width           =   2175
  66.    End
  67.    Begin VB.CommandButton Command1 
  68.       Caption         =   "Create DOC file"
  69.       BeginProperty Font 
  70.          Name            =   "Verdana"
  71.          Size            =   9
  72.          Charset         =   0
  73.          Weight          =   400
  74.          Underline       =   0   'False
  75.          Italic          =   0   'False
  76.          Strikethrough   =   0   'False
  77.       EndProperty
  78.       Height          =   495
  79.       Left            =   6720
  80.       TabIndex        =   0
  81.       Top             =   1905
  82.       Width           =   2175
  83.    End
  84.    Begin VB.Label Label1 
  85.       Caption         =   "Enter Your Text Here"
  86.       BeginProperty Font 
  87.          Name            =   "Verdana"
  88.          Size            =   9.75
  89.          Charset         =   0
  90.          Weight          =   700
  91.          Underline       =   0   'False
  92.          Italic          =   0   'False
  93.          Strikethrough   =   0   'False
  94.       EndProperty
  95.       Height          =   285
  96.       Left            =   150
  97.       TabIndex        =   3
  98.       Top             =   15
  99.       Width           =   2475
  100.    End
  101. Attribute VB_Name = "WordVBAForm"
  102. Attribute VB_GlobalNameSpace = False
  103. Attribute VB_Creatable = False
  104. Attribute VB_PredeclaredId = True
  105. Attribute VB_Exposed = False
  106. Dim WordApp As Word.Application
  107. Private Sub Command1_Click()
  108. Dim thisDoc As Document
  109. Dim thisRange As Range
  110. Dim prnTime As Date
  111. Dim breakLoop As Boolean
  112.     Me.Caption = "Creating document..."
  113.     Set thisDoc = WordApp.Documents.Add
  114.     thisDoc.Range.InsertBefore "Document Title" & vbCrLf & vbCrLf
  115.     Set thisRange = thisDoc.Paragraphs(1).Range
  116.     thisRange.Font.Bold = True
  117.     thisRange.Font.Size = 14
  118.     thisRange.ParagraphFormat.Alignment = wdAlignParagraphCenter
  119.     thisRange.InsertAfter "This sample  document was created automatically with a Visual Basic application." & vbCrLf
  120.     thisRange.InsertAfter "You can   enter additional text here  " & vbCrLf
  121.     thisRange.InsertAfter vbCrLf & vbCrLf
  122.     thisRange.InsertAfter "This  project was   created for Mastering VB5 "
  123.     thisRange.InsertAfter "(Sybex,  1997) and was tested with Word 97."
  124.     thisRange.InsertAfter vbCrLf
  125.     thisRange.InsertAfter "Your text follows"
  126.     thisRange.InsertAfter Text1.Text
  127.     Me.Caption = "Saving document..."
  128.     thisDoc.SaveAs "c:\sample.doc"
  129.     Me.Caption = "Printing document..."
  130.     thisDoc.PrintOut True, True
  131.     prnTime = Time
  132.     breakLoop = False
  133.     While WordApp.BackgroundPrintingStatus <> 0 And Not breakLoop
  134.         If Minute(Time - prnTime) > 1 Then
  135.             Reply = MsgBox("Word is taking too long to print." & vbCrLf & "Do you want to quit?", vbYesNo)
  136.             If Reply = vbYes Then
  137.                 breakLoop = True
  138.             Else
  139.                 prnTime = Time
  140.             End If
  141.         End If
  142.     Wend
  143.     WordApp.Quit
  144.     MsgBox "Document saved and printed!"
  145.     Command2.Enabled = True
  146.     Command3.Enabled = True
  147.     Me.Caption = "Word VBA Demo"
  148. End Sub
  149. Private Sub Command2_Click()
  150. Dim thisDoc As Document
  151. Dim thisRange As Range
  152.     WordApp.Documents.Open ("c:\sample.doc")
  153.     WordApp.Visible = False
  154.     Set thisDoc = WordApp.ActiveDocument
  155.     thisDoc.Content.Find.Execute FindText:="VB5", ReplaceWith:="VB6", Replace:=wdReplaceAll
  156.     While thisDoc.Content.Find.Execute(FindText:="  ", Wrap:=wdFindContinue)
  157.         thisDoc.Content.Find.Execute FindText:="  ", ReplaceWith:=" ", Replace:=wdReplaceAll, Wrap:=wdFindContinue
  158.     Wend
  159. End Sub
  160. Private Sub Command3_Click()
  161.     WordApp.Quit
  162.     Set WordApp = Nothing
  163.     End
  164. End Sub
  165. Private Sub Form_Load()
  166.     Set WordApp = CreateObject("Word.Application")
  167.     WordApp.Visible = False
  168. End Sub
  169.