Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add text to a word document

Status
Not open for further replies.

Geates

Programmer
Aug 25, 2009
1,566
US
I have a document that outlines the version history of our logon script. Every so often, I tweak our logon script and I record the changes in a few locations. I would like to automate updating our document. About half way down the document is where the version history is recorded in a bullet-point list (without the bullet-points). I want to add a new item at the top of that list.

So far I've been able to sort find where to begin inserting text.

Code:
set objArgs = WScript.Arguments
set objWord = CreateObject("Word.Application")

objWord.Visible = false
objWord.Documents.Open objArgs(0)

set objSelection = objWord.ActiveDocument.Content 

with objSelection.Find
	.Text = "4. Version History"
	.Execute
end with

I want to move to the top of the list and insert a new item but am not sure about the best way to go about it. Any suggestions?

This is the result I am looking for:

Code:
[b]4. Version History[/b]

    [red]3.0.11
    This is a new item[/red]

    3.0.10
    The Net Learning icon shows a message to the user that it is no longer available.

    3.0.9
    Remove NetCompetency link

    ...

-Geates

 
Well, what is the VBA code generated by the macrorecorder when you do that manually ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Try something based on:
Code:
objWord.Visible = False
Set objDoc = objWord.Documents.Open(objArgs(0))
With objDoc.Range
  With .Find
    .Text = "4. Version History^p"
    .Execute
  End With
  If .Find.Found = True Then
    With .Duplicate
      .Collapse 0 '0 = wdCollapseEnd
      While Len(.Paragraphs.Last.Range.Text) = 1
        .MoveEnd 4, 1 '4 = wdParagraph
      Wend
      .InsertBefore vbCr & "3.0.12" & vbCr & "This is another new item" & vbCr
    End With
  End If
End With

Cheers
Paul Edstein
[MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top