PDA

View Full Version : [SOLVED:] Selecting all the text in a word document via excel userfrom



Andy2011
03-01-2015, 12:20 PM
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.

gmayor
03-02-2015, 03:49 AM
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

Andy2011
03-02-2015, 08:28 AM
Amazing.....thank you!