Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   Dev Hardware ForumsSOFTWAREProgramming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Hardware Forums Sponsor:
  Trader Rating: 0 · #1  
Old June 6th, 2008, 01:58 AM
arod arod is offline
n00b DevH'er
Dev Hardware Newbie (0 - 499 posts)
 
Join Date: Mar 2008
Posts: 9 arod User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 5 m 56 sec
Reputation Power: 0
Java - [or VB] automated search and replacing of text string in a multiple word documents

I have a number of different word documents which I use a template title pages for reports I write.
These templates are kept in a template folder. When I am working on a report, I copy each of these files into the report folder, and change the title on each of these pages to reflect the report I am writing.

For various reasons these individual title pages cannot be combined into one file.

I am wondering if it is possible to automate this using some kind of batch/wsh programming.
I would envisage it working something like this:
-each individual title page document in the template folder will have the text 'insert title here' in the relevant place within the document that I want the title to be

-I run the batch/wsh program, which asks me what I want the title to be. It then searches for the string 'insert title here' in each of the documents, replaces it with the new text

-It then saves each file with a new file name so it doesn't get mixed up with the template files

-I can then copy them into my report folder

My question is: does anyone know if something like this is possible?
Thanks

Reply With Quote
  Trader Rating: 0 · #2  
Old June 6th, 2008, 02:03 AM
arod arod is offline
n00b DevH'er
Dev Hardware Newbie (0 - 499 posts)
 
Join Date: Mar 2008
Posts: 9 arod User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 5 m 56 sec
Reputation Power: 0
Sorry, that should read DOS Batch rather then Java in my title.
It doesn't appear as though I can change that...if a moderator or someone with the power could do so for me it would be much appreciated.

Reply With Quote
  Trader Rating: 0 · #3  
Old June 6th, 2008, 03:56 AM
bob_man_uk bob_man_uk is offline
Stressed to the nines
Dev Hardware Frequenter (2500 - 2999 posts)
 
Join Date: Sep 2004
Location: in the middle of nowhere important
Posts: 2,788 bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)  Folding Points: 861146 Folding Title: Super Ultimate Folder - Level 2Folding Points: 861146 Folding Title: Super Ultimate Folder - Level 2Folding Points: 861146 Folding Title: Super Ultimate Folder - Level 2Folding Points: 861146 Folding Title: Super Ultimate Folder - Level 2Folding Points: 861146 Folding Title: Super Ultimate Folder - Level 2Folding Points: 861146 Folding Title: Super Ultimate Folder - Level 2Folding Points: 861146 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 1 Month 1 Day 5 h 5 m 11 sec
Reputation Power: 4045
Send a message via ICQ to bob_man_uk Send a message via AIM to bob_man_uk Send a message via MSN to bob_man_uk Send a message via Yahoo to bob_man_uk Send a message via Google Talk to bob_man_uk Send a message via Skype to bob_man_uk Send a message via XFire to bob_man_uk
Bebo Facebook Orkut
nilpo's the man for the job but just to get you started you really should search the forums...

http://www.devhardware.com/forums/p...+string+replace

If push comes to shove, search devshed's forums.
Comments on this post
Nilpo agrees: Thank you.
__________________
Join IRC NOW!! irc.goodchatting.com

Reply With Quote
  Trader Rating: 1 · #4  
Old June 6th, 2008, 10:38 AM
weevilofdoom's Avatar
weevilofdoom weevilofdoom is offline
BLURG
Dev Hardware Loyal (3000 - 3499 posts)
 
Join Date: Sep 2004
Location: Oregon
Posts: 3,438 weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)  Folding Points: 32443 Folding Title: Starter FolderFolding Points: 32443 Folding Title: Starter Folder
Time spent in forums: 2 Months 1 Day 17 h 27 m 49 sec
Reputation Power: 3596
Send a message via ICQ to weevilofdoom Send a message via AIM to weevilofdoom Send a message via MSN to weevilofdoom Send a message via Yahoo to weevilofdoom Send a message via Google Talk to weevilofdoom
if I recall correctly, MS Word documents are written in a binary format - so you do need a language capable of reading/writing binary files. But most languages do come with some sort of string library to do replacements of matched text.

