Consulting

Results 1 to 3 of 3

Thread: Selecting all the text in a word document via excel userfrom

  1. #1
    VBAX Regular
    Joined
    Jul 2011
    Posts
    10
    Location

    Selecting all the text in a word document via excel userfrom

    I am sending data from an excel userform to word using late binding.

    I am trying to then select all the text placed in the document. I have tried using variations of

    docWord.Selection.WholeStory
    docWord.Selection.Copy
    However nothing seems to work. I am sure i'm doing something fundamentally wrong as I'm trying to get my head around this binding process but would appreciate any help...

    I have a sample spreadsheet attached.
    Attached Files Attached Files

  2. #2
    docWord.Range.Copy
    should do the job, but you should do this before unloading the form and certainly not after closing the Excel application.
    Unload the form in the code that calls the userform.
    Change the command button code to
    Private Sub CommandButton1_Click()
        Me.Hide
        Me.Tag = 1
    End Sub
    and the main code to
    Sub Macro1()
    Dim oFrm As New UserForm1
    Dim applWord As Object
    Dim docWord As Object
    Dim checksRange As Range, i As Long, StrChecks As String
        With oFrm
            .Show
            If .Tag = 1 Then
                Set applWord = CreateObject("Word.Application")
                applWord.Visible = True
                applWord.WindowState = 1
                Set docWord = applWord.Documents.Add
                docWord.Content.InsertAfter "These are the animals you have selected...sdkfnskdfskfn" & vbNewLine
                For i = 0 To .ListBox2.ListCount - 1
                    .ListBox2.Selected(i) = True
                    StrChecks = StrChecks & .ListBox2.List(i, 0) & "|"
                Next
                docWord.Content.InsertAfter vbNewLine & Replace(Left(StrChecks, Len(StrChecks) - 1), "|", ", " & vbNewLine) & "."
                docWord.CheckSpelling
                docWord.Range.Copy
            End If
        End With
        Unload oFrm
    'Close the workbook here
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Regular
    Joined
    Jul 2011
    Posts
    10
    Location
    Amazing.....thank you!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •