VBA/Excel/Access/Word/Word/Word Text

Материал из VB Эксперт
Перейти к: навигация, поиск

Define a Range

 
Sub RangeText()
    Dim wdApp As Word.Application
    Dim wdDoc As Document
    Dim wdRng As Word.Range
    
    Set wdApp = GetObject(, "Word.Application")
    Set wdDoc = wdApp.ActiveDocument
    
    Set wdRng = wdDoc.Range(0, 22)
    wdRng.Select
    
    Set wdApp = Nothing
    Set wdDoc = Nothing
    Set wdRng = Nothing
End Sub



The Range object selects paragraphs.

 
Sub SelectSentence()
    Dim wdApp As Word.Application
    Dim wdRng As Word.Range
    
    Set wdApp = GetObject(, "Word.Application")
    
    With wdApp.ActiveDocument
        If .Paragraphs.Count >= 3 Then
            Set wdRng = .Paragraphs(3).Range
            wdRng.Copy
        End If
    End With
    Worksheets("Sheet2").PasteSpecial
    Worksheets("Sheet2").Paste Destination:=Worksheets("Sheet2").Range("A1")
    
    Set wdApp = Nothing
    Set wdRng = Nothing
End Sub



TypeText method inserts text into a Word document

 
Sub InsertText()
    Dim wdApp As Word.Application
    Dim wdDoc As Document
    Dim wdSln As Selection
    
    Set wdApp = GetObject(, "Word.Application")
    Set wdDoc = wdApp.ActiveDocument
    Set wdSln = wdApp.Selection
    
    wdDoc.Application.Options.Overtype = False
    With wdSln
        If .Type = wdSelectionIP Then
            .TypeText ("Inserting at insertion point. ")
        ElseIf .Type = wdSelectionNormal Then
                If wdApp.Options.ReplaceSelection Then
                    .Collapse Direction:=wdCollapseStart
                End If
               .TypeText ("Inserting before a text block. ")
        End If
    End With
    Set wdApp = Nothing
    Set wdDoc = Nothing
End Sub