Run a word vba macro from vb script - vbscript

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
End With
With Selection
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseStart
Else
.Collapse Direction:=wdCollapseEnd
End If
.Find.Execute Replace:=wdReplaceOne
End With
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
End With
With Selection
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseStart
Else
.Collapse Direction:=wdCollapseEnd
End If
.Find.Execute Replace:=wdReplaceOne
End With
Application.ScreenUpdating = True
End Sub
could dear members create a vb script file which contains the above code, so that I call the vb script , in order to run the code.
I have wrecked my brains and troubled google, to no avail. Please help.
Thank you.
Vik

There is an example of VBScript code which opens the document and make two replacements with given options, more compact form of the .Find.Execute method used:
Const wdFindContinue = 1
Const wdReplaceOne = 1
Dim objWord, objDocument
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDocument = objWord.Documents.Open("C:\test.docx")
With objWord
.ScreenUpdating = False
With .Selection
.Collapse
With .Find
' .Execute(FindText, MatchCase, MatchWholeWord, MatchWildcards, MatchSoundsLike, MatchAllWordForms, Forward, Wrap, Format, ReplaceWith, Replace, MatchKashida, MatchDiacritics, MatchAlefHamza, MatchControl)
' "abc" -> "def"
.Execute "abc", False, False, False, False, False, True, wdFindContinue, False, "def", wdReplaceOne
' "pqr" -> "xyz"
.Execute "pqr", False, False, False, False, False, True, wdFindContinue, False, "xyz", wdReplaceOne
End With
End With
.ScreenUpdating = True
End With

Related

Run time error 5:invalid procedure call or argument

Here is the code which I have written in vb6...it is showing error in text1.setfocus
Private Sub Text1_Lostfocus()
s1 = Text1.Text
flag = 0
If Text1.Text = "" Then
flag = 1
End If
For i = 1 To Len(s1)
l = Mid(s1, i, 1)
If IsNumeric(l) = True Then
flag = 1
Exit For
End If
Next i
If flag = 1 Then
MsgBox "Enter valid input"
Text1.ForeColor = vbRed
Text1.SetFocus
End If
End Sub
Do not have this code in LostFocus, instead try to have it in the Validate event, there would be an cancel parameter to the event, if you set Cancel = True (means the cursor will not exit the control) you need not do setfocus
Try the following:
Private Sub Text1_Validate(Cancel As Boolean)
If IsNumeric(Text1.Text) = False Then
MsgBox "Enter valid input"
Text1.ForeColor = vbRed
Cancel = True
End If
End Sub
Try this if you have an empty string and you are trying to work with in in your loop you can get an invalid procedure call. skip over it all togehter don't run the loop if the text is empty.
Private Sub Text1_Lostfocus()
s1 = Text1.Text
flag = 0
If Text1.Text = "" Then
flag = 1
else
For i = 1 To Len(s1)
l = Mid(s1, i, 1)
If IsNumeric(l) = True Then
flag = 1
Exit For
End If
Next i
endif
If flag = 1 Then
MsgBox "Enter valid input"
Text1.ForeColor = vbRed
Text1.SetFocus
End If
End Sub

Speed up VBA execution

