Creating a variable string from a multiselect listbox in MS Word -
i have created userform multiselect listbox "ok" command. when user makes selections listbox , clicks ok command, want create array (based on user's selections in listbox) can loop on each item in array open multiple files user has specified.
for example, if user selects "client 1" , "client 3" in listbox , selects "ok" command, want create array values , call each value in array in "find , replace" sub replaces, e.g., "client 1" "client 1" (colored red), "client 3" "client 3" (colored red). (the red other find , replace macro can skip these items specifying different color find for, along text client 1, client 3, etc.)
reading elsewhere on site, created function try generate array, don't know how , use in userform sub.
after finding answer, below, deleted original code had pasted here, because wrong , won't anyone.
additional information overall objective: have created macro initial find , replace in multiple files. macro opens bunch of files selected user , replaces client names text "confidential client". now, people asking me if can exclude clients being replaced. why want add userform listbox let them select clients exclude.
please help!
so, through trial , error , googling, came following solution, works purpose. first, after clicking f7 on userform, added items list array.
private sub userform_initialize() 'creates , assigns array listboxclients when form loads listboxclients .additem "client 1" .additem "client 2" .additem "client 3" end end sub
then, created following response "ok" command. first, prompts user select files process , opens first file:
private sub cmdok_click() me.hide msgbox "click ok browse , select files exclude.", vbinformation dim mydialog filedialog, getstr(1 3000) string '3000 files maximum applying code on error resume next set mydialog = application.filedialog(msofiledialogfilepicker) mydialog .filters.clear .filters.add "all word file ", "*.docx", 1 .allowmultiselect = true = 1 if .show = -1 each stiselecteditem in .selecteditems getstr(i) = stiselecteditem = + 1 next = - 1 end if application.screenupdating = false j = 1 step 1 set doc = documents.open(filename:=getstr(j), visible:=true) windows(getstr(j)).activate
then, loop through selected items in array , replace each selected item same text, colored red (so other macro--not shown here--will skip on when performs find , replace):
'find , replace listbox items in files dim ii integer ii = 0 listboxclients.listcount - 1 if listboxclients.selected(ii) selection.text = listboxclients.list(ii) selection.find.clearformatting selection.find.replacement.clearformatting selection.find.replacement.font.color = 192 selection.find .text = selection.text .replacement.text = selection.text .forward = true .wrap = wdfindcontinue .format = true .matchcase = false .matchwholeword = false .matchwildcards = false .matchsoundslike = false .matchallwordforms = false end selection.find.execute replace:=wdreplaceall
before finish, rid of selected array item reasons unknown pasted @ top of each of files:
' delete mysterious added text @ top of page (figure out later) selection.homekey unit:=wdstory selection.endkey unit:=wdline, extend:=wdextend selection.delete unit:=wdcharacter, count:=1 end if next ii
then have code closes current document , returns top section of sub open next file (and end). have no idea why "application.run macroname:="newmacros" there, works, i'm not going delete it.
application.run macroname:="newmacros" activedocument.save activewindow.close next application.screenupdating = true end end sub
finally, add code cancel form if user changes his/her mind:
private sub cmdcancel_click() 'user has cancelled hide form me.hide end sub
that's it. hope helps else.
Comments
Post a Comment