Results 1 to 10 of 10

Thread: [RESOLVED] need assistance in debugging my program

  1. #1

    Thread Starter
    Lively Member neo_phyte's Avatar
    Join Date
    Nov 2005
    Location
    Cebu, Philippines
    Posts
    71

    Resolved [RESOLVED] need assistance in debugging my program

    how do i debug my program? error description "Run time error '91':" Object variable or With block variable not set


    Here is my Code:

    VB Code:
    1. Private Sub Command1_Click()
    2.  
    3. Dim objWdApp As Word.Application
    4. Dim objWdRange As Word.Range
    5. Dim objWdDoc As Word.Document
    6.  
    7. Dim count As Integer
    8. Dim wdText As String
    9.  
    10. count = 0
    11. wdText = Text1.Text
    12.  
    13. Set objWdRange = objWdDoc.Content
    14. With objWdRange.Find
    15.      
    16.  Do While .Execute(FindText:=wdText, Format:=False) = True
    17.     count = count + 1
    18.  Loop
    19.  
    20.  
    21. End With
    22. MsgBox "There are " & count + "words", vbInformation
    23. Set objWdRange = Nothing
    24. End Sub


    your help is very much appreciated. thanks in advance.

  2. #2
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: need assistance in debugging my program

    You have not Set the objWdDoc obect variable to anything...
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  3. #3

    Thread Starter
    Lively Member neo_phyte's Avatar
    Join Date
    Nov 2005
    Location
    Cebu, Philippines
    Posts
    71

    Re: need assistance in debugging my program

    how do i set the object variable, can you give me idea on this... please post your code...

  4. #4
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: need assistance in debugging my program

    If you want to set the objWdDoc to a new document then use

    VB Code:
    1. Set objWdDoc = objWdApp.Documents.Add
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  5. #5

    Thread Starter
    Lively Member neo_phyte's Avatar
    Join Date
    Nov 2005
    Location
    Cebu, Philippines
    Posts
    71

    Re: need assistance in debugging my program

    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?

  6. #6

    Thread Starter
    Lively Member neo_phyte's Avatar
    Join Date
    Nov 2005
    Location
    Cebu, Philippines
    Posts
    71

    Re: need assistance in debugging my program

    thanks for a quick reply...

    how about this:

    VB Code:
    1. 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.

  7. #7
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: need assistance in debugging my program

    Quote Originally Posted by neo_phyte
    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.


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

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  8. #8

    Thread Starter
    Lively Member neo_phyte's Avatar
    Join Date
    Nov 2005
    Location
    Cebu, Philippines
    Posts
    71

    Re: need assistance in debugging my program

    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?

  9. #9
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: need assistance in debugging my program

    Try this, but replace "Test.doc" with the name of the document that you wish to use.

    VB Code:
    1. Private Sub Command1_Click()
    2.  
    3. Dim objWdRange As Word.Range
    4. Dim objWdDoc As Word.Document
    5.  
    6. Dim count As Integer
    7. Dim wdText As String
    8.  
    9. count = 0
    10. wdText = Text1.Text
    11.  
    12. Set objWdDoc = Application.Documents("test.doc")
    13. Set objWdRange = objWdDoc.Content
    14. With objWdRange.Find
    15.      
    16.  Do While .Execute(FindText:=wdText, Format:=False) = True
    17.     count = count + 1
    18.  Loop
    19.  
    20.  
    21. End With
    22. MsgBox "There are " & count + "words", vbInformation
    23. Set objWdRange = Nothing
    24. Set objWdDoc = Nothing
    25. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  10. #10

    Thread Starter
    Lively Member neo_phyte's Avatar
    Join Date
    Nov 2005
    Location
    Cebu, Philippines
    Posts
    71

    Re: need assistance in debugging my program

    thanks.... i already made it..... success.....

    VB Code:
    1. Private Sub Command1_Click()
    2.  
    3. 'Dim objWdApp As Word.Application
    4.  
    5. Dim objWdRange As Word.Range
    6. Dim objWdDoc As Word.Document
    7. Dim count As Integer
    8. Dim wdText As String
    9.  
    10. count = 0
    11. wdText = Text1.Text
    12. 'Set objWdApp = New Word.Application
    13. Set objWdDoc = Application.ActiveDocument
    14. 'Set objWdDoc = objWdApp.ActiveDocument.Content
    15. Set objWdRange = objWdDoc.Content
    16.  
    17. 'objWdApp.Visible = True
    18.  
    19. With objWdRange.Find
    20.      
    21.  Do While .Execute(FindText:=wdText, Format:=False) = True
    22.     count = count + 1
    23.  Loop
    24.  
    25. End With
    26. MsgBox "There are " & count, vbInformation
    27. Set objWdRange = Nothing
    28. Set objWdDoc = Nothing
    29. 'Set objWdApp = Nothing
    30. 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.

Posting Permissions

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



Click Here to Expand Forum to Full Width