VBA Word - Sample Code

carsRST
carsRST used Ask the Experts™
on
Looking for 2003 Word VBA example to do the following:

1.  Open an existing Word document
2.  On the 5th line down write "Line 5"
             -Font Size: 14
             -Font: Arial
3.  On line 6 write "Line 6" with no format

Please note the code will be done from OUTSIDE word so don't assume any implied object references.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
GrahamSkanRetired
Top Expert 2012

Commented:
All text must have some formatting. This ensure that Line 5 has the same font formatting as the line above.

You seem to be aware of Referencing. If you are in another Office application, you only need to add the Microsoft Word Object library.
Sub AddText()
    Dim WdDoc As Word.Document
    Dim wdApp As Word.Application
    Dim bNewInstance As Boolean
    Dim rng  As Range
    
    On Error Resume Next 'supress error for next line
    Set wdApp = GetObject(, "Word.Application")
    On Error GoTo 0
    If wdApp Is Nothing Then
        Set wdApp = CreateObject("Word.Application")
        bNewInstance = True
    End If
    wdApp.Visible = True
    
    Set WdDoc = wdApp.Documents.Open("C:\MyFolder\test.doc")
    
    Set rng = WdDoc.Range.Paragraphs(4).Range
    rng.Collapse wdCollapseEnd
    rng.Text = "Line 6" & vbCr
    rng.Collapse wdCollapseStart
    rng.Text = "Line 5" & vbCr
    rng.Font.Name = "Arial"
    rng.Font.Size = 14
    
    'WdDoc.Close wdSaveChanges
    'If bNewInstance Then
        'wdApp.Quit
    'End If


End Sub

Open in new window

Author

Commented:
Thanks.

I'm getting this error...

'5941' the requested member of the collection does not

on this line...

 Set rng = WdDoc.Range.Paragraphs(4).Range
Retired
Top Expert 2012
Commented:
That's because there aren't enough paragraphs (lines) to add a fifth and a sixth. I inferred from your request that there would be. This code will add empty paragraphs until there are.
Sub AddText()
    Dim WdDoc As Word.Document
    Dim wdApp As Word.Application
    Dim bNewInstance As Boolean
    Dim rng  As Range
    
    On Error Resume Next 'supress error for next line
    Set wdApp = GetObject(, "Word.Application")
    On Error GoTo 0
    If wdApp Is Nothing Then
        Set wdApp = CreateObject("Word.Application")
        bNewInstance = True
    End If
    wdApp.Visible = True
    
    Set WdDoc = wdApp.Documents.Open("C:\MyFolder\test.doc")
    
    Do While WdDoc.Range.Paragraphs.Count < 5
        WdDoc.Bookmarks("\EndOfDoc").Range.Text = vbCr
    Loop
    
    Set rng = WdDoc.Range.Paragraphs(4).Range
    rng.Collapse wdCollapseEnd
    rng.Text = "Line 6" & vbCr
    rng.Collapse wdCollapseStart
    rng.Text = "Line 5" & vbCr
    rng.Font.Name = "Arial"
    rng.Font.Size = 14
    
    'WdDoc.Close wdSaveChanges
    'If bNewInstance Then
        'wdApp.Quit
    'End If


End Sub

Open in new window

Author

Commented:
>>I inferred from your request that there would be.

sorry - i didn't know it would matter.   I should have been specific.  


Thanks so much for your help!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial