PDA

View Full Version : call word vba macro from a vb script



vik1
04-22-2016, 08:43 AM
How can I call a word vba macro code from a VB Script:
the word vba macro code is under:

Sub find_replace_vik_42216()


Application.ScreenUpdating =False

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text ="abc"
.Replacement.Text ="def"
.Forward =True
.Wrap = wdFindContinue
.Format =False
.MatchCase =False
.MatchWholeWord =False
.MatchWildcards =False
.MatchSoundsLike =False
.MatchAllWordForms =False
EndWith
With Selection
If.Find.Forward =TrueThen
.Collapse Direction:=wdCollapseStart
Else
.Collapse Direction:=wdCollapseEnd
EndIf
.Find.Execute Replace:=wdReplaceOne
EndWith



Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text ="pqr"
.Replacement.Text ="xyz"
.Forward =True
.Wrap = wdFindContinue
.Format =False
.MatchCase =False
.MatchWholeWord =False
.MatchWildcards =False
.MatchSoundsLike =False
.MatchAllWordForms =False
EndWith
With Selection
If.Find.Forward =TrueThen
.Collapse Direction:=wdCollapseStart
Else
.Collapse Direction:=wdCollapseEnd
EndIf
.Find.Execute Replace:=wdReplaceOne
EndWith

Application.ScreenUpdating =True

EndSub

could dear members create a vb script file which contains the above code, so that I call the vb script to run this code.
I have wrecked my brains and troubled google, to no avail. Please help.
Thank you.
Vik

SamT
04-22-2016, 02:29 PM
The vb script must open the Doc, then call the sub by prefixing it with the document name or the doc variable


Set MyDoc = File(worddoc.Doc).open 'or whatever is right for VB Scripts
myDoc.find_replace_vik_42216

vik1
04-22-2016, 08:21 PM
i am not able to figure out what is right for VB Script. Could someone help this poor man, please .


Vik

vik1
04-25-2016, 03:28 AM
if not background, how to run this in the foreground automatically (means auto schedule after every , say, 10 minutes).

SamT
04-25-2016, 07:34 AM
What are you trying to accomplish in the real world?

I can't help with vb scripting, but we may be able to get done what you need done.

vik1
04-25-2016, 04:07 PM
i have numerous word macros (one macro is above), that I need to run on multiple word documents throughout the day....i'm tired doing this manually. I run them on the same machine i work, so would prefer a background process where word doc does not open. however, if not possible in background, then I can manage with background. But i am stuck at this point, of how to run the macro automatically.

the programming language can be any. Much appreciate help.