So I have the below VBA macros setup and when CompHide runs it takes several minutes to update. I feel like this is due to the line that says C.EntireRow.Columns(43).Value = ""
I tried making a new "helper" column that would check if both of the columns were empty and had it return "Y" or "N" and then had the macro look at that for "Y" and hide those. This sped it up some but I am wanting to get even faster if I could.
Orginal code:
Sub CompHide()
Dim sht As Worksheet, C As Range
Application.ScreenUpdating = False
Application.EnableEvents = False
Set sht = Sheets("Comparison")
sht.Rows.Hidden = False
CSetRowVis "C9", "CMarket1"
CSetRowVis "C115", "CMarket2"
CSetRowVis "C221", "CMarket3"
CSetRowVis "C329", "CMarket4"
CSetRowVis "C437", "CMarket5"
CSetRowVis "C545", "CMarket6"
CSetRowVis "C653", "CMarket7"
CSetRowVis "C761", "CMarket8"
CSetRowVis "C869", "CMarket9"
CSetRowVis "C977", "CMarket10"
For Each C In sht.Range("CNonTest")
If C.Value = "" And C.EntireRow.Columns(43).Value = "" Then
C.EntireRow.Hidden = True
End If
Next
sht.Range("CBlank").EntireRow.Hidden = True
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
Sub CSetRowVis(addr As String, rngName As String)
With Sheets("Comparison")
If .Range(addr).Value = "Unused" Then
.Range(rngName).EntireRow.Hidden = True
End If
End With
End Sub
New Code:
Sub CompHide()
Dim sht As Worksheet, C As Range
Application.ScreenUpdating = False
Application.EnableEvents = False
Set sht = Sheets("Comparison")
sht.Rows.Hidden = False
CSetRowVis "C9", "CMarket1"
CSetRowVis "C115", "CMarket2"
CSetRowVis "C221", "CMarket3"
CSetRowVis "C329", "CMarket4"
CSetRowVis "C437", "CMarket5"
CSetRowVis "C545", "CMarket6"
CSetRowVis "C653", "CMarket7"
CSetRowVis "C761", "CMarket8"
CSetRowVis "C869", "CMarket9"
CSetRowVis "C977", "CMarket10"
For Each C In sht.Range("CHideTest")
If C.Value = "Y" Then
C.EntireRow.Hidden = True
End If
Next
sht.Range("CBlank").EntireRow.Hidden = True
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
Sub CSetRowVis(addr As String, rngName As String)
With Sheets("Comparison")
If .Range(addr).Value = "Unused" Then
.Range(rngName).EntireRow.Hidden = True
End If
End With
End Sub
This is using Excel 2013 Standard 64 bit. Number of rows is just under 1200. Number of Columns is 150. All of those cells are formulas
If there is any extra info you need let me know.
Instead of this:
For Each C In sht.Range("CHideTest")
If C.Value = "Y" Then
C.EntireRow.Hidden = True
End If
Next
consider something like this:
Dim rng As Range 'for collecting rows to be hidden
For Each C In sht.Range("CHideTest")
If C.Value = "Y" Then
if rng Is Nothing Then
set rng = C
Else
set rng = application.union(rng, C)
end if
End If
Next
'hide all accumulated rows (if any found)
if not rng is nothing then rng.EntireRow.Hidden = True

Sorting String with Numbers using VB Script

