borderAndreaVB free resources for Visual Basic developersborder

AndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2013 Andrea Tincani
:: 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
:: Navigation

Home

VB and VBA for Word and Access

Previous Tip

Next Tip

:: Search this site
Google
:: Related Topics
icon 15-08-2004 Re: Display pictures on Forms in VB.NET by fabulous
icon 31-03-2004 Re: How can i load combobox from ".txt" document? by ragess
:: Sponsored Links



borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2013 Andrea Tincaniborder