CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Stuck With Visual Basic 6.0

  1. #1
    Join Date
    Aug 2012
    Posts
    2

    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
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Stuck With Visual Basic 6.0

    The code is obviously designed so that the first time click will open the word document and do a first Find.
    When the statement executes, for the second time, there might not be a visible result, if the text1.text is not found anymore in the document.

    I tested your code in a small document and it seems to work perfectly.

  3. #3
    Join Date
    Aug 2012
    Posts
    2

    Re: Stuck With Visual Basic 6.0

    @WoF : Thanks for you reply, text that I want to find is can be found in my document, but still nothing happens with my button.

    I modified my code to be like this :

    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.Documents.Close
            doc.Documents.Open "\..\myDoc.doc" 
            doc.Selection.Find.Execute Text1.Text 
        End If 
    End Sub
    I have to close the opened document and reopen the document to do find text, but I think it's not effective.

    you say that my code is work perfectly, Is that probably caused by my Visual Basic ?

  4. #4
    Join Date
    Jul 2005
    Posts
    1,083

    Re: Stuck With Visual Basic 6.0

    Isn't VAL a reserved word?
    JG


    ... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...

  5. #5
    Join Date
    Dec 2008
    Location
    Step Into(F11)
    Posts
    465

    Re: Stuck With Visual Basic 6.0

    I have to close the opened document and reopen the document to do find text, but I think it's not effective.
    Try the following way .
    Code:
    Option Explicit
    Dim doc As Word.Application
    Dim x As String
    
    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
                x = MsgBox("text box data is available in word doc" & "Do you want to search again", vbYesNo)
               If x = vbYes Then
                 doc.ActiveDocument.Close
                 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
    Last edited by firoz.raj; August 12th, 2012 at 03:43 PM.

  6. #6
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Stuck With Visual Basic 6.0

    That never closes WORD, or the DOC. Memory leak time...

    This uses a RTB: It finds (and splits on the ":")

    Code:
    Option Explicit
    
    Private Sub Form_Load()
    Dim a%, b%, q%
    With rtb
      .Text = "In process reject rate for Department 1 : 4.34 %" & vbCrLf & _
          "In process reject rate for Department 1 : 124.34 %" & vbCrLf
      q = .Find(":") - 1
      Do While q > 0
        a = .Find(":", q)
        b = .Find(vbCrLf, a)
        .SelStart = a + 1
        .SelLength = b - a + 1
        .SelBold = True
        q = .Find(":", b + 1)
      Loop
      .SelStart = 0
    End With
    End Sub
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  7. #7
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Stuck With Visual Basic 6.0

    Although Val being a reserved word, VB wouldn't mind the usage of Val for a variable within a sub, as long as you dont make use of the Val() function in the same block of code. So making a variable Val being public or global to a form is indeed actually a bad idea, becauses it would throw up an error if you try to use Val() anywhere.

    Apart from that, @goesseog, I said your code works perfectly, meaning it works like expected. Please try a document where the text to find is actually occuring multiple times. Then try your button and see how it works. If the text to find does occur only once in the doc, it looks indeed as if the button wouldn't do anything.

    If the code is effective depends on what you actually want to achieve and where this button is used. I couldn't say.
    Maybe you add a close button?

  8. #8
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,349

    Re: Stuck With Visual Basic 6.0

    Even though Vb may allow it in some case I would strongly suggest that you avoid using any variable name that is the same or even very close to that of the function names within VB. Using vars such as Val is a very bad habit to get into and it will come back to bite you if you maintain this practice.
    Always use [code][/code] tags when posting code.

  9. #9
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Stuck With Visual Basic 6.0

    aVal or bVal work...
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)