How to Sort String with Numeric values using VB Script?
Below are my strings from each row from a table:
"Test 1 pass dec 2"
"Test 3 fail"
"Test 2 pass jun 4"
"Verified"
"Test 10 pass"
"User Accepted"
I would to like get in below order after sorting(natural order):
"Test 1 pass dec 2"
"Test 2 pass jun 4"
"Test 3 fail"
"Test 10 pass"
"User Accepted"
"Verified"
Ways i have tried so far,
Set oAlist=CreateObject("System.Collections.ArrayList")
oAlist.sort
The ArrayList was sorted in below order based on ASCII which I do not prefer:
"Test 1 pass dec 2"
"Test 10 pass"
"Test 2 pass jun 4"
"Test 3 fail"
"User Accepted"
"Verified"
I have tried this link Sort
and i have no idea how to use AppendFormat in my case.
Note:My given string either completely string or string with numbers(dynamic) so not sure how to use RecordSet or AppendFormat here as I am new to programming.
You can have another example.
Sub Sort
Set rs = CreateObject("ADODB.Recordset")
If LCase(Arg(1)) = "n" then
With rs
.Fields.Append "SortKey", 4
.Fields.Append "Txt", 201, 5000
.Open
Do Until Inp.AtEndOfStream
Lne = Inp.readline
SortKey = Mid(Lne, LCase(Arg(3)), LCase(Arg(4)) - LCase(Arg(3)))
If IsNumeric(Sortkey) = False then
Set RE = new Regexp
re.Pattern = "[^0-9\.,]"
re.global = true
re.ignorecase = true
Sortkey = re.replace(Sortkey, "")
End If
If IsNumeric(Sortkey) = False then
Sortkey = 0
ElseIf Sortkey = "" then
Sortkey = 0
ElseIf IsNull(Sortkey) = true then
Sortkey = 0
End If
.AddNew
.Fields("SortKey").value = CSng(SortKey)
.Fields("Txt").value = Lne
.UpDate
Loop
If LCase(Arg(2)) = "a" then SortColumn = "SortKey ASC"
If LCase(Arg(2)) = "d" then SortColumn = "SortKey DESC"
.Sort = SortColumn
Do While not .EOF
Outp.writeline .Fields("Txt").Value
.MoveNext
Loop
End With
ElseIf LCase(Arg(1)) = "d" then
With rs
.Fields.Append "SortKey", 4
.Fields.Append "Txt", 201, 5000
.Open
Do Until Inp.AtEndOfStream
Lne = Inp.readline
SortKey = Mid(Lne, LCase(Arg(3)), LCase(Arg(4)) - LCase(Arg(3)))
If IsDate(Sortkey) = False then
Set RE = new Regexp
re.Pattern = "[^0-9\\\-:]"
re.global = true
re.ignorecase = true
Sortkey = re.replace(Sortkey, "")
End If
If IsDate(Sortkey) = False then
Sortkey = 0
ElseIf Sortkey = "" then
Sortkey = 0
ElseIf IsNull(Sortkey) = true then
Sortkey = 0
End If
.AddNew
.Fields("SortKey").value = CDate(SortKey)
.Fields("Txt").value = Lne
.UpDate
Loop
If LCase(Arg(2)) = "a" then SortColumn = "SortKey ASC"
If LCase(Arg(2)) = "d" then SortColumn = "SortKey DESC"
.Sort = SortColumn
Do While not .EOF
Outp.writeline .Fields("Txt").Value
.MoveNext
Loop
End With
ElseIf LCase(Arg(1)) = "t" then
With rs
.Fields.Append "SortKey", 201, 260
.Fields.Append "Txt", 201, 5000
.Open
Do Until Inp.AtEndOfStream
Lne = Inp.readline
SortKey = Mid(Lne, LCase(Arg(3)), LCase(Arg(4)) - LCase(Arg(3)))
.AddNew
.Fields("SortKey").value = SortKey
.Fields("Txt").value = Lne
.UpDate
Loop
If LCase(Arg(2)) = "a" then SortColumn = "SortKey ASC"
If LCase(Arg(2)) = "d" then SortColumn = "SortKey DESC"
.Sort = SortColumn
Do While not .EOF
Outp.writeline .Fields("Txt").Value
.MoveNext
Loop
End With
ElseIf LCase(Arg(1)) = "tt" then
With rs
.Fields.Append "SortKey", 201, 260
.Fields.Append "Txt", 201, 5000
.Open
Do Until Inp.AtEndOfStream
Lne = Inp.readline
SortKey = Trim(Mid(Lne, LCase(Arg(3)), LCase(Arg(4)) - LCase(Arg(3))))
.AddNew
.Fields("SortKey").value = SortKey
.Fields("Txt").value = Lne
.UpDate
Loop
If LCase(Arg(2)) = "a" then SortColumn = "SortKey ASC"
If LCase(Arg(2)) = "d" then SortColumn = "SortKey DESC"
.Sort = SortColumn
Do While not .EOF
Outp.writeline .Fields("Txt").Value
.MoveNext
Loop
End With
End If
End Sub
Since you are working with strings, you are going to need to write a custom sort function that can parse the test numbers from the strings.
Alternatively, you could pre-process your list and parse the numbers into a separate field, then sort based on that field.
To apply the techniques from here to the problem (using Split instead of a RegExp):
Option Explicit
Dim aInp : aInp = Array( _
"Test 1 pass dec 2" _
, "Test 3 fail" _
, "Test 2 pass jun 4" _
, "Verified" _
, "Test 10 pass" _
, "User Accepted" _
)
WScript.Echo "----- Input:", vbCrLf & Join(aInp, vbCrLf)
Dim aOtp : aOtp = Array( _
"Test 1 pass dec 2" _
, "Test 2 pass jun 4" _
, "Test 3 fail" _
, "Test 10 pass" _
, "User Accepted" _
, "Verified" _
)
WScript.Echo "----- Expected:", vbCrLf & Join(aOtp, vbCrLf)
Dim oNAL : Set oNAL = CreateObject( "System.Collections.ArrayList" )
Dim oSB : Set oSB = CreateObject( "System.Text.StringBuilder" )
Dim sInp, aParts, aWTF
For Each sInp In aInp
aParts = Split(sInp, " ", 3)
Select Case UBound(aParts)
Case 0 ' add 2 blank elms to "verified"
aWTF = aParts
ReDim Preserve aWTF(2)
Case 1 ' put an empty elm in the middle
' aParts = Array( aParts(0), "", aParts(1))
' ==> VBScript runtime error: This array is fixed or temporarily locked
aWTF = Array( aParts(0), "", aParts(1))
Case 2 ' What the doctor ordered
aWTF = aParts
Case Else
Err.Raise "Shit hits fan"
End Select
oSB.AppendFormat_3 "{0}{1,4}{2}", aWTF(0), aWTF(1), aWTF(2)
sInp = oSB.ToString() & "|" & sInp ' dirty trick: append org data th ease 'reconstruction'
oSB.Length = 0
oNAL.Add sInp
Next
oNAL.Sort
ReDim aOut(oNAL.Count - 1)
Dim i
For i = 0 To UBound(aOut)
aOut(i) = Split(oNAL(i), "|")(1)
Next
WScript.Echo "----- Output:", vbCrLf & Join(aOut, vbCrLf)
output:
cscript 37946075.vbs
----- Input:
Test 1 pass dec 2
Test 3 fail
Test 2 pass jun 4
Verified
Test 10 pass
User Accepted
----- Expected:
Test 1 pass dec 2
Test 2 pass jun 4
Test 3 fail
Test 10 pass
User Accepted
Verified
----- Output:
Test 1 pass dec 2
Test 2 pass jun 4
Test 3 fail
Test 10 pass
User Accepted
Verified
Just for fun: The 'same', but using a RegExp (better scaling technique):
...
Dim r : Set r = New RegExp
r.Pattern = "^(\w+\s*)(\d+\s*)?(.*)$"
Dim sInp, m, aParts(2)
Dim i
For Each sInp In aInp
Set m = r.Execute(sInp)
If 1 = m.Count Then
For i = 0 To 2
aParts(i) = m(0).SubMatches(i)
Next
Else
Err.Raise "Shit hits fan"
End If
oSB.AppendFormat_3 "{0}{1,4}{2}", aParts(0), aParts(1), aParts(2)
sInp = oSB.ToString() & "|" & sInp ' dirty trick: append org data to ease 'reconstruction'
...

