Solved

code hangs in vb6

Posted on 2014-11-11
11
153 Views
Last Modified: 2014-11-15
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
Comment
Question by:al4629740
  • 4
  • 4
  • 3
11 Comments
 
LVL 2

Expert Comment

by:Glen Richmond
ID: 40436779
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
 
LVL 2

Expert Comment

by:Glen Richmond
ID: 40436800
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
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 40436984
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
 

Author Comment

by:al4629740
ID: 40439104
Glen, is that in vb6?
0
 

Author Comment

by:al4629740
ID: 40439105
I am able to open it in word and I have proper permissions
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 2

Expert Comment

by:Glen Richmond
ID: 40439442
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
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 40439506
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
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 40441080
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
 

Author Comment

by:al4629740
ID: 40442913
Graham, that almost works.  Although is not collecting only the "BlockedIP" lines.  Its collecting ALL the lines
0
 

Author Comment

by:al4629740
ID: 40442919
Fixed it.  Had to change this line from
If InStr(strLine, "BlockedIP") = 0 Then
to
If InStr(strLine, "BlockedIP") > 0 Then
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 40444359
Thanks. I had your objective the wrong way round, thinking that you wanted to drop those lines.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now