Sign in to follow this  
Followers 0
oxb

Noob needs help vbs script change text in word .doc

59 posts in this topic

Hi all

For my business i need a script to change certain text in a word .doc

In my own way i have made a small batch script that uses user input to make a directory and .doc filename with the name the user gave.

I have got a office.document that is copied over with the name the user gave in the folder the user specified. (this document is always the same is copied over with name user gave)

after creation the document is opened so the user can fill in the document.

After creation of the document offerte_example.doc i need the script to find the offerte_example.doc file (only .doc file in folder) take its name en copie to factuur_example.doc, (offerte_example.doc needs to stay!)

after that change some text to another part of text. (text is always the same only needs to be changed to different text)

The script itself wil be in the same folder as the example.doc file.

Hope i made myself clear what i want .

I am not a coder so please be patient.

Youre help is greatly appriciated.

Thanx in advance.

0

Share this post


Link to post
Share on other sites

I am lost as to what you want, do you want to copy a file and rename factuur_example.doc to factuur_offerte_example.doc

It would also help if you post any code that you are using.

0

Share this post


Link to post
Share on other sites

I want the offerte_example.doc copied to factuur_example.doc

Then i want to edit some standard text thats always present in offerte_example.doc wich is now filled in with the users text (wich wil stay there)

As offerte_example.doc is copied to factuur_example.doc this wil be a exact copy But a few standard lines need to be changed.

Dont know how to put it in better words.

I found a script that will change the lines of text, but as i dont know how the user will name the file i dont know how to pass the filename to the script.

