IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

  1. #1
    Membre à l'essai
    Homme Profil pro
    médecin
    Inscrit en
    Juin 2018
    Messages
    15
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Suisse

    Informations professionnelles :
    Activité : médecin

    Informations forums :
    Inscription : Juin 2018
    Messages : 15
    Points : 17
    Points
    17
    Par défaut Calculer à partir de valeurs de 2 contrôles de contenu et mettre le résultat dans un 3e contrôle de contenu Wo
    Bonjour,

    je débute sur ce forum. J'ai Office 365, dernière version de Word. J'essaie de calculer l'IMC et le mettre dans un contrôle de contenu, à partir de la taille et du poids, saisis préalablement dans le texte, dans 2 contrôles de contenu.
    Le texte dans word est le suivant :
    mesure 168 cm, pèse 78 kg (BMI
    J'ai trouvé le code d'origine ici : https://gregmaxey.com/word_tip_pages...perations.html


    Voici mon code:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    Option Explicit
    Private Sub Document_ContentControlOnExit(ByVal CC As ContentControl, Cancel As Boolean)
    Dim oCC As ContentControl
    'Handle OnExit event bug in Word 2007
     ' If Application.Version < "14.0" Then MAIN.SetDeveloperTabActive
      Select Case CC.Title
      Case "Taillecm", "Poidskg"
    'Validate/format data entered in variable content control.
        If Not IsNumeric(CC.Range.Text) Then
          Cancel = True
          Beep
          CC.Range.Select
          Exit Sub
        Else
          CC.Range.Text = FormatValue(CC.Range.Text)
        End If
    'Update content control contents.
        Set oCC = ActiveDocument.SelectContentControlsByTitle("BMI").Item(1)
        With oCC
          .LockContents = False
          .Range.Text = FormatValue(MathAdd(ActiveDocument.SelectContentControlsByTitle("Taillecm").Item(1).Range.Text, _
          ActiveDocument.SelectContentControlsByTitle("Poidskg").Item(1).Range.Text))
          .LockContents = True
        End With
      End Select
      Set oCC = Nothing
    lbl_Exit:
      Exit Sub
    End Sub
     
    Function FormatValue(ByRef pStr As String) As String
    'This function applies formatting to the numbers entered in the variable content controls _
     and to the results of the mathematical operation functions.
      If InStr(pStr, ".") > 0 Then
        FormatValue = Format(pStr, "##,####,####,##0.0###########;(##,####,####,##0.0###########);0")
      Else
        FormatValue = Format(pStr, "##,###,###,###;(##,###,###,###);0")
      End If
    lbl_Exit:
      Exit Function
    End Function
     
    Function MathAdd(ByRef A As Double, B As Double, C As Double) As Double
    'This is the mathematical operation function to compute the sum of the addition _
       content control variables.
      MathAdd = (Poidskg / ((Taillecm / 100) * (Taillecm / 100)))
    lbl_Exit:
      Exit Function
    End Function
    Dès que j'arrive dans le champ BMI sous word, je reçois l'erreur "MathAdd Erreur de compilation, argument non facultatif".



    Est-ce que quelqu'un aurait la gentillesse de m'expliquer comment je dois corriger mon code s'il vous plaît ? Merci d'avance

  2. #2
    Membre à l'essai
    Homme Profil pro
    médecin
    Inscrit en
    Juin 2018
    Messages
    15
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Suisse

    Informations professionnelles :
    Activité : médecin

    Informations forums :
    Inscription : Juin 2018
    Messages : 15
    Points : 17
    Points
    17
    Par défaut Résultat partiel - qiuestion sur le calcul
    Rebonjour,

    j'ai trouvé une des erreurs et mon code fait presque ce qu'il doit, sauf que le format des nombres ne me va pas et que le calcul est faux. Merci pour un peu d'aide, voici mon code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    Option Explicit
    Private Sub Document_ContentControlOnExit(ByVal CC As ContentControl, Cancel As Boolean)
    Dim oCC As ContentControl
    'Handle OnExit event bug in Word 2007
     ' If Application.Version < "14.0" Then MAIN.SetDeveloperTabActive
      Select Case CC.Title
      Case "Taillecm", "Poidskg"
    'Validate/format data entered in variable content control.
        If Not IsNumeric(CC.Range.Text) Then
          Cancel = True
          Beep
          CC.Range.Select
          Exit Sub
        Else
          CC.Range.Text = FormatValue(CC.Range.Text)
        End If
    'Update content control contents.
        Set oCC = ActiveDocument.SelectContentControlsByTitle("BMI").Item(1)
        With oCC
          .LockContents = False
          .Range.Text = FormatValue(MathAdd(ActiveDocument.SelectContentControlsByTitle("Taillecm").Item(1).Range.Text, ActiveDocument.SelectContentControlsByTitle("Poidskg").Item(1).Range.Text))
          .LockContents = True
        End With
      End Select
      Set oCC = Nothing
    lbl_Exit:
      Exit Sub
    End Sub
     
    Function FormatValue(ByRef pStr As String) As String
    'This function applies formatting to the numbers entered in the variable content controls _
     and to the results of the mathematical operation functions.
      If InStr(pStr, ".") > 0 Then
        FormatValue = Format(pStr, "#.#;(#.#);#.#")
      Else
        FormatValue = Format(pStr, "#.#;(#.#);#.#")
      End If
    lbl_Exit:
      Exit Function
    End Function
     
    Function MathAdd(ByRef Poidskg As Double, Taillecm As Double) As Double
    'This is the mathematical operation function to compute the BMI based on the  content control variables.
      MathAdd = Poidskg / ((Taillecm / 10) ^ 2)
    lbl_Exit:
      Exit Function
    End Function

  3. #3
    Membre à l'essai
    Homme Profil pro
    médecin
    Inscrit en
    Juin 2018
    Messages
    15
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Suisse

    Informations professionnelles :
    Activité : médecin

    Informations forums :
    Inscription : Juin 2018
    Messages : 15
    Points : 17
    Points
    17
    Par défaut Code totalement corrigé
    Alors finalement, j'ai trouvé :
    J'ai mis les paramètres régionaux de Windows de sorte à ce que le séaprateur décimal soit le point.

    Le code qui me convient est :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    Option Explicit
    Private Sub Document_ContentControlOnExit(ByVal CC As ContentControl, Cancel As Boolean)
    Dim oCC As ContentControl
    'Handle OnExit event bug in Word 2007
     ' If Application.Version < "14.0" Then MAIN.SetDeveloperTabActive
      Select Case CC.Title
      Case "Taillecm", "Poidskg"
    'Validate/format data entered in variable content control.
        If Not IsNumeric(CC.Range.Text) Then
          Cancel = True
          Beep
          CC.Range.Select
          Exit Sub
        Else
          'CC.Range.Text = FormatValue(CC.Range.Text)
        End If
    'Update content control contents.
        Set oCC = ActiveDocument.SelectContentControlsByTitle("BMI").Item(1)
        With oCC
          .LockContents = False
          .Range.Text = FormatValue(MathAdd(ActiveDocument.SelectContentControlsByTitle("Taillecm").Item(1).Range.Text, ActiveDocument.SelectContentControlsByTitle("Poidskg").Item(1).Range.Text))
          .LockContents = True
        End With
      End Select
      Set oCC = Nothing
    lbl_Exit:
      Exit Sub
    End Sub
     
    Function FormatValue(ByRef pStr As String) As String
    'This function applies formatting to the numbers entered in the variable content controls _
     and to the results of the mathematical operation functions.
      If InStr(pStr, ".") > 0 Then
        FormatValue = Format(pStr, "#.#;#.#;#.#")
      Else
        FormatValue = Format(pStr, "#.#;#.#;#.#")
      End If
    lbl_Exit:
      Exit Function
    End Function
     
    Function MathAdd(ByRef Taillecm As Double, Poidskg As Double) As Double
    'This is the mathematical operation function to compute the BMI based on the  content control variables.
      MathAdd = Poidskg / ((Taillecm) / 100) ^ 2
    lbl_Exit:
      Exit Function
    End Function

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 14
    Dernier message: 15/08/2018, 16h41
  2. [Débutant] Valeur calculé à partir d'une autre liste
    Par pliza dans le forum Développement Sharepoint
    Réponses: 6
    Dernier message: 29/09/2014, 01h09
  3. [CakePHP] Calcul avec une valeur recuperée à partir autre modele
    Par PrincessKC dans le forum Bibliothèques et frameworks
    Réponses: 2
    Dernier message: 04/06/2014, 12h36
  4. [XL-2007] calcul des moyennes journalières à partir des valeurs instantanées
    Par Jennyfer 75 dans le forum Excel
    Réponses: 10
    Dernier message: 14/01/2014, 17h40
  5. Trigger et calcul d'une valeur à partir de plusieures tables
    Par tomasi dans le forum SQL Procédural
    Réponses: 1
    Dernier message: 31/07/2008, 16h55

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo