Results 1 to 11 of 11

Thread: Stuck With Visual Basic 6.0

Hybrid View

  1. #1
    New Member
    Join Date
    Aug 12
    Posts
    4

    Stuck With Visual Basic 6.0

    I am a newbie, what I want to do is open a word document and do some find actions from Text1, but when I click the commandButton in second time, there is no action with the commandButton
    here is my code :

    Code:
    Dim doc As Word.Application 
    Dim val As Boolean 
    
    Private Sub Command1_Click() 
        If val <> True Then 
            Set doc = New Word.Application 
            doc.Visible = True 
            
            doc.Documents.Open "\..\myDoc.doc" 
            
            doc.Selection.Find.Execute Text1.Text 
            val = True 
        ElseIf val = True Then 
            doc.Selection.Find.Execute Text1.Text 
        End If 
    End Sub

    Could you give me the solutions please ?

  2. #2
    PowerPoster
    Join Date
    Feb 12
    Location
    West Virginia
    Posts
    4,978

    Re: Stuck With Visual Basic 6.0

    No idea about working with Word from VB but I do see that you are using a reserved word for a variable name which should be changed.

    Val is used in VB to get the numeric value of a variable as in Val(MyString)

    You also do not need the ElseIF a simple Else will do as there are only 2 possible values for a boolean eithe rit is true or it is not no need to test it twice.

  3. #3
    New Member
    Join Date
    Aug 12
    Posts
    4

    Re: Stuck With Visual Basic 6.0

    @DataMiser : Thanks for your reply, I know what the function of val() and I have changed the variable name and if else statement. But that doesn't solve my problem.

  4. #4
    Addicted Member
    Join Date
    Jun 09
    Location
    C:\Windows\SysWOW64\
    Posts
    197

    Re: Stuck With Visual Basic 6.0

    Put a breakpoint at the beginning of the Comman1_Click event and step through the code. That should give you a hint of what is actually happening...


  5. #5
    Hyperactive Member
    Join Date
    Jun 04
    Posts
    468

    Re: Stuck With Visual Basic 6.0

    There doesn't appear to be anything wrong with the code. But if the search value was not found, it would appear as though no action was taken.

  6. #6
    Addicted Member
    Join Date
    Jun 09
    Location
    C:\Windows\SysWOW64\
    Posts
    197

    Re: Stuck With Visual Basic 6.0

    The Execute function returns a boolean value. If the search string is not found, it will return False, so it is up to you to check it.


  7. #7
    New Member
    Join Date
    Aug 12
    Posts
    4

    Re: Stuck With Visual Basic 6.0

    @Cube8 : I'm not really understand what you say, the search value that I type is found in my word document, so what do I have to do ?

  8. #8
    Frenzied Member
    Join Date
    Jan 09
    Location
    Watch Window(Shift+f9)
    Posts
    1,434

    Re: Stuck With Visual Basic 6.0

    Hi in build function Execute returns true if something is found .and it returns false if does not found .
    so try the following way .and you will get the idea . hope it might be a great help .
    Code:
    Option Explicit
    Dim doc As Word.Application
    Dim val As Boolean
    
    Private Sub Command1_Click()
             Set doc = New Word.Application
             doc.Visible = True
             doc.Documents.Open "C:\TestProject\myDoc.doc"
             doc.Selection.Find.Execute Text1.Text
             If doc.Selection.Find.Execute = True Then
                 MsgBox ("text box data is available in word doc")
             Else
                MsgBox ("text box data is not availble in word doc")
            End If
        End Sub

  9. #9
    New Member
    Join Date
    Aug 12
    Posts
    4

    Re: Stuck With Visual Basic 6.0

    @firoz.raj : everytime I click the button, new word application and the document will open, what I want is open new word application and the document once and do some actions without reopen new word application and the document.

  10. #10
    Frenzied Member
    Join Date
    Aug 11
    Location
    B.C., Canada
    Posts
    1,946

    Re: Stuck With Visual Basic 6.0

    Simple fix from firoz

    Code:
    Option Explicit
    Dim doc As Word.Application
    
    
    
    Private Sub Command1_Click()
             doc.Selection.Find.Execute Text1.Text
             If doc.Selection.Find.Execute = True Then
                 MsgBox ("text box data is available in word doc")
             Else
                MsgBox ("text box data is not availble in word doc")
            End If
        End Sub 
    
    Private Sub Form_Load()
    Set doc = New Word.Application
    doc.Visible = True
    doc.Documents.Open "C:\TestProject\myDoc.doc"
    End Sub
    Last edited by Max187Boucher; Aug 11th, 2012 at 04:30 PM.

  11. #11
    Frenzied Member
    Join Date
    Jan 09
    Location
    Watch Window(Shift+f9)
    Posts
    1,434

    Re: Stuck With Visual Basic 6.0

    everytime I click the button, new word application and the document will open, what I want is open new word application and the document once and do some actions without reopen new word application and the document.
    Try the following way .
    Code:
    Option Explicit
    Dim doc As Word.Application
    Dim x As String
    
    Private Sub Command1_Click()
                doc.Selection.Find.Execute Text1.Text
             If doc.Selection.Find.Execute = True Then
                x = MsgBox("text box data is available in word doc" & "    Do you want to search again", vbYesNo)
               If x = vbYes Then
                 doc.ActiveDocument.Close
                 Set doc = New Word.Application
                 doc.Visible = True
                 doc.Documents.Open "C:\TestProject\myDoc.doc"
                 doc.Selection.Find.Execute Text1.Text
               ElseIf x = vbNo Then
                 Unload Me
             Else
                MsgBox ("text box data is not availble in word doc")
              End If
            End If
        End Sub
    
    Private Sub Form_Load()
             Set doc = New Word.Application
             doc.Visible = True
             doc.Documents.Open "C:\TestProject\myDoc.doc"
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •