Cannot use Selection.TypeText method when writing to Word document from Excel VBA

I am trying to write a Word document from Excel VBA, and when I try to use a method .TypeTextfor an object Selection, I get an error: "The object does not support this property or method."

I read somewhere that Excel VBA does not know what I mean by the Selection object in a Word document, so I tried the proposed solution, which was to try to do this in With- End With.

I basically tried this:

Set WrdApp = New Word.Application
Set DestDoc = WrdApp.Documents.Add
With DestDoc
   .Activate
   .Select
   .Selection.TypeText Text:="Test"
End With    

But it always reports the same error on the line .Selection.TypeText.

Any help would be appreciated.

+4
source share
1

:

Sub WriteToWord()

Dim WrdApp As New Word.Application
Dim WrdDoc As Document
Dim WrdSel As Selection

WrdApp.Visible = True
Set WrdDoc = WrdApp.Documents.Add
Set WrdSel = WrdApp.Selection

WrdSel.TypeText "Test"

End Sub

. , , , DestDoc .Selection. . , , , .:)

, .

+2

Source: https://habr.com/ru/post/1525328/


All Articles