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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to write to a word document?

Status
Not open for further replies.

Deeppurple

Programmer
Jan 17, 2003
16
IN
Hi all,

How to write to a document at a particular position? My requirement is, say if user has selected few words on his document,- "this is very beautiful"-, then i want to add few characters before and after this selection,-"abc 'this is very beautiful' abc"-,something like this. How can i do this. How can I position my cursor before the sentance and write the text?

Any hints, or sample code would be very useful

-TIA
 
Hiya,

you might try the following bit of code - it's based off the premise that the user has already selected the text you need to change:

Code:
    Dim l_selSelectedText As Selection
    Dim l_docTest As Document
    
    Set l_docTest = ActiveDocument
    Set l_selSelectedText = Application.Windows(l_docTest.Name).Selection
    'Add text before / after the selected text
    l_selSelectedText.Text = "abc" & l_selSelectedText.Text & "abc"
    'release objects
    Set l_selSelectedText = Nothing
    Set l_docTest = Nothing

If they haven't selected any text - just use find&replace

HTH

Cheers
Nikki
 
Here's a star for Nikki... for a good answer _and_ for cleaning up the objects. :)
 
Hi nikki,
Thanks for the reply. But it doesn't work if selection includes any image or table. How to handle those scenarios?

-regards
 
Oops - figured it'd always be text. I've adjusted the code - it's a bit of a naff solution BUT will always work. It cuts the selected text, adds the text you want to add, then pastes the original text (+ any tab;les, pictures or other objects!) back the way they were ...

Code:
    Dim l_selSelectedText As Selection
    Dim l_docTest As Document
    Dim l_sTextToAdd As String
    
    Set l_docTest = ActiveDocument
    Set l_selSelectedText = Application.Windows(l_docTest.Name).Selection
    l_sTextToAdd = "abc"
    
    'Add text before / after the selected text
    'Bit naff but this'll always work ;-)
    'adds 2 spaces between the twoe strings added, and one after the last string
    'that's just to neaten up
    l_selSelectedText.Cut
    Selection.TypeText l_sTextToAdd & "  " & l_sTextToAdd & " "
    Selection.MoveLeft wdCharacter, 4
    Selection.Paste
    
    'release objects
    Set l_selSelectedText = Nothing
    Set l_docTest = Nothing

Hope that clears things up

Cheers!
Nikki

and thanks for the star euskadi - much appreciated
 
Here's another way to do something similar:

set s1=selection.Range
set s2=selection.Range
s1.collapse
s2.collapse wdcollapseend
s1.select
selection.typetext "abc"
s2.select
selection.typetext "def"

Rob
[flowerface]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top