| :: How to add specific text inside a Word's document using the Clipboard |
Author |
Andrea Tincani |
Language |
VB5, VB6 |
Operating
Systems |
Windows 95, 98 and NT |
| Usage |
Option Explicit
'Create a Form with a command
button, a miltiline textbox and two checkboxes (with 'bold' and 'underline' caption)
Dim WithEvents w As Word.Application
Dim UnloadForm As Boolean
Const docName = "c:\Doc1.doc"
Private Sub Command1_Click()
Dim MyDoc As Document
Dim Opened As Boolean
Opened = False
For Each MyDoc In w.Documents
If MyDoc.FullName = docName Then Opened = True
Next
If Opened Then GoTo AlreadyOpen
On Error GoTo NewDoc
Opened = False
Set MyDoc = w.Documents.Open(docName, Revert:=False)
GoTo AlreadyOpen
NewDoc:
Set MyDoc = w.Documents.Add
MyDoc.SaveAs docName
AlreadyOpen:
'Clear the
Clipboard
Clipboard.Clear
'Insert
the text into the Clipboard
Clipboard.SetText .Text
'Set the
format of the new text
MyDoc.Words.Item(MyDoc.Words.Count).Bold = (Check1.Value = vbChecked)
MyDoc.Words.Item(MyDoc.Words.Count).Underline = (Check2.Value =
vbChecked)
'Paste the
text at the end of the document
MyDoc.Words.Item(MyDoc.Words.Count).Paste
End Sub
Private Sub Form_Activate()
If UnloadForm Then Unload Me
End Sub
Private Sub Form_Load()
'Create
the Word reference
On Error GoTo WordError
Set w = New Word.Application
w.Visible = True
Exit Sub
WordError:
MsgBox "Error creating a Word reference: " &
Err.Description, vbCritical, "Automation Error"
UnloadForm = True
End Sub
Private Sub Form_Unload(Cancel As Integer)
'Quit Word
and save changes
w.Quit wdSaveChanges
End Sub |
|
 |
|
 |