vbscript multi find/replace string in word document

I'm trying to create a VB script that will perform the substitution of certain characters in a word document and I managed in the following way:
objSelection.Find.Text = "["
objSelection.Find.Forward = TRUE
objSelection.Find.Replacement.Text = "q"
objSelection.Find.Execute ,,,,,,,,,,2
objSelection.Find.Text = "{"
objSelection.Find.Forward = TRUE
objSelection.Find.Replacement.Text = "w"
objSelection.Find.Execute ,,,,,,,,,,2
objSelection.Find.Text = "^"
objSelection.Find.Forward = TRUE
objSelection.Find.Replacement.Text = "y"
objSelection.Find.Execute ,,,,,,,,,,2
objSelection.Find.Text = "~"
objSelection.Find.Forward = TRUE
objSelection.Find.Replacement.Text = "z"
objSelection.Find.Execute ,,,,,,,,,,2
objSelection.Find.Text = "]"
objSelection.Find.Forward = TRUE
objSelection.Find.Replacement.Text = "x"
objSelection.Find.Execute ,,,,,,,,,,2
objSelection.Find.Text = "}"
objSelection.Find.Forward = TRUE
objSelection.Find.Replacement.Text = "ć"
objSelection.Find.Execute ,,,,,,,,,,2
objSelection.Find.Text = "#"
objSelection.Find.Forward = TRUE
objSelection.Find.Replacement.Text = "]"
objSelection.Find.Execute ,,,,,,,,,,2
However, the way in which I managed to do is a very slow, especially when increasing the number of characters that need to be replaced when a text file is several MB... So, the script goes through the entire documet for each replace characters and that resulting is very slow execution of script. Is there a possibility of parallel changes several characters in a word document, the script only one pass through the document and depending on which character is encountered, to perform the replacement?
I also tried loading line by line from word document into a variable and comparison of each character with the given character for a replacement which resulted in much slower execution of scripts...
Sorry for my english, i hope i succeeded to explain my problem.
Please help me. Thank you. :)
Could you do something like this?
objSelection.Find.ClearFormatting
With objSelection.Find
.Text = "[\[\{~\]\}\#^0094]" ' ^0094 is "^"
.Replacement.Text = ""
.Forward = True
.Wrap = 1 'wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Do While objSelection.Find.Execute()
Dim t: t = objSelection.Text
Select Case t
Case "["
objSelection.Text = "q"
Case "{"
objSelection.Text = "w"
Case "~"
objSelection.Text = "z"
'... fill in the rest
End Select
objSelection.Move 1
Loop
I think this may only work if you look for and replace single characters, but what this does is match any of the characters you are looking for, checks which one it's found and replaces it with the appropriate character.
This was taken from a recorded macro in Word 2010 but I don't think any conversion to VBScript should be necessary.

Picture changing in vb6

