|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|||
|
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 |
|
|||
|
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. |
|
|||
|
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.
__________________
Join IRC NOW!! irc.goodchatting.com |
|
|||
|
new office documents are made in xml i believe... docx just fyi
![]() |
|
||||
|
Quote:
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!
![]() |
|
||||
|
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. |
|
|||
|
Quote:
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 |
|
||||
|
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:
|