compare copied number with another fixed number.

T

Tommy

Hi All,

I have a question to ask in Word Macro please. thanks in advance.

can i put in "if / then" statement in the macro? like in batch file?

step 1: let's say I manually highlight a number (34567) and select
edit/copy from the document. click on the command button which will do the
following.

step 2: if the copied number = 23456, then goto a subroutine 23456.
if the copied number = 34567, then goto a subroutine 34567.
 
N

NOPIK

step 1:  let's say I manually highlight a number (34567) and select
edit/copy from the document. click on the command button which will do the
following.

step 2:  if the copied number =  23456, then goto a subroutine 23456.
            if the copied number =  34567, then goto a subroutine 34567.

Yes, Word macros are Visual Basic programs.
There is no Word macro command for clipboard (you can find program for
this googling "VBA clipboard"), but you can easily operate highlighted
areas, so, for your case:

If Selection.Text="23456" Then
call sub 23456
End If

or CASE:

Select Case Selection.Text
case "23456"
call sub 23456
case "3456"
call sub 3456
End Select
 
G

Graham Mayor

You could use something like

Sub Macro1()
Dim sNum As Variant
sNum = Selection.Range
If IsNumeric(sNum) = False Then Exit Sub
Select Case sNum
Case 23456
Call Test1
Case 34567
Call Test2
Case Else
Call Test3
End Select
End Sub

Function Test1()
MsgBox "This is function 23456"
End Function
Function Test2()
MsgBox "This is function 34567"
End Function
Function Test3()
MsgBox "No function defined"
End Function
 
T

Tommy

Thanks Mr. Mayor! this part works.

I have another question, how can I put all these in a loop? I have a report
that contains 23456, 34567, 45678, per say. How can I make it loops to
accomplish this? I have the macro below to find the number, then highlight
and run the script you provided. there is a line to break the section from
the first part. thanks again in advance!

Dim sNum As Variant
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Text = "REPORT ID:"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=2
Selection.MoveRight Unit:=wdCharacter, Count:=5, Extend:=wdExtend

sNum = Selection.Range
If IsNumeric(sNum) = False Then Exit Sub
Select Case sNum
Case 23456
Call Test1
Case 34567
Call Test2
Case 45678
Call Test3
Case Else
Call Test4
End Select
End Sub

Function Test1()
MsgBox "This is function 23456"
End Function
Function Test2()
MsgBox "This is function 34567"
End Function
Function Test3()
MsgBox "This is function 45678"
End Function
Function Test4()
MsgBox "No function defined"
End Function
 
G

Graham Mayor

Dim sNum As Range
With Selection
.HomeKey wdStory
With .Find
.ClearFormatting
.Replacement.ClearFormatting
Do While .Execute(findText:="REPORT ID:", _
MatchWildcards:=False, _
Wrap:=wdFindStop, Forward:=True) = True

With Selection
.MoveRight Unit:=wdCharacter, Count:=2
.MoveRight Unit:=wdCharacter, Count:=5, Extend:=wdExtend
End With
Set sNum = Selection.Range
If IsNumeric(sNum) = False Then Exit Sub
Select Case sNum
Case 23456
Call Test1
Case 34567
Call Test2
Case 45678
Call Test3
Case Else
Call Test4
End Select
Selection.Collapse
Loop
End With
End With
End Sub

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top