O Tal Antiquado
O Tal Antiquado

Reputation: 397

How do I print a price depending on the dropdown value that is selected?

I have this table with the following headers: Product, Quantity, Price/Kg, Total

The cells in the column "Product" are all dropdown lists with the products the company offers and the "Price/Kg" are TextInputs.

I want to print the designated price onto the TextInput, depending on the dropdown's value.

I have this subroutine, but it doesn't do anything:

Sub getPreco()

Select Case ActiveDocument.FormFields("produtos").DropDown.Value
    Case "Amêijoas Vietnamitas"
        ActiveDocument.FormFields("TextBox1").TextInput.Result = "1 euro"
    Case "Gambas"
        ActiveDocument.FormFields("TextBox1").TextInput.Result = "2 euros"
    Case Else
        ActiveDocument.FormFields("TextBox1").TextInput.Clear
        MsgBox "Price not yet defined"

End Select
End Sub

Upvotes: 0

Views: 30

Answers (2)

macropod
macropod

Reputation: 13515

You don't need any VBA for this. All you need do is check the dropdown formfield's 'Calculate on Exit' property and employ a formula field coded along the lines of:

{IF{REF produtos}= "Amêijoas Vietnamitas" "€1" {IF{REF produtos}= "Gambas" "€2" "Preco ainda nao definido"}}

Note: The field brace pairs (i.e. '{ }') for the above example are all created in the document itself, via Ctrl-F9 (Cmd-F9 on a Mac or, if you’re using a laptop, you might need to use Ctrl-Fn-F9); you can't simply type them or copy & paste them from this message. Nor is it practical to add them via any of the standard Word dialogues. The spaces represented in the field constructions are all required.

Upvotes: 1

O Tal Antiquado
O Tal Antiquado

Reputation: 397

Sub getPreco()

Select Case ActiveDocument.FormFields("produtos").DropDown.Value
    Case 1
        ActiveDocument.FormFields("Texto1").Result = "1 euro"
    Case 2
        ActiveDocument.FormFields("Texto1").Result = "2 euros"
    Case Else
        ActiveDocument.FormFields("Texto1").TextInput.Clear
        MsgBox "Preco ainda nao definido"

End Select
End Sub

Upvotes: 0

Related Questions