The file also needs to be copied as its a file with cel en table`s dos wont do the trick.

The file below does not copy, i need copy and replace

i will post the script below

Const wdReplaceAll = 2

Set oWord = CreateObject("Word.Application")

oWord.Visible = False

set oDoc = oWord.Documents.Open("dont know what to put here")

With oWord.Selection

.Find.Text = "Offerte:"

.Find.Replacement.Text = "factuur:"

.Find.Forward = True

.Find.MatchWholeWord = True

.Find.Execute ,,,,,,,,,,wdReplaceAll

.Find.Text = "Na eventuele accordatie stellen wij betaling per pin op prijs."

.Find.Replacement.Text = "Wij danken u voor uw opdracht, graag betaling via PIN"

.Find.Forward = True

.Find.MatchWholeWord = True

.Find.Execute ,,,,,,,,,,wdReplaceAll

End With

oDoc.Save

oDoc.Close

oWord.Quit

Edited by oxb
0

Share this post


Link to post
Share on other sites

You know it would be better to use VBS script to get the user input and then pass that varible.

Example


Dim UserInput
GetUserInput()

Function GetUserInput()
Do
UserInput = InputBox(_
"Text With Details About User Input Required" & vbCrLf & vbCrLf &_
"To Close And Do Nothing, Type Exit Or Quit","","",6500,5500)
If InStr(1,UserInput,"exit",1) And Len(UserInput) = 4 Or _
InStr(1,UserInput,"quit",1) And Len(UserInput) = 4 Then
WScript.Quit
End if
If Not UserInput = "" And Len(UserInput) >= 0 Then
'-> User Input
MsgBox "User Input = " & UserInput ,4128,"Input Return"
Exit Do
End if
Loop Until Len(Input) = 1000
End Function

0

Share this post


Link to post
Share on other sites

Maybe relevant, maybe not, please consider how a Word document contains some metadata (including name it was last saved with - as an example) which may need to be updated.

What you posted seems not like VBS (Visual Basic Script language) but rather VBA (Visual Basic for Applications).

Have you considered leveraging on Word built-in features, such as Mail Merge?

jaclaz

0

Share this post


Link to post
Share on other sites

Thanx for the info, like i said big noob when it comes to coding.

Mail merge never heard of will look into that and will try the code from gunsmokingman (first see if i can understand what the code does :).

First question how do i pass the userinput in the code gunsmokingman provided to the vba script?

Again noob question.

If possible would one of you edit the code i already have so that the user input is passed to the vba? script.

Thanx again for your time and help.

Edited by oxb
0

Share this post


Link to post
Share on other sites

UserInput in the script is a varible that can be passed to other parts of the script.

Example


Dim UserInput
GetUserInput()

Function GetUserInput()
Do
UserInput = InputBox(_
"Text With Details About User Input Required" & vbCrLf & vbCrLf &_
"To Close And Do Nothing, Type Exit Or Quit","","",6500,5500)
If InStr(1,UserInput,"exit",1) And Len(UserInput) = 4 Or _
InStr(1,UserInput,"quit",1) And Len(UserInput) = 4 Then
WScript.Quit
End if
If Not UserInput = "" And Len(UserInput) >= 0 Then
'-> User Input Passed To The Function WordDocument
WordDocument(UserInput)
Exit Do
End if
Loop Until Len(Input) = 1000
End Function

Function WordDocument(Name)
Const wdReplaceAll = 2
Set oWord = CreateObject("Word.Application")
oWord.Visible = False
set oDoc = oWord.Documents.Open(Name & ".doc")
With oWord.Selection
.Find.Text = "Offerte:"
.Find.Replacement.Text = "factuur:"
.Find.Forward = True
.Find.MatchWholeWord = True
.Find.Execute ,,,,,,,,,,wdReplaceAll
.Find.Text = "Na eventuele accordatie stellen wij betaling per pin op prijs."
.Find.Replacement.Text = "Wij danken u voor uw opdracht, graag betaling via PIN"
.Find.Forward = True
.Find.MatchWholeWord = True
.Find.Execute ,,,,,,,,,,wdReplaceAll
End With
oDoc.Save
oDoc.Close
oWord.Quit
End Function

The above code passes UserInput which becomes the new varibles Name in the function WordDocument

0

Share this post


Link to post
Share on other sites

Loosely mail merge is traditionally used to manage "circular letters", i.e. printing a "same" letter in several copies with addresses taken from a database.

But in it's essence is a way to insert into a document "variable fields" that are "populated" or assigned values by a lookup in a database (or spreadsheet).

This may be suitable to your scopes (or it may be not).

jaclaz

0

Share this post


Link to post
Share on other sites

@jaclaz

i read up about mailmerge but its not suitable for me.

Thanx for the input though.

@gunsmokingman

Thanx for the script but when i run the script i get the message File not found?

I know for sure the file is there.

I am lost now dont know how to proceed, strange the file is present, name is passed on fine but no luck.

EDIT my guess is that the line set oDoc = oWord.Documents.Open (name & ".doc") is missing the full path to the directory.

The script opens word in the background and opens the requested file but i think (correct me if i am wrong) that word needs the full path,

or does it run in the same folder where the vbs file was started?

Thanx again

Edited by oxb
0

Share this post


Link to post
Share on other sites

set oDoc = oWord.Documents.Open(Name & ".doc")

How are you naming it? The script was just an example I never tested it for runtime errors.

0

Share this post


Link to post
Share on other sites

SUCCES

As mentioned earlier the path was missing!

Thank you very much Gunsmokingman im a happy camper now.

Greetz from Holland

Ended up with this vbs file

currentDirectory = left(WScript.ScriptFullName,(Len(WScript.ScriptFullName))-(len(WScript.ScriptName)))

WScript.Echo currentDirectory

input = Inputbox("Enter filename")

Const wdReplaceAll = 2

Set oWord = CreateObject("Word.Application")

oWord.Visible = false

set oDoc = oWord.Documents.Open (currentdirectory & input & ".doc")

With oWord.Selection

.Find.Text = "Offerte:"

.Find.Replacement.Text = "factuur:"

.Find.Forward = True

.Find.MatchWholeWord = True

.Find.Execute ,,,,,,,,,,wdReplaceAll

.Find.Text = "Na eventuele accordatie stellen wij betaling per pin op prijs."

.Find.Replacement.Text = "Wij danken u voor uw opdracht, graag betaling via PIN"

.Find.Forward = True

.Find.MatchWholeWord = True

.Find.Execute ,,,,,,,,,,wdReplaceAll

End With

oDoc.Save

oDoc.Close

oWord.Quit

0

Share this post


Link to post
Share on other sites

It always good practice when using VBS to include checks to prevents errors. Your script will

still run even if the Inputbox is blank, the cancel being pressed, the X being pressed, that

would caused it to error out.

Another way to get CurrentDirectory=Replace(WScript.ScriptFullName,"\"&WScript.ScriptName,"")

0

Share this post


Link to post
Share on other sites

It always good practice when using VBS to include checks to prevents errors. Your script will

still run even if the Inputbox is blank, the cancel being pressed, the X being pressed, that

would caused it to error out.

Another way to get CurrentDirectory=Replace(WScript.ScriptFullName,"\"&WScript.ScriptName,"")

Noted wil inplement it.

I have accoplished with youre help what i tried to do, however i got one big batch file wich does take user input create dir copy files etc.

What i would really like to do is take the commands in the batch ad make it run onder vb.

Like i said everything working fine now, only thing is that my batch could look better in vb, and couldt be less messy.

If you have some time to spare please take a look at my batch DONT LAUGH :)

Maybe you got a shorter solution to my situation.

Again it works but maybe a little messy.

Thanx again.

@echo off

cls

rem PUSHD network Path to be filled in later

Echo Geef alfabetische letter en druk op enter!

choice /C:abcdefghijklmnopqrstuvwxyz /N >NUL

if '%errorlevel%'=='1' copy offerte.doc a & copy factuur.vbs a & cd a & Set directory=a

if '%errorlevel%'=='2' copy offerte.doc b & copy factuur.vbs b & cd b & Set directory=b

if '%errorlevel%'=='3' copy offerte.doc c & copy factuur.vbs c & cd c & Set directory=c

if '%errorlevel%'=='4' copy offerte.doc d & copy factuur.vbs d & cd d & Set directory=d

if '%errorlevel%'=='5' copy offerte.doc e & copy factuur.vbs e & cd e & Set directory=e

if '%errorlevel%'=='6' copy offerte.doc f & copy factuur.vbs f & cd f & Set directory=f

if '%errorlevel%'=='7' copy offerte.doc g & copy factuur.vbs g & cd g & Set directory=g

if '%errorlevel%'=='8' copy offerte.doc h & copy factuur.vbs h & cd h & Set directory=h

if '%errorlevel%'=='9' copy offerte.doc i & copy factuur.vbs i & cd i & Set directory=i

if '%errorlevel%'=='10' copy offerte.doc j & copy factuur.vbs j & cd j & Set directory=j

if '%errorlevel%'=='11' copy offerte.doc k & copy factuur.vbs k & cd k & Set directory=k

if '%errorlevel%'=='12' copy offerte.doc l & copy factuur.vbs l & cd l & Set directory=l

if '%errorlevel%'=='13' copy offerte.doc m & copy factuur.vbs m & cd m & Set directory=m

if '%errorlevel%'=='14' copy offerte.doc n & copy factuur.vbs n & cd n & Set directory=n

if '%errorlevel%'=='15' copy offerte.doc o & copy factuur.vbs o & cd o & Set directory=o

if '%errorlevel%'=='16' copy offerte.doc p & copy factuur.vbs p & cd p & Set directory=p

if '%errorlevel%'=='17' copy offerte.doc q & copy factuur.vbs q & cd q & Set directory=q

if '%errorlevel%'=='18' copy offerte.doc r & copy factuur.vbs r & cd r & Set directory=r

if '%errorlevel%'=='19' copy offerte.doc s & copy factuur.vbs s & cd s & Set directory=s

if '%errorlevel%'=='20' copy offerte.doc t & copy factuur.vbs t & cd t & Set directory=t

if '%errorlevel%'=='21' copy offerte.doc u & copy factuur.vbs u & cd u & Set directory=u

if '%errorlevel%'=='22' copy offerte.doc v & copy factuur.vbs v & cd v & Set directory=v

if '%errorlevel%'=='23' copy offerte.doc w & copy factuur.vbs w & cd w & Set directory=w

if '%errorlevel%'=='24' copy offerte.doc x & copy factuur.vbs x & cd x & Set directory=x

if '%errorlevel%'=='25' copy offerte.doc y & copy factuur.vbs y & cd y & Set directory=y

if '%errorlevel%'=='26' copy offerte.doc z & copy factuur.vbs z & cd z & Set directory=z

set cd=%cd&

set newfolder=

set /p newfolder=Achternaam ZONDER SPATIE wat wel kan Van_Zanten dus met underscore!!!:

if {%newfolder%}=={} goto :end

mkdir %newfolder%

move factuur.vbs %newfolder%

move offerte.doc %newfolder%

cd %newfolder%

rename offerte.doc %newfolder%.doc

start %newfolder%.doc

cd..

endlocal

exit

Probably already got a smile on youre face, dont you?

Greets Oscar

Edited by oxb
0

Share this post


Link to post
Share on other sites

1:\ Please note I do not have any experience at Networks, I only have one computer

2:\ This script is to replace choice /C:abcdefghijklmnopqrstuvwxyz /N >NUL

3:\ Tested this on my local drives and it copies the 2 files

In regards to Number 1 you will have read up on how to use VBS to connect to

another computer.

'-> This code is property of Gunsmokingman and Or Jake1Eye and you must have his permission to use.'-> This is only posted as example code and meant only to used as such.'-> Run Time ObjectsDim Act :Set Act = CreateObject("Wscript.Shell")Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")'-> Runtime VariblesDim Drv'-> Check To Make Both Files ExistsIf Fso.FileExists("offerte.doc") And Fso.FileExists("factuur.vbs") ThenGetDrvLtr()ElseMsgBox "Error Missing the offerte.doc or factuur.vbs."WScript.Quit()End IfFunction GetDrvLtr()'-> Loop To Keep Inputbox ActiveDo'-> Get Drive LetterDrv = InputBox(_"Type In The Drive Letter You Want To Use" & vbCrLf & vbCrLf &_"To Close And Do Nothing, Type Exit Or Quit","","",6500,5500)'-> Quit Or Exit ScriptIf InStr(1,Drv,"exit",1) And Len(Drv) = 4 Or _InStr(1,Drv,"quit",1) And Len(Drv) = 4 ThenWScript.QuitEnd ifIf Not IsNumeric(Drv) And Len(Drv) = 1 ThenIf Fso.DriveExists(Drv) ThenFso.CopyFile "offerte.doc", Drv & ":\"Fso.CopyFile "factuur.vbs", Drv & ":\"ElseMsgBox Drv & ":\ is missing. Type in another drive letter"GetDrvLtr()End IfExit DoEnd IfLoop Until Len(Drv) = 1000End Function
0

Share this post


Link to post
Share on other sites

Wow thats fast

What i forgot to say that the letters used are not driveletters but folders.

Each folder contains a offer with the name of the client (stored in a folder named after the client, taken with user iput)

So structure is

networkdrive> folder >a folder >Ajax (offerte.doc copied to folder ajax as ajax.doc)

So the user first gives in the correct folder letter a,b,c,d,..etc

Then in the chosen folder a,b,c..etc a directory wil be created (user input) and the files offerte.doc and factuur.vbs will be copied there.

Later on when the text need to be altered to a bill/invoice the user will start factuur.bat and the invoice is done.

Hope you understand my english is ok ,but dont know if offer is the right word for it.

As i said everything works so if its to much trouble dont bother, its just i think neater in vb.

only one question remains, in my dos batch the userinput cant have spaces in the filled in name like> john doe it has to be john_doe.

Thank you very much.

0

Share this post


Link to post
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
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.