Jump to content

Recommended Posts

Posted

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:

  • Moderators
Posted

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:

  Reveal hidden contents

 

Posted

  On 4/30/2010 at 8:09 AM, 'Melba23 said:

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? :)
  • Moderators
Posted

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:

  Reveal hidden contents

 

Posted

  On 4/30/2010 at 11:57 AM, 'Melba23 said:

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.

  • Moderators
Posted

obscurant1st,

  Quote

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:

  Reveal hidden contents

 

  • Moderators
Posted

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:

  Reveal hidden contents

 

Posted

I dont know is it solved or not, but you could use that example:

http://www.autoitscript.com/forum/index.php?showtopic=114123&st=0&p=798037&hl=Word&fromsearch=1&#entry798037

  • 3 months later...
Posted (edited)

  On 5/12/2010 at 9:54 AM, 'obscurant1st said:

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
Posted

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!

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
  • Recently Browsing   0 members

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