• We should now be fully online following an overnight outage. Apologies for any inconvenience, we do not expect there to be any further issues.

help writing vb script...

AznMaverick

Platinum Member
Apr 4, 2001
2,776
0
0
is there any way that i can write a script that would search for a start marker and end marker within a Microsoft Word document, make a selection between those two markers, copy what is selected, then save what was copyed on to a new document with a file name that matches the search phrase for the first marker with the current date? i would also want to do this multiple times within the document...basically seperating one document into several ones.

Example:

Code: 1200

adfasdfasfasdfasfsadfsdaf

This is the end.

Code 13000
adfasdfasdfasdfasdfsadfasdf

This is the end

would result in the following. first it would search for "Code: 1200" mark that as the beginning until it finds "This is th end." It would then save that selection to a file called "1200 current date.doc" then it would go on doing it for Code 13000...and so forth. is this possible?
 

WW

Golden Member
Jun 21, 2001
1,514
0
0
it's possible.....

use the markers to set up a range...copy that range to the new document, and use a loop to repeat until you can't find the marker again .

here's the code...change it to fit your exact text strings etc....make sure the markers are always the same for each text block...

start the macro with the cursor at the top of the data file for this example...you can modify it to open the file or whatever....

let me know if you have problems running it...

WW
 

AznMaverick

Platinum Member
Apr 4, 2001
2,776
0
0
thanks for the help, i got it to work with office 2000, but it doesn't seem to work with office 97...where would it save the file for 97? i know in 2000 i saves it in "My Documents"...

Again, i really appreciate the help, thanks!
 

WW

Golden Member
Jun 21, 2001
1,514
0
0
you're welcome.

you could make it save to any directory you want....

change

ActiveDocument.SaveAs FileName:=savename

to

ActiveDocument.SaveAs FileName:="c:\files\" + savename

(make sure you make a directory named c:\files)

I don't have office 97...let me know if it doesn't work...step through it in the editor (F8).

WW
 

AznMaverick

Platinum Member
Apr 4, 2001
2,776
0
0
thanks for your help, unfortunately it doesn't work in 97. but i think i'll just stick to office 2k for now. what i want to know now is how to save a certain string after a phrase to a variable and add what is saved to the variable to the filename. for instance...

if the document had
"Today's date is: 09/08/04" i would want to capture what comes after "Today's date is:" and add that to the filename (so 8 characters long, and i would need to get rid of the '/') is there any way to do this?

btw, how did you learn all this stuff? which books have you learned/referred to?

Thanks for the help!
 

AznMaverick

Platinum Member
Apr 4, 2001
2,776
0
0
OK, problem is solved and i modified your code to make things work. now last thing, is there any way that i can prompt a user for an input (with a finite set of choices in a drop down menu) which i would use to add to the filename...for instance

What do you want the filename of this file to be? drop down menu would have "choice 1" "choice 2" etc. then what ever they choose will be added to the filename in the end when the file is saved.

Thanks
 

WW

Golden Member
Jun 21, 2001
1,514
0
0
that's more complicated...but doable.

insert a userform in your project, then view > toolbox, and drop a listbox or combobox in your form.

Then you'll need code to populate the listbox /combobox, show it, and so on...

search for listbox control or combobox in the vba help file

you asked how I learned vba, and this is pretty much how :) the help file, plus do a google search for "word combobox control" etc.

good luck...post if you have any specific questions after digging in a little
 

AznMaverick

Platinum Member
Apr 4, 2001
2,776
0
0
ok, i've created a form, and i know how to use a button to execute commands. my question is now, how do you populate the listbox with entries and after have a user choose an item from the listbox and whatever choice they make have that saved into a variable so i can use it in the name that i will save the file as.

example

------------------
| Choice1 |
| Choice2 |
| Choice3 |
------------------

OK

if the user chooses Choice1 savename = value of Choice1,

edit: oh btw, i've been searching all over google for source code, but can't really find what i am looking for. I know how to populate the form using the Access wizard, but i can't seem to export it into word...
 

WW

Golden Member
Jun 21, 2001
1,514
0
0
ok, so you have your choices in variables already:
c1 = "choice1"
c2 = "choice2"
c3 = "choice3"

before the main macro, right before "Sub macro1()" add a Public statement to make those variables that you want to share between subroutines global, (otherwise the variable get wiped out in each separate procedure):

Public c1, c2, c3, savename


add the line:

userform1.show

in the main macro to make the form appear.

and add this in userform code:

Private Sub UserForm_Initialize()

ListBox1.AddItem c1
ListBox1.AddItem c2

End Sub


then the listbox will have those items...now in the listbox click event, simply have

Private Sub ListBox1_Click()

savename = ListBox1.Value
UserForm1.hide

End Sub

that will save the value in that public variable, and hide the form (and complete the code)

remember to use F8 to step through the code, and if you hover mouse over a variable it will show you the current value of the variable...handy in debugging and understanding whats going where..

let me know if anything here is confusing...it's kind of hard to describe.

WW
 

AznMaverick

Platinum Member
Apr 4, 2001
2,776
0
0
thanks for all of your help, it actually works perfectly the way i wanted it to work now...thanks!