code hangs in vb6

My code stops and hangs on line 5.  I can't figure out what it could be.  When looking at the code, do you think it could be the file?  I am using vb6.

Private Sub Command1_Click()
Dim objMSWord As Word.Application
Dim myDOc As Word.Document
Set objMSWord = CreateObject("Word.Application")
Set myDOc = objMSWord.Documents.Open("C:\Users\Alpesh\Desktop\111114.txt")

For Each p In myDOc.Paragraphs
  If InStr(p.Range.Text, "BlockedIP") > 0 Then
   'Skipping - found text
  Else
    p.Range.Delete
  End If
Next

myDOc.SaveAs ("c:\Users\Me\Desktop\052214_2.txt")
myDOc.Close
Set myDOc = Nothing
objMSWord.Quit
End Sub

Open in new window

al4629740Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Glen RichmondCustomer Reporting Programmer.Commented:
silly question but i feel i need to ask it..

you have and your application has permissions to access "Alpesh" user files and desktop?
0
Glen RichmondCustomer Reporting Programmer.Commented:
also you dont need this
Set objMSWord = CreateObject("Word.Application")

as your object is already a word COM object becuse of this early binding line
Dim objMSWord As Word.Application

But make sure you have the word com libary referenced! (youll get an error if it wasnt anyway)

Also is there any reason you are using word instead of FileSystem/FileScripting object to open and read a txt file
could try somthign like this:-
it will cycle through all files in DIR looking for txt files and then scanning them line by line for your instr..
It may need some tweeking to fit you demand, but it has the benefit of no hard coding of fiel name, just the DIR path.

 Function ProcessDataFiles() As Boolean
        
        Dim fs As New Scripting.FileSystemObject

        Dim InputFileDirectory As String               'path to the reports.

        Dim strFileName As Object               'file name found in input location

        'default function to failed
        ProcessDataFiles = False

        'LIVE SETTINGS
        InputFileDirectory = "C:\Users\Alpesh\Desktop"
        
        'cycle through the files in the dir.
        For Each strFileName In fs.GetFolder(InputFileDirectory).Files

            If IsNothing(strFileName) Then
                'response.write "Nothing to process"
                ProcessDataFiles = False
                fs = Nothing
                Exit Function

            Else

               If UCase(fs.GetExtensionName(strFileName.name)) = "TXT" Then
                'process file 
                    If LoadFile(strFileName.path) Then
                          'did load
                    Else
                           'didnt load
                    End If
              End if
            End If

        Next

        fs = Nothing

        Exit Function

    End Function

    Function LoadFile(ByVal strSource As String)

        LoadFile = True

        Dim fs 'As Scripting.FileSystemObject                 'file system object
        Dim fh 'As Integer                'file handler pointer
        Dim strTMP As String            'line from file
        Dim strSplit() As String            'break line into components
        Dim intA As Integer              'split array counter

        If strSource <> "" Then
            fs = CreateObject("Scripting.FileSystemObject")

            fh = fs.OpenTextFile(strSource, 1)

            Do While fh.AtEndOfStream <> True

                strTMP = fh.ReadLine

                If InStr(strTMP, "BlockedIP") > 0 Then
                    'do somthing
                End If

            Loop
            fh.close()
        End If

        'clean up objects and handles
        fh = Nothing
        fs = Nothing

        LoadFile = True

        Exit Function

    End Function

Open in new window

0
GrahamSkanRetiredCommented:
Can you use Word to open the file manually?
Do make sure that there isn't a message box hidden under another window. You may have several instances of Word running so that the file is already being used by one of those instances. Use Task Manager to check it and close unwanted instances.
You can avoid creating multiple instances of Word with code like this:
Option Explicit
Private Sub Command1_Click()

Dim objMSWord As Word.Application
Dim myDOc As Word.Document

On Error Resume Next 'suppress error message if no instance already running
    Set objMSWord = GetObject(, "Word.Application")
On Error GoTo 0 'resume error checking

If objMSWord Is Nothing Then
    Set objMSWord = CreateObject("Word.Application")
End If

Set myDOc = objMSWord.Documents.Open("C:\Users\Alpesh\Desktop\111114.txt")

For Each p In myDOc.Paragraphs
  If InStr(p.Range.Text, "BlockedIP") > 0 Then
   'Skipping - found text
  Else
    p.Range.Delete
  End If
Next

myDOc.SaveAs ("c:\Users\Me\Desktop\052214_2.txt")
myDOc.Close
Set myDOc = Nothing 'this line does nothing that wouldn't happen automatically when the Sub is exited
objMSWord.Quit
End Sub

Open in new window

0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

al4629740Author Commented:
Glen, is that in vb6?
0
al4629740Author Commented:
I am able to open it in word and I have proper permissions
0
Glen RichmondCustomer Reporting Programmer.Commented:
Its VB.Net (origonally converted frm VB6)
but to change it to VB6 simply replace the ref

Dim fs As New Scripting.FileSystemObject

with

Dim fs As New FileSystemObject


thats should do it..

(if you are using VB.net and cant get it to work, it may be you need to include the Microsoft Scripting Runtime COM Library)
0
GrahamSkanRetiredCommented:
I have just noticed that your code and my modification do not make the Word application visible. That can make it more difficult to debug , so do add a line to accomplish that.
Option Explicit
Private Sub Command1_Click()

Dim objMSWord As Word.Application
Dim myDOc As Word.Document

On Error Resume Next 'suppress error message that will occur if no instance already running
    Set objMSWord = GetObject(, "Word.Application")
On Error GoTo 0 'resume error checking

If objMSWord Is Nothing Then
    Set objMSWord = CreateObject("Word.Application")
End If

objMSWord.Visible = True 'make sure that you can see the application
Set myDOc = objMSWord.Documents.Open("C:\Users\Alpesh\Desktop\111114.txt")

For Each p In myDOc.Paragraphs
  If InStr(p.Range.Text, "BlockedIP") > 0 Then
   'Skipping - found text
  Else
    p.Range.Delete
  End If
Next

myDOc.SaveAs ("c:\Users\Me\Desktop\052214_2.txt")
myDOc.Close
Set myDOc = Nothing 'this line does nothing that wouldn't happen automatically when the Sub is exited
objMSWord.Quit
End Sub

Open in new window

0
GrahamSkanRetiredCommented:
If you only need to treat the files as text, you can use VB6's intrinsic file functions:
Private Sub Command1_Click()
    Dim f As Integer
    Dim g As Integer
    Dim strLine As String
    
    f = FreeFile
    Open "C:\Users\Alpesh\Desktop\111114.txt" For Input As #f
        g = FreeFile
        Open "c:\Users\Me\Desktop\052214_2.txt" For Append As #g
            Do Until EOF(f)
                Line Input #f, strLine
                If InStr(strLine, "BlockedIP") = 0 Then
                    Print #g, strLine
                End If
            Loop
        Close #g
    Close #f
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
al4629740Author Commented:
Graham, that almost works.  Although is not collecting only the "BlockedIP" lines.  Its collecting ALL the lines
0
al4629740Author Commented:
Fixed it.  Had to change this line from
If InStr(strLine, "BlockedIP") = 0 Then
to
If InStr(strLine, "BlockedIP") > 0 Then
0
GrahamSkanRetiredCommented:
Thanks. I had your objective the wrong way round, thinking that you wanted to drop those lines.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.