Using Automation with Microsoft Word - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Using Automation with Microsoft Word

VBobVBob Posts: 1Member
We are taking text from a Rich Textbox control on a VB form and putting it into a new instance of Microsoft Word. We want to be able to edit the text in the Word document, close the document and put the edited text back into the Rich Textbox control. This is the code sample:

Private Sub cmdShowWord_Click()
Dim oDoc As Word.Document

If MSWordOpen() Then
' Add a New Document
Set oDoc = goWord.Documents.Add
' Add Information to the Document
oDoc.Range.Text = txtSentence

At this point, we want Word to "pause" so that the user can
edit the text that we placed into oDoc.Range.Text, disable
the Save button since we don't want to save the file to a
directory on the network, close the document and finally return
the text back into the Rich Text Box control on our VB form.

' Get Sentence Back From Word Document
txtSentence.Text = oDoc.Range.Text

End If
End Sub

We would appreciate any code that would help achieve our objective. Thank you.

Comments

  • PavlinIIPavlinII Posts: 404Member
    : We are taking text from a Rich Textbox control on a VB form and putting it into a new instance of Microsoft Word. We want to be able to edit the text in the Word document, close the document and put the edited text back into the Rich Textbox control. This is the code sample:
    :
    : Private Sub cmdShowWord_Click()
    : Dim oDoc As Word.Document
    :
    : If MSWordOpen() Then
    : ' Add a New Document
    : Set oDoc = goWord.Documents.Add
    : ' Add Information to the Document
    : oDoc.Range.Text = txtSentence
    :
    : At this point, we want Word to "pause" so that the user can
    : edit the text that we placed into oDoc.Range.Text, disable
    : the Save button since we don't want to save the file to a
    : directory on the network, close the document and finally return
    : the text back into the Rich Text Box control on our VB form.
    :
    : ' Get Sentence Back From Word Document
    : txtSentence.Text = oDoc.Range.Text
    :
    : End If
    : End Sub
    :
    : We would appreciate any code that would help achieve our objective. Thank you.
    :
    :

    Hi, what about this? (WithEvents gives you events from Word)

    [code]Private WithEvents myWord As Word.Application
    Private oDoc As Word.Document
    Private WordHasBeenClosed As Boolean [green]'For pausing proc[/green]

    Private Sub cmdShowWord_Click()
    Set myWord = New Word.Application
    myWord.Visible = True
    [green]'Add a New Document[/green]
    Set oDoc = myWord.Documents.Add()
    [green]'Add Information to the Document[/green]
    oDoc.Range.Text = txtSentence

    [green]'Recognising that word has been closed inside this procedure - this may be unnecessary[/green]
    WordHasBeenClosed = False [green]'Make sure[/green]
    Do While Not WordHasBeenClosed
    DoEvents [green]'Let's the system DoWhatItWants[/green]
    Loop
    MsgBox "Word has been closed"

    End Sub

    Private Sub myWord_DocumentBeforeClose(ByVal Doc As Word.Document, Cancel As Boolean)
    If Doc.Name = oDoc.Name Then [green]'You don't want text from any other document[/green]
    txtSentence.Text = oDoc.Range.Text [green]'Save the text[/green]
    [green]'Prevent asking whether "Do you want to save changes.." Yes/No/Cancel - Saving is disabled[/green]
    Doc.Close False
    End If
    End Sub

    Private Sub myWord_DocumentBeforeSave(ByVal Doc As Word.Document, SaveAsUI As Boolean, Cancel As Boolean)
    If Doc.Name = oDoc.Name Then [green]'If you want to let work with another documents[/green]
    MsgBox "It's not possible to save document"
    Cancel = True
    End If
    End Sub

    Private Sub myWord_Quit() [green]'This may be not necessary[/green]
    WordHasBeenClosed = True [green]'Pausing[/green]
    End Sub[/code]Hope, this will help..

    PavlinII
Sign In or Register to comment.