Create Word Application

  • To create word application use below mentioned code
  • Dim WApp As Word.Application
    Set WApp = New Word.Application

     

    Create Word Document

  • To Open Word Document which exists in a path - use below mentioned code
  • Dim WDoc As Word.Document
    Set WDoc = WApp.Documents.Open(Filepath)

    To Create\Add new Word Document

  • To Create or Add new Word Document - use below mentioned code
  • Dim NewWDoc As Word.Document
    Set NewWDoc = WApp.Documents.Add

     

    Find the Paragraphs Count in a word document:

  • wdoc.Paragraphs.Count
  •  

    Copy the data from one word document to another

    WDoc.Range.Copy
    NewWDoc.Range.PasteAndFormat wdFormatOriginalFormatting

    Define a Paragraph as Range

  • To define a paragraph as range use below mentioned code
  • Set Rng = WDoc.Range(WDoc.Paragraphs(1).Range.Start, WDoc.Paragraphs(1).Range.End - 1)

    Copy The data from Excel to end of word document

  • Copy the Range("A1") cell's data at the end of word document
  • ActiveSheet.Range("A1").Copy
    WDoc.Paragraphs(WDoc.Paragraphs.Count).Range.Paste

     

    To add Blank Lines

  • WDoc.Paragraphs.Add
  • Loop All the words In a Paragraph

    For Wrd = 1 To WDoc.Paragraphs(2).Range.Words.Count
    MsgBox WDoc.Paragraphs(2).Range.Words(Wrd)
    Next

    Define the Range in a paragraph based on words

    If WDoc.Paragraphs(2).Range.Words(Wrd) = "Begin Here" Then
    StartRng = WDoc.Paragraphs(2).Range.Words(Wrd).Start
    End If

    If WDoc.Paragraphs(2).Range.Words(Wrd) = "End Here" Then
    EndRng = WDoc.Paragraphs(2).Range.Words(Wrd).End
    End If

    Set Rng = WDoc.Range(StartRng, EndRng)

     

    Define the Paragraph and Range

  • First define the Paragraph as Object
  • Next apply the font properties on Range
  • Dim Pgraph As Paragraph
    Set Pgraph = WDoc.Paragraphs(3)
    Pgraph.Alignment = WdParagraphAlignment.wdAlignParagraphLeft
    With Pgraph.Range
    .Font.Name = "Calibri"
    .Font.Size = 12
    .Font.ColorIndex = wdBlue
    .Font.UnderlineColor = wdColorBrightGreen
    End With