If statement

  • 10 years ago

    I have created a form that allows the user to create a report document.  The user clicks "Print Report" from the "File" menu and a MS Word document opens up with the information they want to display.  I created a mock form with three text fields: First Name, Middle Name, and Last Name.  Beside those fields I have a checkbox.  I want the user to be able to select which fields they want to show on the report.  Below is the code I'm using for my conditional statement:

    If First_Name_Check.Checked Then

       With oWord.ActiveDocument.Sections.Item(1)

          .Range.Font.Color = Word.WdColor.wdColorBlack

          .Range.Text = "First Name: " & First_Name.Text

       End With

    End If

    If Last_Name_Check.Checked Then

       With oWord.ActiveDocument.Sections.Item(1)

          .Range.Font.Color = Word.WdColor.wdColorBlack

          .Range.Text = "Last Name: " & Last_Name.Text

       End With

    End If

    The problem is that if I select both checkboxes, when I click "Print Report," only the last name information displays instead of both fields.  What am I doing wrong?????

  • 10 years ago
    The problem is you are over-writing the first name with the last name instead of appending to it.  For example if you wanted to put the first name and last name in a textbox and did the following you would only end up with the last name.
    Textbox1.text = First_Name.text
    Textbox1.text = Last_Name.Text
    I'm not sure if this works in this case but try this in your second if statement:
    .Range.Text &= "Last Name: " & Last_Name.Text
    You could also try combining the output first and then writing it to the document. For example:
            Dim output As String = ""
    
            If First_Name_Check.Checked Then output = "First Name: " & First_Name.Text
            If Last_Name_Check.Checked Then output &= " Last Name: " & Last_Name.Text
    
            With oWord.ActiveDocument.Sections.Item(1)
                .Range.Font.Color = Word.WdColor.wdColorBlack
                .Range.Text = output.TrimStart
            End With
  • 10 years ago
    i actually figured out the problem...thanks for your help!

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Before software should be reusable, it should be usable.” - Ralph Johnson