read a field value and pass it to a variable on a macro

J

jmc

Hown can I read a field value and pass it to a variable that is used by a macro (function)

The macro is to transform the numeric value of that field and display it in the same page in words

The macro (Function)

Function Extenso(nValor
Text3 = "
'------------------------------------------------------------------------------------------------------------------
' Codigo de Substituição do ponto pela virgola, isto para permitir introdusir tanto o ponto como avirgul

a = Len(nValor
For i = 1 To
X = Left(nValor, i
K = Mid(nValor, i, 1
If K = "." The
K = ",
b = Len(X) -
txtnValor = Left(nValor, b
txtnValor = txtnValor &
nValor = txtnValor & Right(nValor, (a - (b + 1))
nValor = FormatNumber((nValor), 2
End I
Next
'-------------------------------------------------------------------------------------------------
If IsNull(nValor) Or nValor = "" The
Extenso = "Valor Nulo
Text3 = Format(nValor, "##,##0.00") & " - " & Extens
Exit Functio
ElseIf nValor <= 0 The
Extenso = "Valor Inferior a 0
Text3 = Format(nValor, "##,##0.00") & " - " & Extens
Exit Functio
ElseIf nValor > 99999999999.99 The
Extenso = "Valor Superior 99 MIL MILHÕES de Euros
Text3 = Format(nValor, "##,##0.00") & " - " & Extens
Exit Functio
End I
Dim nContador, nTamanho As Intege
Dim cValor, cParte, cFinal As Strin
ReDim aGrupo(5), aTexto(5) As Strin
ReDim aUnid(19) As Strin
aUnid(1) = "UM ": aUnid(2) = "DOIS ": aUnid(3) = "TRES
aUnid(4) = "QUATRO ": aUnid(5) = "CINCO ": aUnid(6) = "SEIS
aUnid(7) = "SETE ": aUnid(8) = "OITO ": aUnid(9) = "NOVE
aUnid(10) = "DEZ ": aUnid(11) = "ONZE ": aUnid(12) = "DOZE
aUnid(13) = "TREZE ": aUnid(14) = "CATORZE ": aUnid(15) = "QUINZE
aUnid(16) = "DEZASSEIS ": aUnid(17) = "DEZASSETE ": aUnid(18) = "DEZOITO
aUnid(19) = "DEZANOVE

ReDim aDezena(9) As Strin
aDezena(1) = "DEZ ": aDezena(2) = "VINTE ": aDezena(3) = "TRINTA
aDezena(4) = "QUARENTA ": aDezena(5) = "CINQUENTA
aDezena(6) = "SESSENTA ": aDezena(7) = "SETENTA ": aDezena(8) = "OITENTA
aDezena(9) = "NOVENTA

ReDim aCentena(9) As Strin
aCentena(1) = "CENTO ": aCentena(2) = "DUZENTOS
aCentena(3) = "TREZENTOS ": aCentena(4) = "QUATROCENTOS
aCentena(5) = "QUINHENTOS ": aCentena(6) = "SEISCENTOS
aCentena(7) = "SETECENTOS ": aCentena(8) = "OITOCENTOS
aCentena(9) = "NOVECENTOS
cValor = Format$(nValor, "0000000000000.00"
aGrupo(1) = Mid$(cValor, 2, 3
aGrupo(2) = Mid$(cValor, 5, 3
aGrupo(3) = Mid$(cValor, 8, 3
aGrupo(4) = Mid$(cValor, 11, 3
aGrupo(5) = "0" + Mid$(cValor, 15, 2
For nContador = 1 To
cParte = aGrupo(nContador
nTamanho = Switch(Val(cParte) < 10, 1, Val(cParte) < 100, 2, Val(cParte) < 1000, 3
If nTamanho = 3 The
If Right$(cParte, 2) <> "00" The
aTexto(nContador) = aTexto(nContador) + aCentena(Left(cParte, 1)) + "E
nTamanho =
Els
aTexto(nContador) = aTexto(nContador) + IIf(Left$(cParte, 1) = "1", "CEM ", aCentena(Left(cParte, 1))
End I
End I
If nTamanho = 2 The
If Val(Right(cParte, 2)) < 20 The
aTexto(nContador) = aTexto(nContador) + aUnid(Right(cParte, 2)
Els
aTexto(nContador) = aTexto(nContador) + aDezena(Mid(cParte, 2, 1)
If Right$(cParte, 1) <> "0" The
aTexto(nContador) = aTexto(nContador) + "E
nTamanho =
End I
End I
End I
If nTamanho = 1 The
aTexto(nContador) = aTexto(nContador) + aUnid(Right(cParte, 1)
End I
Nex
If Val(aGrupo(1) + aGrupo(2) + aGrupo(3) + aGrupo(4)) = 0 And Val(aGrupo(5)) <> 0 The
cFinal = aTexto(5) + IIf(Val(aGrupo(5)) = 1, "CÊNTIMO", "CÊNTIMOS"
Else
cFinal = ""
cFinal = cFinal + IIf(Val(aGrupo(1)) <> 0, aTexto(1) + IIf(Val(aGrupo(1)) > 1, "MIL MILHÕES ", "MILHAR DE MILHÃO "), "")
cFinal = cFinal + IIf(Val(aGrupo(2)) <> 0, aTexto(2) + IIf(Val(aGrupo(2)) > 1, "MILHÕES ", "MILHÃO "), "")
If Val(aGrupo(2) + aGrupo(3) + aGrupo(4)) = 0 Then
cFinal = cFinal + "DE "
Else
cFinal = cFinal + IIf(Val(aGrupo(3)) <> 0, IIf(Val(aGrupo(3)) = 1, "MIL ", aTexto(3) + "MIL "), "")
End If
cFinal = cFinal + aTexto(4) + IIf(Val(aGrupo(1) + aGrupo(2) + aGrupo(3) + aGrupo(4)) = 1, "EURO ", "EUROS ")
cFinal = cFinal + IIf(Val(aGrupo(5)) <> 0, "E " + aTexto(5) + IIf(Val(aGrupo(5)) = 1, "CÊNTIMO", "CÊNTIMOS"), "")
End If
Extenso = cFinal
Text3 = Extenso
Labelextenso = Format(nValor, "##,##0.00") & " Euros"
End Function

Thanks.

JMC
 
J

Jezebel

Put the code for reading the field value inside the macro.




jmc said:
Hown can I read a field value and pass it to a variable that is used by a macro (function).

The macro is to transform the numeric value of that field and display it in the same page in words.

The macro (Function):

Function Extenso(nValor)
Text3 = ""
'---------------------------------------------------------------------------
introdusir tanto o ponto como avirgula
'
a = Len(nValor)
For i = 1 To a
X = Left(nValor, i)
K = Mid(nValor, i, 1)
If K = "." Then
K = ","
b = Len(X) - 1
txtnValor = Left(nValor, b)
txtnValor = txtnValor & K
nValor = txtnValor & Right(nValor, (a - (b + 1)))
nValor = FormatNumber((nValor), 2)
End If
Next i
'---------------------------------------------------------------------------
-----------------------
If IsNull(nValor) Or nValor = "" Then
Extenso = "Valor Nulo"
Text3 = Format(nValor, "##,##0.00") & " - " & Extenso
Exit Function
ElseIf nValor <= 0 Then
Extenso = "Valor Inferior a 0"
Text3 = Format(nValor, "##,##0.00") & " - " & Extenso
Exit Function
ElseIf nValor > 99999999999.99 Then
Extenso = "Valor Superior 99 MIL MILHÕES de Euros"
Text3 = Format(nValor, "##,##0.00") & " - " & Extenso
Exit Function
End If
Dim nContador, nTamanho As Integer
Dim cValor, cParte, cFinal As String
ReDim aGrupo(5), aTexto(5) As String
ReDim aUnid(19) As String
aUnid(1) = "UM ": aUnid(2) = "DOIS ": aUnid(3) = "TRES "
aUnid(4) = "QUATRO ": aUnid(5) = "CINCO ": aUnid(6) = "SEIS "
aUnid(7) = "SETE ": aUnid(8) = "OITO ": aUnid(9) = "NOVE "
aUnid(10) = "DEZ ": aUnid(11) = "ONZE ": aUnid(12) = "DOZE "
aUnid(13) = "TREZE ": aUnid(14) = "CATORZE ": aUnid(15) = "QUINZE "
aUnid(16) = "DEZASSEIS ": aUnid(17) = "DEZASSETE ": aUnid(18) = "DEZOITO "
aUnid(19) = "DEZANOVE "

ReDim aDezena(9) As String
aDezena(1) = "DEZ ": aDezena(2) = "VINTE ": aDezena(3) = "TRINTA "
aDezena(4) = "QUARENTA ": aDezena(5) = "CINQUENTA "
aDezena(6) = "SESSENTA ": aDezena(7) = "SETENTA ": aDezena(8) = "OITENTA "
aDezena(9) = "NOVENTA "

ReDim aCentena(9) As String
aCentena(1) = "CENTO ": aCentena(2) = "DUZENTOS "
aCentena(3) = "TREZENTOS ": aCentena(4) = "QUATROCENTOS "
aCentena(5) = "QUINHENTOS ": aCentena(6) = "SEISCENTOS "
aCentena(7) = "SETECENTOS ": aCentena(8) = "OITOCENTOS "
aCentena(9) = "NOVECENTOS "
cValor = Format$(nValor, "0000000000000.00")
aGrupo(1) = Mid$(cValor, 2, 3)
aGrupo(2) = Mid$(cValor, 5, 3)
aGrupo(3) = Mid$(cValor, 8, 3)
aGrupo(4) = Mid$(cValor, 11, 3)
aGrupo(5) = "0" + Mid$(cValor, 15, 2)
For nContador = 1 To 5
cParte = aGrupo(nContador)
nTamanho = Switch(Val(cParte) < 10, 1, Val(cParte) < 100, 2, Val(cParte) < 1000, 3)
If nTamanho = 3 Then
If Right$(cParte, 2) <> "00" Then
aTexto(nContador) = aTexto(nContador) +
aCentena(Left(cParte, 1)) + "E "
nTamanho = 2
Else
aTexto(nContador) = aTexto(nContador) + IIf(Left$(cParte,
1) = "1", "CEM ", aCentena(Left(cParte, 1)))
End If
End If
If nTamanho = 2 Then
If Val(Right(cParte, 2)) < 20 Then
aTexto(nContador) = aTexto(nContador) + aUnid(Right(cParte, 2))
Else
aTexto(nContador) = aTexto(nContador) + aDezena(Mid(cParte, 2, 1))
If Right$(cParte, 1) <> "0" Then
aTexto(nContador) = aTexto(nContador) + "E "
nTamanho = 1
End If
End If
End If
If nTamanho = 1 Then
aTexto(nContador) = aTexto(nContador) + aUnid(Right(cParte, 1))
End If
Next
If Val(aGrupo(1) + aGrupo(2) + aGrupo(3) + aGrupo(4)) = 0 And
Val(aGrupo(5)) said:
cFinal = aTexto(5) + IIf(Val(aGrupo(5)) = 1, "CÊNTIMO", "CÊNTIMOS")
Else
cFinal = ""
cFinal = cFinal + IIf(Val(aGrupo(1)) <> 0, aTexto(1) +
IIf(Val(aGrupo(1)) > 1, "MIL MILHÕES ", "MILHAR DE MILHÃO "), "")
cFinal = cFinal + IIf(Val(aGrupo(2)) <> 0, aTexto(2) +
IIf(Val(aGrupo(2)) > 1, "MILHÕES ", "MILHÃO "), "")
If Val(aGrupo(2) + aGrupo(3) + aGrupo(4)) = 0 Then
cFinal = cFinal + "DE "
Else
cFinal = cFinal + IIf(Val(aGrupo(3)) <> 0, IIf(Val(aGrupo(3))
= 1, "MIL ", aTexto(3) + "MIL "), "")
End If
cFinal = cFinal + aTexto(4) + IIf(Val(aGrupo(1) + aGrupo(2) +
aGrupo(3) + aGrupo(4)) = 1, "EURO ", "EUROS ")
cFinal = cFinal + IIf(Val(aGrupo(5)) <> 0, "E " + aTexto(5) +
IIf(Val(aGrupo(5)) = 1, "CÊNTIMO", "CÊNTIMOS"), "")
 
J

Jean-Guy Marcil

Hi JMC,

Assign a bookmark to your field, and read the text from the bookmark:

MyVariable = ActiveDocument.Bookmarks("MyBookmark").Range.Text

--
Cheers!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org


jmc said:
Hown can I read a field value and pass it to a variable that is used by a macro (function).

The macro is to transform the numeric value of that field and display it in the same page in words.

The macro (Function):

Function Extenso(nValor)
Text3 = ""
'---------------------------------------------------------------------------
introdusir tanto o ponto como avirgula
'
a = Len(nValor)
For i = 1 To a
X = Left(nValor, i)
K = Mid(nValor, i, 1)
If K = "." Then
K = ","
b = Len(X) - 1
txtnValor = Left(nValor, b)
txtnValor = txtnValor & K
nValor = txtnValor & Right(nValor, (a - (b + 1)))
nValor = FormatNumber((nValor), 2)
End If
Next i
'---------------------------------------------------------------------------
-----------------------
If IsNull(nValor) Or nValor = "" Then
Extenso = "Valor Nulo"
Text3 = Format(nValor, "##,##0.00") & " - " & Extenso
Exit Function
ElseIf nValor <= 0 Then
Extenso = "Valor Inferior a 0"
Text3 = Format(nValor, "##,##0.00") & " - " & Extenso
Exit Function
ElseIf nValor > 99999999999.99 Then
Extenso = "Valor Superior 99 MIL MILHÕES de Euros"
Text3 = Format(nValor, "##,##0.00") & " - " & Extenso
Exit Function
End If
Dim nContador, nTamanho As Integer
Dim cValor, cParte, cFinal As String
ReDim aGrupo(5), aTexto(5) As String
ReDim aUnid(19) As String
aUnid(1) = "UM ": aUnid(2) = "DOIS ": aUnid(3) = "TRES "
aUnid(4) = "QUATRO ": aUnid(5) = "CINCO ": aUnid(6) = "SEIS "
aUnid(7) = "SETE ": aUnid(8) = "OITO ": aUnid(9) = "NOVE "
aUnid(10) = "DEZ ": aUnid(11) = "ONZE ": aUnid(12) = "DOZE "
aUnid(13) = "TREZE ": aUnid(14) = "CATORZE ": aUnid(15) = "QUINZE "
aUnid(16) = "DEZASSEIS ": aUnid(17) = "DEZASSETE ": aUnid(18) = "DEZOITO "
aUnid(19) = "DEZANOVE "

ReDim aDezena(9) As String
aDezena(1) = "DEZ ": aDezena(2) = "VINTE ": aDezena(3) = "TRINTA "
aDezena(4) = "QUARENTA ": aDezena(5) = "CINQUENTA "
aDezena(6) = "SESSENTA ": aDezena(7) = "SETENTA ": aDezena(8) = "OITENTA "
aDezena(9) = "NOVENTA "

ReDim aCentena(9) As String
aCentena(1) = "CENTO ": aCentena(2) = "DUZENTOS "
aCentena(3) = "TREZENTOS ": aCentena(4) = "QUATROCENTOS "
aCentena(5) = "QUINHENTOS ": aCentena(6) = "SEISCENTOS "
aCentena(7) = "SETECENTOS ": aCentena(8) = "OITOCENTOS "
aCentena(9) = "NOVECENTOS "
cValor = Format$(nValor, "0000000000000.00")
aGrupo(1) = Mid$(cValor, 2, 3)
aGrupo(2) = Mid$(cValor, 5, 3)
aGrupo(3) = Mid$(cValor, 8, 3)
aGrupo(4) = Mid$(cValor, 11, 3)
aGrupo(5) = "0" + Mid$(cValor, 15, 2)
For nContador = 1 To 5
cParte = aGrupo(nContador)
nTamanho = Switch(Val(cParte) < 10, 1, Val(cParte) < 100, 2, Val(cParte) < 1000, 3)
If nTamanho = 3 Then
If Right$(cParte, 2) <> "00" Then
aTexto(nContador) = aTexto(nContador) +
aCentena(Left(cParte, 1)) + "E "
nTamanho = 2
Else
aTexto(nContador) = aTexto(nContador) + IIf(Left$(cParte,
1) = "1", "CEM ", aCentena(Left(cParte, 1)))
End If
End If
If nTamanho = 2 Then
If Val(Right(cParte, 2)) < 20 Then
aTexto(nContador) = aTexto(nContador) + aUnid(Right(cParte, 2))
Else
aTexto(nContador) = aTexto(nContador) + aDezena(Mid(cParte, 2, 1))
If Right$(cParte, 1) <> "0" Then
aTexto(nContador) = aTexto(nContador) + "E "
nTamanho = 1
End If
End If
End If
If nTamanho = 1 Then
aTexto(nContador) = aTexto(nContador) + aUnid(Right(cParte, 1))
End If
Next
If Val(aGrupo(1) + aGrupo(2) + aGrupo(3) + aGrupo(4)) = 0 And
Val(aGrupo(5)) said:
cFinal = aTexto(5) + IIf(Val(aGrupo(5)) = 1, "CÊNTIMO", "CÊNTIMOS")
Else
cFinal = ""
cFinal = cFinal + IIf(Val(aGrupo(1)) <> 0, aTexto(1) +
IIf(Val(aGrupo(1)) > 1, "MIL MILHÕES ", "MILHAR DE MILHÃO "), "")
cFinal = cFinal + IIf(Val(aGrupo(2)) <> 0, aTexto(2) +
IIf(Val(aGrupo(2)) > 1, "MILHÕES ", "MILHÃO "), "")
If Val(aGrupo(2) + aGrupo(3) + aGrupo(4)) = 0 Then
cFinal = cFinal + "DE "
Else
cFinal = cFinal + IIf(Val(aGrupo(3)) <> 0, IIf(Val(aGrupo(3))
= 1, "MIL ", aTexto(3) + "MIL "), "")
End If
cFinal = cFinal + aTexto(4) + IIf(Val(aGrupo(1) + aGrupo(2) +
aGrupo(3) + aGrupo(4)) = 1, "EURO ", "EUROS ")
cFinal = cFinal + IIf(Val(aGrupo(5)) <> 0, "E " + aTexto(5) +
IIf(Val(aGrupo(5)) = 1, "CÊNTIMO", "CÊNTIMOS"), "")
 
J

jmc

Thanks Jea

I put the cursor before the field and then I inserted a bookmark (this field have a numeric value that is the result of the sum of others fields on the same page) then I tried to pass that value to a variable as you said (MyVariable = ActiveDocument.Bookmarks("MyBookmark").Range.Text) inside the macro, but did not work
Now, I have another problem that is how to display this value in words in the page of Word. Is necessary to add some control like a textbox and put it there. How I make that

HEELPPPP! While I'm alive

Sub macro1 (
dim valor as strin

valor = Extenso(ActiveDocument.Bookmarks("T").Range.Text

End Su

Function Extenso(nValor
Dim Text3 as strin
Text3 = "
'--------------------------------------------------------------------------
' Codigo de Substituição do ponto pela virgula, isto para permitir introdusir tanto o ponto como a virgul

a = Len(nValor
For i = 1 To
X = Left(nValor, i
K = Mid(nValor, i, 1
If K = "." The
K = ",
b = Len(X) -
txtnValor = Left(nValor, b
txtnValor = txtnValor &
nValor = txtnValor & Right(nValor, (a - (b + 1))
nValor = FormatNumber((nValor), 2
End I
Next

'-------------------------------------------------------------------------------
If IsNull(nValor) Or nValor = "" The
Extenso = "Valor Nulo
Text3 = Format(nValor, "##,##0.00") & " - " & Extens
Exit Functio

ElseIf nValor <= 0 The
Extenso = "Valor Inferior a 0
Text3 = Format(nValor, "##,##0.00") & " - " & Extens
Exit Functio

ElseIf nValor > 99999999999.99 The
Extenso = "Valor Superior 99 MIL MILHÕES de Euros
Text3 = Format(nValor, "##,##0.00") & " - " & Extens
Exit Functio
End I

Dim nContador, nTamanho As Intege
Dim cValor, cParte, cFinal As Strin
ReDim aGrupo(5), aTexto(5) As Strin
ReDim aUnid(19) As Strin
aUnid(1) = "UM ": aUnid(2) = "DOIS ": aUnid(3) = "TRES
aUnid(4) = "QUATRO ": aUnid(5) = "CINCO ": aUnid(6) = "SEIS
aUnid(7) = "SETE ": aUnid(8) = "OITO ": aUnid(9) = "NOVE
aUnid(10) = "DEZ ": aUnid(11) = "ONZE ": aUnid(12) = "DOZE
aUnid(13) = "TREZE ": aUnid(14) = "CATORZE ": aUnid(15) = "QUINZE
aUnid(16) = "DEZASSEIS ": aUnid(17) = "DEZASSETE ": aUnid(18) ="DEZOITO
aUnid(19) = "DEZANOVE

ReDim aDezena(9) As Strin
aDezena(1) = "DEZ ": aDezena(2) = "VINTE ": aDezena(3) = "TRINTA
aDezena(4) = "QUARENTA ": aDezena(5) = "CINQUENTA
aDezena(6) = "SESSENTA ": aDezena(7) = "SETENTA ": aDezena(8) ="OITENTA
aDezena(9) = "NOVENTA

ReDim aCentena(9) As Strin
aCentena(1) = "CENTO ": aCentena(2) = "DUZENTOS
aCentena(3) = "TREZENTOS ": aCentena(4) = "QUATROCENTOS
aCentena(5) = "QUINHENTOS ": aCentena(6) = "SEISCENTOS
aCentena(7) = "SETECENTOS ": aCentena(8) = "OITOCENTOS
aCentena(9) = "NOVECENTOS
cValor = Format$(nValor, "0000000000000.00"
aGrupo(1) = Mid$(cValor, 2, 3
aGrupo(2) = Mid$(cValor, 5, 3
aGrupo(3) = Mid$(cValor, 8, 3
aGrupo(4) = Mid$(cValor, 11, 3
aGrupo(5) = "0" + Mid$(cValor, 15, 2
For nContador = 1 To
cParte = aGrupo(nContador
nTamanho = Switch(Val(cParte) < 10, 1, Val(cParte) < 100, 2,Val(cParte) < 1000, 3
If nTamanho = 3 The
If Right$(cParte, 2) <> "00" The
aTexto(nContador) = aTexto(nContador) +aCentena(Left(cParte, 1)) + "E
nTamanho =
Els
aTexto(nContador) = aTexto(nContador) + IIf(Left$(cParte,1) = "1", "CEM ", aCentena(Left(cParte, 1))
End I
End I
If nTamanho = 2 The
If Val(Right(cParte, 2)) < 20 The
aTexto(nContador) = aTexto(nContador) + aUnid(Right(cParte, 2)
Els
aTexto(nContador) = aTexto(nContador) + aDezena(Mid(cParte, 2, 1)
If Right$(cParte, 1) <> "0" Then
aTexto(nContador) = aTexto(nContador) + "E "
nTamanho = 1
End If
End If
End If
If nTamanho = 1 Then
aTexto(nContador) = aTexto(nContador) + aUnid(Right(cParte,1))
End If
Next
If Val(aGrupo(1) + aGrupo(2) + aGrupo(3) + aGrupo(4)) = 0 And Val(aGrupo(5)) <> 0 Then
cFinal = aTexto(5) + IIf(Val(aGrupo(5)) = 1, "CÊNTIMO","CÊNTIMOS")
Else
cFinal = ""
cFinal = cFinal + IIf(Val(aGrupo(1)) <> 0, aTexto(1) + IIf(Val(aGrupo(1)) > 1, "MIL MILHÕES ", "MILHAR DE MILHÃO "), "")
cFinal = cFinal + IIf(Val(aGrupo(2)) <> 0, aTexto(2) + IIf(Val(aGrupo(2)) > 1, "MILHÕES ", "MILHÃO "), "")
If Val(aGrupo(2) + aGrupo(3) + aGrupo(4)) = 0 Then
cFinal = cFinal + "DE "
Else
cFinal = cFinal + IIf(Val(aGrupo(3)) <> 0, IIf(Val(aGrupo(3)) = 1, "MIL ", aTexto(3) + "MIL "), "")
End If
cFinal = cFinal + aTexto(4) + IIf(Val(aGrupo(1) + aGrupo(2) + aGrupo(3) + aGrupo(4)) = 1, "EURO ", "EUROS ")
cFinal = cFinal + IIf(Val(aGrupo(5)) <> 0, "E " + aTexto(5) + IIf(Val(aGrupo(5)) = 1, "CÊNTIMO", "CÊNTIMOS"), "")
End If
Extenso = cFinal
Text3 = Extenso
Labelextenso = Format(nValor, "##,##0.00") & " Euros"
End Function
 
J

Jean-Guy Marcil

jmc said:
Thanks Jean

I put the cursor before the field and then I inserted a bookmark
(this field have a numeric value that is the result of the sum of
others fields on the same page) then I tried to pass that value to a
variable as you said (MyVariable =
ActiveDocument.Bookmarks("MyBookmark").Range.Text)
inside the macro, but did not work.

Read carefully what I posted. I did not say to add abookmark in front of the
field, but to assign a bookmerk to the whole field. So, select the field (do
not just put the cursor next to it), then create a bookmark. Then it should
work.
Now, I have another problem that is how to display this value in
words in the page of Word. Is necessary to add some control
like a textbox and put it there. How I make that?

I do not understand what you want to do here. Isn't it already displayed on
the page (you just created a bookamrk...), and what do mean "in words". Do
you mean to say that you want to convert a numrical value like "12" to its
spelled out equivalent like "twelve"?
HEELPPPP! While I'm alive.

Sub macro1 ()
dim valor as string

valor = Extenso(ActiveDocument.Bookmarks("T").Range.Text)

End Sub

I do not understand this code:
Function Extenso(nValor)
Dim Text3 as string
Text3 = ""
'---------------------------------------------------------------------------
' Codigo de Substituição do ponto pela virgula, isto para permitir
introdusir tanto o ponto como a virgula
a = Len(nValor)
For i = 1 To a
X = Left(nValor, i)
K = Mid(nValor, i, 1)
If K = "." Then
K = ","
b = Len(X) - 1
txtnValor = Left(nValor, b)
txtnValor = txtnValor & K
nValor = txtnValor & Right(nValor, (a - (b + 1)))
nValor = FormatNumber((nValor), 2)
End If
Next i

'---------------------------------------------------------------------------
-----
If IsNull(nValor) Or nValor = "" Then
Extenso = "Valor Nulo"
Text3 = Format(nValor, "##,##0.00") & " - " & Extenso
Exit Function

ElseIf nValor <= 0 Then
Extenso = "Valor Inferior a 0"
Text3 = Format(nValor, "##,##0.00") & " - " & Extenso
Exit Function

ElseIf nValor > 99999999999.99 Then
Extenso = "Valor Superior 99 MIL MILHÕES de Euros"
Text3 = Format(nValor, "##,##0.00") & " - " & Extenso
Exit Function
End If

Dim nContador, nTamanho As Integer
Dim cValor, cParte, cFinal As String
ReDim aGrupo(5), aTexto(5) As String
ReDim aUnid(19) As String
aUnid(1) = "UM ": aUnid(2) = "DOIS ": aUnid(3) = "TRES "
aUnid(4) = "QUATRO ": aUnid(5) = "CINCO ": aUnid(6) = "SEIS "
aUnid(7) = "SETE ": aUnid(8) = "OITO ": aUnid(9) = "NOVE "
aUnid(10) = "DEZ ": aUnid(11) = "ONZE ": aUnid(12) = "DOZE "
aUnid(13) = "TREZE ": aUnid(14) = "CATORZE ": aUnid(15) = "QUINZE "
aUnid(16) = "DEZASSEIS ": aUnid(17) = "DEZASSETE ": aUnid(18) ="DEZOITO "
aUnid(19) = "DEZANOVE "

ReDim aDezena(9) As String
aDezena(1) = "DEZ ": aDezena(2) = "VINTE ": aDezena(3) = "TRINTA "
aDezena(4) = "QUARENTA ": aDezena(5) = "CINQUENTA "
aDezena(6) = "SESSENTA ": aDezena(7) = "SETENTA ": aDezena(8) ="OITENTA "
aDezena(9) = "NOVENTA "

ReDim aCentena(9) As String
aCentena(1) = "CENTO ": aCentena(2) = "DUZENTOS "
aCentena(3) = "TREZENTOS ": aCentena(4) = "QUATROCENTOS "
aCentena(5) = "QUINHENTOS ": aCentena(6) = "SEISCENTOS "
aCentena(7) = "SETECENTOS ": aCentena(8) = "OITOCENTOS "
aCentena(9) = "NOVECENTOS "
cValor = Format$(nValor, "0000000000000.00")
aGrupo(1) = Mid$(cValor, 2, 3)
aGrupo(2) = Mid$(cValor, 5, 3)
aGrupo(3) = Mid$(cValor, 8, 3)
aGrupo(4) = Mid$(cValor, 11, 3)
aGrupo(5) = "0" + Mid$(cValor, 15, 2)
For nContador = 1 To 5
cParte = aGrupo(nContador)
nTamanho = Switch(Val(cParte) < 10, 1, Val(cParte) < 100, 2,Val(cParte) < 1000, 3)
If nTamanho = 3 Then
If Right$(cParte, 2) <> "00" Then
aTexto(nContador) = aTexto(nContador)
+aCentena(Left(cParte, 1)) + "E "
nTamanho = 2
Else
aTexto(nContador) = aTexto(nContador) +
IIf(Left$(cParte,1) = "1", "CEM ", aCentena(Left(cParte, 1)))
End If
End If
If nTamanho = 2 Then
If Val(Right(cParte, 2)) < 20 Then
aTexto(nContador) = aTexto(nContador) + aUnid(Right(cParte, 2))
Else
aTexto(nContador) = aTexto(nContador) + aDezena(Mid(cParte, 2, 1))
If Right$(cParte, 1) <> "0" Then
aTexto(nContador) = aTexto(nContador) + "E "
nTamanho = 1
End If
End If
End If
If nTamanho = 1 Then
aTexto(nContador) = aTexto(nContador) + aUnid(Right(cParte,1))
End If
Next
If Val(aGrupo(1) + aGrupo(2) + aGrupo(3) + aGrupo(4)) = 0 And
Val(aGrupo(5)) said:
cFinal = aTexto(5) + IIf(Val(aGrupo(5)) = 1, "CÊNTIMO","CÊNTIMOS")
Else
cFinal = ""
cFinal = cFinal + IIf(Val(aGrupo(1)) <> 0, aTexto(1) +
IIf(Val(aGrupo(1)) > 1, "MIL MILHÕES ", "MILHAR DE MILHÃO "), "")
cFinal = cFinal + IIf(Val(aGrupo(2)) <> 0, aTexto(2) +
IIf(Val(aGrupo(2)) > 1, "MILHÕES ", "MILHÃO "), "")
If Val(aGrupo(2) + aGrupo(3) + aGrupo(4)) = 0 Then
cFinal = cFinal + "DE "
Else
cFinal = cFinal + IIf(Val(aGrupo(3)) <> 0, IIf(Val(aGrupo(3))
= 1, "MIL ", aTexto(3) + "MIL "), "")
End If
cFinal = cFinal + aTexto(4) + IIf(Val(aGrupo(1) + aGrupo(2) +
aGrupo(3) + aGrupo(4)) = 1, "EURO ", "EUROS ")
cFinal = cFinal + IIf(Val(aGrupo(5)) <> 0, "E " + aTexto(5) +
IIf(Val(aGrupo(5)) = 1, "CÊNTIMO", "CÊNTIMOS"), "")
 
J

Jean-Guy Marcil

Sorry, it was sent too fast... Outlook Express can be funny like that... I
do not know what key combination is the equivalent of clicking "Send", but
it happens once in a while, when I type too fast, then I hit some keys
simultaneously, and the message is sent... I never can tell what keys I have
hit... The same for spelling. Sometimes it does not spell check... No idea
why!

Anyway,...

jmc said:
Thanks Jean

I put the cursor before the field and then I inserted a bookmark
(this field have a numeric value that is the result of the sum of
others fields on the same page) then I tried to pass that value to a
variable as you said (MyVariable =
ActiveDocument.Bookmarks("MyBookmark").Range.Text)
inside the macro, but did not work.

Read carefully what I posted. I did not say to add a bookmark in front of
the
field, but to assign a bookmark to the whole field. So, select the field (do
not just put the cursor next to it), then create a bookmark. Then it should
work.
Now, I have another problem that is how to display this value in
words in the page of Word. Is necessary to add some control
like a textbox and put it there. How I make that?

I do not understand what you want to do here. Isn't it already displayed on
the page (you just created a bookmark...), and what do mean "in words". Do
you mean to say that you want to convert a numerical value like "12" to its
spelled out equivalent like "twelve" (by using your "extenso" function I
guess...)? If so, where exactly would you like this result to go? What about
the numeric one? what happens to it once the written form is put in the
document?


--
Cheers!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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