in VB:

strTemp = Replace(string, whatToFind, replaceWith )
__________________

Reply With Quote
  Trader Rating: 0 · #5  
Old June 6th, 2008, 10:41 AM
bob_man_uk bob_man_uk is offline
Stressed to the nines
Dev Hardware Frequenter (2500 - 2999 posts)
 
Join Date: Sep 2004
Location: in the middle of nowhere important
Posts: 2,788 bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)  Folding Points: 861146 Folding Title: Super Ultimate Folder - Level 2Folding Points: 861146 Folding Title: Super Ultimate Folder - Level 2Folding Points: 861146 Folding Title: Super Ultimate Folder - Level 2Folding Points: 861146 Folding Title: Super Ultimate Folder - Level 2Folding Points: 861146 Folding Title: Super Ultimate Folder - Level 2Folding Points: 861146 Folding Title: Super Ultimate Folder - Level 2Folding Points: 861146 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 1 Month 1 Day 5 h 5 m 11 sec
Reputation Power: 4045
Send a message via ICQ to bob_man_uk Send a message via AIM to bob_man_uk Send a message via MSN to bob_man_uk Send a message via Yahoo to bob_man_uk Send a message via Google Talk to bob_man_uk Send a message via Skype to bob_man_uk Send a message via XFire to bob_man_uk
Bebo Facebook Orkut
new office documents are made in xml i believe... docx just fyi

Reply With Quote
  Trader Rating: 1 · #6  
Old June 6th, 2008, 10:43 AM
weevilofdoom's Avatar
weevilofdoom weevilofdoom is offline
BLURG
Dev Hardware Loyal (3000 - 3499 posts)
 
Join Date: Sep 2004
Location: Oregon
Posts: 3,438 weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)  Folding Points: 32443 Folding Title: Starter FolderFolding Points: 32443 Folding Title: Starter Folder
Time spent in forums: 2 Months 1 Day 17 h 27 m 49 sec
Reputation Power: 3596
Send a message via ICQ to weevilofdoom Send a message via AIM to weevilofdoom Send a message via MSN to weevilofdoom Send a message via Yahoo to weevilofdoom Send a message via Google Talk to weevilofdoom
ya, beh!! still using office 2003 when i have to use windows... XML Parsing is quite a bit more difficult, FYI than parsing straight text ...
Comments on this post
Nilpo agrees: Trying to edit Word docs directly (as binary or as XML) would be far too difficult.

Reply With Quote
  Trader Rating: 2 · #7  
Old June 10th, 2008, 01:47 AM
Nilpo's Avatar
Nilpo Nilpo is offline
Dev Hardware God (5000 - 5499 posts)
 