I was trying this script from a pdf file.I got stuck where the target image should change to exploding image if clicked but the target image does not change from the standing image.Please Help!
Option Explicit
Dim fiPlayersScore As Integer
Dim fiNumberofMisses As Integer
Dim fbTargetHit As Boolean
Private Sub Form_Load()
Randomize
imgTarget.Enabled = False
imgTarget.Visible = False
cmdStop.Enabled = False
lblGameOver.Visible = False
lblGameOver.Enabled = False
End Sub
Private Sub cmdStart_Click()
Dim lsUserResponse As String
Dim lbResponse As Boolean
lsUserResponse = InputBox("Enter a level from 1 to 3." & _
(Chr(13)) & "" & (Chr(13)) & "1 being the Easiest and 3 being the " & _
"Hardest.", "Level Select", "1")
lbResponse = False
If lsUserResponse = "1" Then
Timer1.Interval = 1500
lbResponse = True
ElseIf lsUserResponse = "2" Then
Timer1.Interval = 1000
lbResponse = True
ElseIf lsUserResponse = "3" Then
Timer1.Interval = 750
lbResponse = True
Else
MsgBox ("Game Not Started.")
lbResponse = False
End If
If lbResponse = True Then
cmdStart.Enabled = False
imgTarget.Picture = imgStanding.Picture
frmMain.MousePointer = 5
fbTargetHit = False
Load_Sounds
cmdStop.Enabled = True
fiPlayersScore = 0
fiNumberofMisses = 0
lblScore.Caption = fiPlayersScore
lblMisses.Caption = fiNumberofMisses
Timer1.Enabled = True
lblGameOver.Visible = False
lblGameOver.Enabled = False
End If
End Sub
Private Sub cmdStop_Click()
Unload_Sounds
frmMain.MousePointer = vbNormal
Timer1.Enabled = False
imgTarget.Enabled = False
imgTarget.Visible = False
cmdStart.Enabled = True
cmdStop.Enabled = False
cmdStart.SetFocus
lblGameOver.Visible = True
lblGameOver.Enabled = True
End Sub
Private Sub Form_Click()
MMControl1.Command = "Play"
MMControl1.Command = "Prev"
fiNumberofMisses = fiNumberofMisses + 1
lblMisses.Caption = fiNumberofMisses
If CheckForLoose = True Then
cmdStop_Click
lblMisses.Caption = fiNumberofMisses
Exit Sub
End If
End Sub
Private Sub imgTarget_Click()
MMControl2.Command = "Play"
MMControl2.Command = "Prev"
Timer1.Enabled = False
imgTarget.Picture = imgExplode.Picture '**I AM STUCK HERE**
pauseProgram
fiPlayersScore = fiPlayersScore + 1
Timer1.Enabled = True
If CheckForWin = True Then
cmdStop_Click
lblScore.Caption = fiPlayersScore
Exit Sub
End If
lblScore.Caption = fiPlayersScore
fbTargetHit = True
imgStanding.Enabled = False
imgTarget.Visible = False
imgTarget.Enabled = False
Timer1.Enabled = True
End Sub
Public Sub Load_Sounds()
'Set initial property values for blaster sound
MMControl1.Notify = False
MMControl1.Wait = True
MMControl1.Shareable = False
MMControl1.DeviceType = "WaveAudio"
MMControl1.FileName = _
"C:\Temp\Sounds\Blaster_1.wav"
'Open the media device
MMControl1.Command = "Open"
Private Sub Timer1_Timer()
Dim liRandomLeft As Integer
Dim liRandomTop As Integer
imgTarget.Visible = True
If fbTargetHit = True Then
fbTargetHit = False
imgTarget.Picture = imgStanding.Picture
End If
liRandomLeft = (6120 * Rnd)
liRandomTop = (4680 * Rnd)
imgTarget.Left = liRandomLeft
imgTarget.Top = liRandomTop
imgTarget.Enabled = True
imgTarget.Visible = True
End Sub
Public Function CheckForWin() As Boolean
CheckForWin = False
If fiPlayersScore = 5 Then
CheckForWin = True
lblGameOver.Caption = "You Win.Game Over"
End If
End Function
Public Function CheckForLoose() As Boolean
CheckForLoose = False
If fiNumberofMisses = 5 Then
CheckForLoose = True
lblGameOver.Caption = "You Loose.Game Over"
End If
End Function
Private Sub Form_QueryUnload(Cancel As Integer, _
UnloadMode As Integer)
Unload_Sounds
End Sub
Public Sub Unload_Sounds()
MMControl1.Command = "Close"
MMControl2.Command = "Close"
End Sub
Public Sub pauseProgram()
Dim currentTime
Dim newTime
currentTime = Second(Time)
newTime = Second(Time)
Do Until Abs(newTime - currentTime) >= 1
newTime = Second(Time)
Loop
End Sub
EDIT:
imgTarget.Picture = imgExplode.Picture
imgTarget.Refresh
Note:
Set imgTarget.Picture = imgExplode.Picture
imgTarget.Refresh
will be faster than
imgTarget.Picture = imgExplode.Picture
imgTarget.Refresh
if imgExplode is going to be around during the lifetime of imgTarget (the first command copies the image, the Set command references the image).

Resources