Log in

View Full Version : Solved: Negative number handling



AyeSee
08-13-2010, 12:19 PM
Hello,

I'm writing some code that will read a number in my word document and that will write down on the next line "negative " if the word is negative, and write nothing is the word is positive. Currently, the result I am getting is always negative whether the number is positive or negative.

To do so, I used the Selection and wdextend to get the whole number and then I use this code :

Dim ga As Double

If ga <= 0 Then
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.TypeText Text:="negative "
Else
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.TypeText Text:=""

End If

Does anyone understand why it never captures handles it properly? Many thanks for any kind of help

Alex

gmaxey
08-13-2010, 12:43 PM
The procedure has you have it written has no idea what the value of ga is supposed to be:

Sub ScratchMacro()
Dim ga As Double
ga = CDbl(Selection.Text)
If ga <= 0 Then
MsgBox "ga is negative"
Else
MsgBox "ga is not negative"
End If
End Sub

macropod
08-13-2010, 10:20 PM
Surely, though, a ga is negative if, and only if, ga < 0, and not when ga = 0!

gmaxey
08-14-2010, 05:21 AM
Yes, one would surely think that. At the time this one (me) didn't though :-(

fumei
08-15-2010, 11:43 PM
" used the Selection and wdextend to get the whole number"

Selection (unless converted) is a string.

Once converted, and talking inot account the < 0 issue macropod points out it should work.

AyeSee
08-16-2010, 04:06 AM
Thanks everybody for the responses!

I had a suspicion that selection was in string.. And as for the 0 value, it is indeed intended for it not to be included.

and fumei, thanks again. I had two mistakes in my code:

1. I forgot to parse
2. I forgot to store the selection.text inside the variable! lol (uber fail)

now it works!! thanks!

Dim ga As Double

ga = CLng(Selection.Text)

If ga <= 0 Then
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.TypeText Text:="negative "
Else
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.TypeText Text:=""

End If