Jump to content

find and replace in word


Recommended Posts

Ii have wordfl, in that how can i find one particular set of continuous words and replace the word coming after that.

for example:

in my word doc, there are instances like "the filename is" appearing many times.

I have to replace the word coming after this sentance ie "the filename is"

So the file contains

"the filename is" aaa

"the filename is" bbb

"the filename is" kjhdfjdhf

I want to replace everything with a word aa.doc, so the result will be

"the filename is" aa.doc

"the filename is" aa.doc

"the filename is" aa.doc

can i do this by any means?? :idea:

Link to comment
Share on other sites

  • Moderators

obscurant1st,

This SRE seems to work: :idea:

$sText = "the filename is aaa and more text the filename is bbb and more text" & @CRLF & "the filename is kjhdfjdhf and more text"

$sCorrected = StringRegExpReplace($sText, "(?i)(?U)(the filename is (\b.+\b))", "the filename is aa.doc")

ConsoleWrite($sCorrected & @CRLF)

No doubt a real SRE guru will be along shortly with a much more snazzy version if you want to hang around. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

obscurant1st,

This SRE seems to work: :idea:

$sText = "the filename is aaa and more text the filename is bbb and more text" & @CRLF & "the filename is kjhdfjdhf and more text"

$sCorrected = StringRegExpReplace($sText, "(?i)(?U)(the filename is (\b.+\b))", "the filename is aa.doc")

ConsoleWrite($sCorrected & @CRLF)

No doubt a real SRE guru will be along shortly with a much more snazzy version if you want to hang around. :(

M23

But here in my case $sText is in a word document. So how can i get the contents to $sText? :)
Link to comment
Share on other sites

  • Moderators

obscurant1st,

Ever heard of "Save As..."? :)

Save the Word file as a .txt file and then read it into your script with FileRead. Once you have finished changing it, write teh result back to the file with FileWrite and then import into Word. :idea:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

obscurant1st,

Ever heard of "Save As..."? :)

Save the Word file as a .txt file and then read it into your script with FileRead. Once you have finished changing it, write teh result back to the file with FileWrite and then import into Word. :idea:

M23

But saving it as txt file will cause the formatting of my word file to loose right?

if not its ok.

Link to comment
Share on other sites

  • Moderators

obscurant1st,

saving it as txt file will cause the formatting of my word file to loose

Correct.

I can only suggest you do which ever the following you think would take the shortest time: :idea:

- 1. Use Word's built-in VB scripting to change the text. I imagine there are forums for that which can help you.

- 2. Go manually through the file and change the text.

- 3. Do as I suggested and reformat the file afterwards.

M23

P.S. When you reply please use the "Add Reply" button at the top and bottom of the page rather then the "Reply" button in the post itself. That way you do not get the contents of the previous post quoted in your reply and the whole thread becomes easier to read.

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • 3 months later...

its not solved, i am still trying. ;)

How about:

$oWordApp = _WordCreate ($Filename, 0, 0)
; Display the text within the document
    $sContent = $oWordApp.Activedocument.Range.Text
    
MsgBox(0, "Document Text", $sContent)

    _WordQuit ($oWordApp)

William

Edited by saywell
Link to comment
Share on other sites

Two good, related word groups are:

http://groups.yahoo.com/group/Word_VBA

http://groups.yahoo.com/group/Word_DocDesign

You'll be able to find out how to do what you need natively within word.

You may not need a macro, this might work:

Open the Find & replace dialog (CTRL+H)

Make sure the Use Wildcards, or use Pattern Matching box is ticked.

In the Find: box, enter

the filename is <*>

In the replace box, enter

the filename is aa.doc

"<" is the wildcard for start of a word, ">" is the end of a word, and "*" is any characters between those two - so that should select "the filename is " and whatever single word follows. It then replaces it with "the filename is aa.doc"

Make a backup of your document first, and check the output thoroughly!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...