Join Date: May 2004
Location: Salem, OH
Posts: 5,374 Nilpo User rank is General 74th Grade (Above 100000 Reputation Level)Nilpo User rank is General 74th Grade (Above 100000 Reputation Level)Nilpo User rank is General 74th Grade (Above 100000 Reputation Level)Nilpo User rank is General 74th Grade (Above 100000 Reputation Level)Nilpo User rank is General 74th Grade (Above 100000 Reputation Level)Nilpo User rank is General 74th Grade (Above 100000 Reputation Level)Nilpo User rank is General 74th Grade (Above 100000 Reputation Level)Nilpo User rank is General 74th Grade (Above 100000 Reputation Level)Nilpo User rank is General 74th Grade (Above 100000 Reputation Level)Nilpo User rank is General 74th Grade (Above 100000 Reputation Level)Nilpo User rank is General 74th Grade (Above 100000 Reputation Level)Nilpo User rank is General 74th Grade (Above 100000 Reputation Level)Nilpo User rank is General 74th Grade (Above 100000 Reputation Level)Nilpo User rank is General 74th Grade (Above 100000 Reputation Level)Nilpo User rank is General 74th Grade (Above 100000 Reputation Level)Nilpo User rank is General 74th Grade (Above 100000 Reputation Level)  Folding Points: 199538 Folding Title: Super Ultimate Folder - Level 1Folding Points: 199538 Folding Title: Super Ultimate Folder - Level 1Folding Points: 199538 Folding Title: Super Ultimate Folder - Level 1Folding Points: 199538 Folding Title: Super Ultimate Folder - Level 1Folding Points: 199538 Folding Title: Super Ultimate Folder - Level 1Folding Points: 199538 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 1 Month 1 Week 3 Days 10 h 9 m 13 sec
Reputation Power: 5908
Send a message via ICQ to Nilpo Send a message via AIM to Nilpo Send a message via MSN to Nilpo Send a message via Yahoo to Nilpo Send a message via Google Talk to Nilpo Send a message via Skype to Nilpo Send a message via XFire to Nilpo
MySpace Orkut
Quote:
Originally Posted by bob_man_uk
nilpo's the man for the job but just to get you started you really should search the forums...
Well, it seems that bob_man_uk has given some pretty good advice. Sorry I didn't see this thread sooner.

The fact is that you CAN do this in WSH pretty easily. I just need to know a few things.

What are the file paths and names of the templates that you want to change?

What is the exact text string that you want to change? (I would recommend something like "[insert title here]" (without the quotes, of course) or that you make them actual form fields. The latter is the most reliable method.

If possible, it would be great if you could post a copy of the templates you're using so that I can see exactly what you're working with.

I'd be happy to put a script together for you to play with.
__________________
Click the image if at any point you don't like my decision.

10011100011000101111100011100000011000000100000011 10010011011110110001101101011011110100011000001110 0100111101000100001

Visit Nilpo.com and Ask the Windows Guru!

Open me for some very useful links!    

Reply With Quote
  Trader Rating: 1 · #8  
Old June 10th, 2008, 10:34 AM
weevilofdoom's Avatar
weevilofdoom weevilofdoom is offline
BLURG
Dev Hardware Loyal (3000 - 3499 posts)
 
Join Date: Sep 2004
Location: Oregon
Posts: 3,438 weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)weevilofdoom User rank is General 39th Grade (Above 100000 Reputation Level)  Folding Points: 32443 Folding Title: Starter FolderFolding Points: 32443 Folding Title: Starter Folder
Time spent in forums: 2 Months 1 Day 17 h 27 m 49 sec
Reputation Power: 3596
Send a message via ICQ to weevilofdoom Send a message via AIM to weevilofdoom Send a message via MSN to weevilofdoom Send a message via Yahoo to weevilofdoom Send a message via Google Talk to weevilofdoom
Actually, dealing with binary files is easy as shitting in the morning after some colon blow.

Code:
Dim file_name As String
Dim file_length As Long
Dim fnum As Integer
Dim bytes() As Byte

    file_name = "/path/to/somefile.doc"
    file_length = FileLen(file_name)

    fnum = FreeFile
    ReDim bytes(1 To file_length)

    Open file_name For Binary As #fnum
    Get #fnum, 1, bytes
    Close fnum


The "bytes" array now contains all file information. Work with it as you normally would, with a "replace" etc ... just don't forget to "Format$" the byte array

It's even really easy in C++ but that is out of scope for this thread.

For writing file, reverse your logic and change "Get" to "Put".

Last edited by weevilofdoom : June 10th, 2008 at 10:39 AM.

Reply With Quote
  Trader Rating: 0 · #9  
Old July 9th, 2008, 08:57 AM
arod arod is offline
n00b DevH'er
Dev Hardware Newbie (0 - 499 posts)
 
