Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 177
  • Last Modified:

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

0
al4629740
Asked:
al4629740
  • 4
  • 4
  • 3
1 Solution
 
Glen RichmondCommented:
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 RichmondCommented:
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
 
GrahamSkanCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 RichmondCommented:
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
 
GrahamSkanCommented:
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
 
GrahamSkanCommented:
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
 
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
 
GrahamSkanCommented:
Thanks. I had your objective the wrong way round, thinking that you wanted to drop those lines.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now