PDA

Click to See Complete Forum and Search --> : [RESOLVED] need assistance in debugging my program


neo_phyte
Nov 17th, 2005, 08:28 AM
how do i debug my program? error description "Run time error '91':" Object variable or With block variable not set


Here is my Code:


Private Sub Command1_Click()

Dim objWdApp As Word.Application
Dim objWdRange As Word.Range
Dim objWdDoc As Word.Document

Dim count As Integer
Dim wdText As String

count = 0
wdText = Text1.Text

Set objWdRange = objWdDoc.Content
With objWdRange.Find

Do While .Execute(FindText:=wdText, Format:=False) = True
count = count + 1
Loop


End With
MsgBox "There are " & count + "words", vbInformation
Set objWdRange = Nothing
End Sub



your help is very much appreciated. thanks in advance.

DKenny
Nov 17th, 2005, 08:30 AM
You have not Set the objWdDoc obect variable to anything...

neo_phyte
Nov 17th, 2005, 08:43 AM
how do i set the object variable, can you give me idea on this... please post your code...

DKenny
Nov 17th, 2005, 09:01 AM
If you want to set the objWdDoc to a new document then use

Set objWdDoc = objWdApp.Documents.Add

neo_phyte
Nov 17th, 2005, 09:23 AM
how about i am going to count the occurrence of words in the already opened document, what code i am going to use.... example instances are when certain opened documents i am going to use this program.. do i use ActiveDocument? but how?

neo_phyte
Nov 17th, 2005, 09:27 AM
thanks for a quick reply...

how about this:


Set objWdDoc = objWdApp.ActiveDocument.Content


but there is a an error: This command is not available since no document is open.. it is very much confusing error since i already opened document to count the occurence.. by the way, i am using here microsoft word 2003.

DKenny
Nov 17th, 2005, 09:36 AM
This command is not available since no document is open..

OK, in that case you need to open the document you wish to work with.


Set objWdDoc = objWdApp.Documents.Open(FileName:="insert your path and file name here")

neo_phyte
Nov 17th, 2005, 09:50 AM
thanks again, you mean everytime i opened the document i have to change my code? Changing the filename and the path of the filename? how about ActiveDocument property? I think it would be nicer and easier if it is already opened then after opening run the program. How will i do that?

DKenny
Nov 17th, 2005, 09:55 AM
Try this, but replace "Test.doc" with the name of the document that you wish to use.

Private Sub Command1_Click()

Dim objWdRange As Word.Range
Dim objWdDoc As Word.Document

Dim count As Integer
Dim wdText As String

count = 0
wdText = Text1.Text

Set objWdDoc = Application.Documents("test.doc")
Set objWdRange = objWdDoc.Content
With objWdRange.Find

Do While .Execute(FindText:=wdText, Format:=False) = True
count = count + 1
Loop


End With
MsgBox "There are " & count + "words", vbInformation
Set objWdRange = Nothing
Set objWdDoc = Nothing
End Sub

neo_phyte
Nov 17th, 2005, 10:06 AM
thanks.... i already made it..... success.....



Private Sub Command1_Click()

'Dim objWdApp As Word.Application

Dim objWdRange As Word.Range
Dim objWdDoc As Word.Document
Dim count As Integer
Dim wdText As String

count = 0
wdText = Text1.Text
'Set objWdApp = New Word.Application
Set objWdDoc = Application.ActiveDocument
'Set objWdDoc = objWdApp.ActiveDocument.Content
Set objWdRange = objWdDoc.Content

'objWdApp.Visible = True

With objWdRange.Find

Do While .Execute(FindText:=wdText, Format:=False) = True
count = count + 1
Loop

End With
MsgBox "There are " & count, vbInformation
Set objWdRange = Nothing
Set objWdDoc = Nothing
'Set objWdApp = Nothing
End Sub



thanks for the time that you shared to help me.... i changed to ActiveDocument Property..... but you shared a lot to me.... thanks again.