Join Date: Mar 2008
Posts: 9 arod User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 5 m 56 sec
Reputation Power: 0
Quote:
Originally Posted by Nilpo
Well, it seems that bob_man_uk has given some pretty good advice. Sorry I didn't see this thread sooner.

The fact is that you CAN do this in WSH pretty easily. I just need to know a few things.

What are the file paths and names of the templates that you want to change?

What is the exact text string that you want to change? (I would recommend something like "[insert title here]" (without the quotes, of course) or that you make them actual form fields. The latter is the most reliable method.

If possible, it would be great if you could post a copy of the templates you're using so that I can see exactly what you're working with.

I'd be happy to put a script together for you to play with.


Thanks for your reply Nilpo, apologies for not getting back to this earlier.

All the templates are located in C:\working. The names of the title pages are titlepage1.doc, titlepage2.doc through to titlepage5.doc.

The text I want to replace is exactly as you said, 'insert text here'.
I would post an example of a title page but I don't see an option of including attachments to my post.

If you would be able to put together some script for me to play with that would be great. My VB knowledge is very limited, but I can have a crack at it.
Would you recommend something like what weevilofdoom has posted?

Thanks

Reply With Quote
  Trader Rating: 2 · #10  
Old July 11th, 2008, 02:46 PM
Nilpo's Avatar
Nilpo Nilpo is offline
Dev Hardware God (5000 - 5499 posts)
 
Join Date: May 2004
Location: Salem, OH
Posts: 5,374 Nilpo User rank is General 74th Grade (Above 100000 Reputation Level)Nilpo User rank is General 74th Grade (Above 100000 Reputation Level)Nilpo User rank is General 74th Grade (Above 100000 Reputation Level)Nilpo User rank is General 74th Grade (Above 100000 Reputation Level)Nilpo User rank is General 74th Grade (Above 100000 Reputation Level)Nilpo User rank is General 74th Grade (Above 100000 Reputation Level)Nilpo User rank is General 74th Grade (Above 100000 Reputation Level)Nilpo User rank is General 74th Grade (Above 100000 Reputation Level)Nilpo User rank is General 74th Grade (Above 100000 Reputation Level)Nilpo User rank is General 74th Grade (Above 100000 Reputation Level)Nilpo User rank is General 74th Grade (Above 100000 Reputation Level)Nilpo User rank is General 74th Grade (Above 100000 Reputation Level)Nilpo User rank is General 74th Grade (Above 100000 Reputation Level)Nilpo User rank is General 74th Grade (Above 100000 Reputation Level)Nilpo User rank is General 74th Grade (Above 100000 Reputation Level)Nilpo User rank is General 74th Grade (Above 100000 Reputation Level)  Folding Points: 199538 Folding Title: Super Ultimate Folder - Level 1Folding Points: 199538 Folding Title: Super Ultimate Folder - Level 1Folding Points: 199538 Folding Title: Super Ultimate Folder - Level 1Folding Points: 199538 Folding Title: Super Ultimate Folder - Level 1Folding Points: 199538 Folding Title: Super Ultimate Folder - Level 1Folding Points: 199538 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 1 Month 1 Week 3 Days 10 h 9 m 13 sec
Reputation Power: 5908
Send a message via ICQ to Nilpo Send a message via AIM to Nilpo Send a message via MSN to Nilpo Send a message via Yahoo to Nilpo Send a message via Google Talk to Nilpo Send a message via Skype to Nilpo Send a message via XFire to Nilpo
MySpace Orkut
The code that weevilofdoom posted will work. The problem is that it only works in full VB and not in VBScript which is what we're limited to using unless you wanted to compile an application.

VBScript is just a subset of Visual Basic and binary processing happens to be one of the parts that was left out. There are a few workarounds that make it possible, but there's no point playing with those when we can handle this problem directly, in my opinion.

This code will open Microsoft Word and use Word's own Find and Replace feature to do the trick.
vb Code: