-
August 7th, 2012, 08:00 AM
#1
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 ?
-
August 8th, 2012, 07:52 AM
#2
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.
-
August 8th, 2012, 05:13 PM
#3
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 ?
-
August 8th, 2012, 06:18 PM
#4
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 ...
-
August 10th, 2012, 06:55 PM
#5
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.
-
August 12th, 2012, 01:50 PM
#6
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
-
August 13th, 2012, 06:35 AM
#7
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?
-
August 13th, 2012, 03:17 PM
#8
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.
-
August 13th, 2012, 09:49 PM
#9
Re: Stuck With Visual Basic 6.0
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|