Interrupting a macro

Discussion in 'Word VBA Beginners' started by Jean-François Colas, Sep 27, 2010.

  1. Hi all,

    Hoping I'm in the right place for that question.

    I have a macro which scans a file, word by word, inserting some
    sequences of characters according to some conditions. This macro must
    sometimes query the user for an action to do. I would like to implement
    one command that would ask the macro not to query for anything else and
    go through the end of the file. But... then I would like to have a
    chance to interrupt it and, moreover, having a way to know where we are.

    I was hoping I could check for a keystroke, for exemple the Escape key,
    somewhere in a loop, but I didn't find anything about it.

    Thanks in advance if you have any suggestions.
    J.-F.
     
    Jean-François Colas, Sep 27, 2010
    #1
    1. Advertisements

  2. Jean-François Colas

    Graham Mayor Guest

    If you tell us about what the macro does (post your code) and under what
    conditions you want to either stop processing or to provide some user input,
    we would be better able to assist.

    "Jean-François Colas" <> wrote in message
    news:i7qvuf$36q$...
    > Hi all,
    >
    > Hoping I'm in the right place for that question.
    >
    > I have a macro which scans a file, word by word, inserting some sequences
    > of characters according to some conditions. This macro must sometimes
    > query the user for an action to do. I would like to implement one command
    > that would ask the macro not to query for anything else and go through the
    > end of the file. But... then I would like to have a chance to interrupt it
    > and, moreover, having a way to know where we are.
    >
    > I was hoping I could check for a keystroke, for exemple the Escape key,
    > somewhere in a loop, but I didn't find anything about it.
    >
    > Thanks in advance if you have any suggestions.
    > J.-F.
    >
     
    Graham Mayor, Sep 28, 2010
    #2
    1. Advertisements

  3. OK, I don't know for sure if I have the right to post the code (it's
    not mine for most), so here is at least the scenario.

    The macro searches for the next word beginning with a capital letter.
    The question is: do we need to insert a sequence of characters before
    that word or not. To determine that, two ways :
    1) a dictionary, containing the words to "tag", and the words NOT to "tag".
    2) User response, if the word is not present in the dictionary.
    The user will say, by one-letter commands, if he wants the word to be
    tagged here, always, not here, or never. The goal would be to have a way
    for the macro (same if possible) to rely only on the dictionary, without
    user input.

    When the user is requested a command, he can interrupt the macro by
    pressing Escape. He can run it later from the point ha stopped it :
    that's what I would like to have a chance to do even if I launched it in
    a non-interactive way. I thought I woula have missed a basic point, but
    considering my first question and your first response, I presume it's
    not so basic.

    Hoping I provided some useful new clues even so...

    J.-F.

    Graham Mayor a écrit :
    > If you tell us about what the macro does (post your code) and under what
    > conditions you want to either stop processing or to provide some user input,
    > we would be better able to assist.
    >
    > "Jean-François Colas"<> wrote in message
    > news:i7qvuf$36q$...
    >> Hi all,
    >>
    >> Hoping I'm in the right place for that question.
    >>
    >> I have a macro which scans a file, word by word, inserting some sequences
    >> of characters according to some conditions. This macro must sometimes
    >> query the user for an action to do. I would like to implement one command
    >> that would ask the macro not to query for anything else and go throughthe
    >> end of the file. But... then I would like to have a chance to interrupt it
    >> and, moreover, having a way to know where we are.
    >>
    >> I was hoping I could check for a keystroke, for exemple the Escape key,
    >> somewhere in a loop, but I didn't find anything about it.
    >>
    >> Thanks in advance if you have any suggestions.
    >> J.-F.
    >>

    >
     
    Jean-François Colas, Sep 28, 2010
    #3
  4. Jean-François Colas

    Graham Mayor Guest

    I don't know if you are still watching, but I briefly had a look at your
    requirements soon after your message was posted. I could not see an easy way
    to do what you asked, and then I forgot about it.

    However having spotted it again, I took another look and I believe the
    following may come close to what you require. The 'dictionary' comprises a
    two column table in a document defined at sFname.

    The first column contains words to tag, the second contains words to skip.
    If a word is found that is not in the table, you get the choice to tag that
    word. If you choose 'yes' the word is added to the first column and tagged.
    If you select no, it is added to the second column and skipped. If you
    choose Cancel the start of the word is bookmarked to allow you to resume
    from the same position later.

    The macro is a two stage process. The first stage colours all the tagged
    words red. The second stage removes the colour and adds the prefix text.

    The macro is annotated so that you can see how it works.
    http://www.gmayor.com/installing_macro.htm

    Sub TagWords()
    Dim oWord As Variant
    Dim oChanges As Document, oDoc As Document
    Dim oTable As Table
    Dim oCell As Cell
    Dim rCell As Range
    Dim oRng As Range
    Dim rTag As Range
    Dim i As Long
    Dim sFname As String
    Dim bFound As Boolean
    Dim bAdded As Boolean
    Dim sTag As String
    Dim sAsk As String
    'define the prefix text to add to the tagged words
    sTag = "<tag>"
    'define the name and path to the dictionary document
    sFname = "D:\My Documents\Test\Changes.doc"
    'name the active document
    Set oDoc = ActiveDocument
    'open and name the dictionary document
    Set oChanges = Documents.Open(FileName:=sFname, Visible:=False)
    'name the table containing the words to be tagged or skipped
    Set oTable = oChanges.Tables(1)
    'mark the document as a range
    Set oRng = oDoc.Range
    'If the macro has previously been run and
    'paused there will be a bookmark
    'Locate that bookmark if it exists and move
    'the start of the range to the bookmark
    If oDoc.Bookmarks.Exists("LastWord") = True Then
    oRng.Start = oDoc.Bookmarks("LastWord").Range.End
    End If
    'Check each word in the range for an upper case first letter
    For Each oWord In oRng.Words
    bFound = False
    If Asc(Left(oWord, 1)) >= 65 And _
    Asc(Left(oWord, 1)) <= 90 Then
    'if found check if the word is in the first column of the table
    For i = 1 To oTable.Rows.Count
    If InStr(1, oTable.Cell(i, 1).Range, Trim(oWord)) Then
    'if it is colour it red
    oWord.Font.Color = wdColorRed
    'and set a flag to mark it as found
    bFound = True
    Exit For
    End If
    Next i
    If bFound = False Then
    'the word was not in the first column, so check the second
    For i = 1 To oTable.Rows.Count
    If InStr(1, oTable.Cell(i, 2).Range, Trim(oWord)) Then
    oRng.Start = oWord.End
    'and if found set a flag to mark it as found
    bFound = True
    Exit For
    End If
    Next i
    End If
    If bFound = False Then
    'the word was not in the table
    oWord.Select
    'so ask the user what to do with it
    sAsk = MsgBox("Tag " & oWord & "?", vbYesNoCancel)
    If sAsk = vbYes Then
    'The user has asked to tag the word
    bAdded = False
    'so colour it red
    oWord.Font.Color = wdColorRed
    'and add it to the first empty cell in column 1
    For Each oCell In oTable.Columns(1).Cells
    If Len(oCell.Range) = 2 Then
    Set rCell = oCell.Range
    rCell.End = rCell.End - 1
    rCell.Text = oWord
    'and set flag to mark it as entered
    bAdded = True
    Exit For
    End If
    Next oCell
    If bAdded = False Then
    'there were no empty cells so add one and
    'insert the word in the new row
    oTable.Rows.Add
    Set rCell =
    oTable.Columns(1).Cells(oTable.Rows.Count).Range
    rCell.End = rCell.End - 1
    rCell.Text = oWord
    End If
    End If
    If sAsk = vbNo Then
    'the user responded no
    bAdded = False
    'so add the word to the first empty cell of column 2
    For Each oCell In oTable.Columns(2).Cells
    If Len(oCell.Range) = 2 Then
    Set rCell = oCell.Range
    rCell.End = rCell.End - 1
    rCell.Text = oWord
    'and set a flag to mark it as entered
    bAdded = True
    Exit For
    End If
    Next oCell
    If bAdded = False Then
    'there were no empty cells so add one and
    'insert the word in the new row
    oTable.Rows.Add
    Set rCell =
    oTable.Columns(2).Cells(oTable.Rows.Count).Range
    rCell.End = rCell.End - 1
    rCell.Text = oWord
    End If
    End If
    If sAsk = vbCancel Then
    'the user selected Cancel
    'so add a bookmark in front of the word
    oWord.End = oWord.Start
    oWord.Bookmarks.Add "LastWord"
    'and quit the macro
    Exit Sub
    End If
    End If
    End If
    Next oWord
    'Now find all the red colured words
    Set oRng = ActiveDocument.Range
    With oRng.Find
    .Font.Color = wdColorRed
    Do While .Execute(Forward:=True) = True
    'remove the red colour
    oRng.Font.Color = wdColorAutomatic
    'and insert the prefix text
    oRng.InsertBefore sTag
    oRng.Collapse wdCollapseEnd
    Loop
    End With
    'close the dictionary with the added words saved.
    oChanges.Close wdSaveChanges
    End Sub

    --
    <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
    Graham Mayor - Word MVP


    <>>< ><<> ><<> <>>< ><<> <>>< <>><<>

    "Jean-François Colas" <> wrote in message
    news:i7tgug$30i$...
    OK, I don't know for sure if I have the right to post the code (it's
    not mine for most), so here is at least the scenario.

    The macro searches for the next word beginning with a capital letter.
    The question is: do we need to insert a sequence of characters before
    that word or not. To determine that, two ways :
    1) a dictionary, containing the words to "tag", and the words NOT to "tag".
    2) User response, if the word is not present in the dictionary.
    The user will say, by one-letter commands, if he wants the word to be
    tagged here, always, not here, or never. The goal would be to have a way
    for the macro (same if possible) to rely only on the dictionary, without
    user input.

    When the user is requested a command, he can interrupt the macro by
    pressing Escape. He can run it later from the point ha stopped it :
    that's what I would like to have a chance to do even if I launched it in
    a non-interactive way. I thought I woula have missed a basic point, but
    considering my first question and your first response, I presume it's
    not so basic.

    Hoping I provided some useful new clues even so...

    J.-F.

    Graham Mayor a écrit :
    > If you tell us about what the macro does (post your code) and under what
    > conditions you want to either stop processing or to provide some user
    > input,
    > we would be better able to assist.
    >
    > "Jean-François Colas"<> wrote in message
    > news:i7qvuf$36q$...
    >> Hi all,
    >>
    >> Hoping I'm in the right place for that question.
    >>
    >> I have a macro which scans a file, word by word, inserting some sequences
    >> of characters according to some conditions. This macro must sometimes
    >> query the user for an action to do. I would like to implement one command
    >> that would ask the macro not to query for anything else and go through
    >> the
    >> end of the file. But... then I would like to have a chance to interrupt
    >> it
    >> and, moreover, having a way to know where we are.
    >>
    >> I was hoping I could check for a keystroke, for exemple the Escape key,
    >> somewhere in a loop, but I didn't find anything about it.
    >>
    >> Thanks in advance if you have any suggestions.
    >> J.-F.
    >>

    >
     
    Graham Mayor, Oct 10, 2010
    #4
  5. Jean-François Colas

    Graham Mayor Guest

    A minor anomaly could may make the macro fail to complete, so change the
    line

    Set oRng = ActiveDocument.Range
    to
    Set oRng = oDoc range

    in the section

    Set oRng = ActiveDocument.Range
    With oRng.Find
    .Font.Color = wdColorRed
    Do While .Execute(Forward:=True) = True
    'remove the red colour
    oRng.Font.Color = wdColorAutomatic
    'and insert the prefix text
    oRng.InsertBefore sTag
    oRng.Collapse wdCollapseEnd
    Loop
    End With

    near the end of the macro

    --
    <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
    Graham Mayor - Word MVP


    <>>< ><<> ><<> <>>< ><<> <>>< <>><<>

    "Graham Mayor" <> wrote in message
    news:i8rt3r$vfq$-september.org...
    >I don't know if you are still watching, but I briefly had a look at your
    >requirements soon after your message was posted. I could not see an easy
    >way to do what you asked, and then I forgot about it.
    >
    > However having spotted it again, I took another look and I believe the
    > following may come close to what you require. The 'dictionary' comprises a
    > two column table in a document defined at sFname.
    >
    > The first column contains words to tag, the second contains words to skip.
    > If a word is found that is not in the table, you get the choice to tag
    > that word. If you choose 'yes' the word is added to the first column and
    > tagged. If you select no, it is added to the second column and skipped. If
    > you choose Cancel the start of the word is bookmarked to allow you to
    > resume from the same position later.
    >
    > The macro is a two stage process. The first stage colours all the tagged
    > words red. The second stage removes the colour and adds the prefix text.
    >
    > The macro is annotated so that you can see how it works.
    > http://www.gmayor.com/installing_macro.htm
    >
    > Sub TagWords()
    > Dim oWord As Variant
    > Dim oChanges As Document, oDoc As Document
    > Dim oTable As Table
    > Dim oCell As Cell
    > Dim rCell As Range
    > Dim oRng As Range
    > Dim rTag As Range
    > Dim i As Long
    > Dim sFname As String
    > Dim bFound As Boolean
    > Dim bAdded As Boolean
    > Dim sTag As String
    > Dim sAsk As String
    > 'define the prefix text to add to the tagged words
    > sTag = "<tag>"
    > 'define the name and path to the dictionary document
    > sFname = "D:\My Documents\Test\Changes.doc"
    > 'name the active document
    > Set oDoc = ActiveDocument
    > 'open and name the dictionary document
    > Set oChanges = Documents.Open(FileName:=sFname, Visible:=False)
    > 'name the table containing the words to be tagged or skipped
    > Set oTable = oChanges.Tables(1)
    > 'mark the document as a range
    > Set oRng = oDoc.Range
    > 'If the macro has previously been run and
    > 'paused there will be a bookmark
    > 'Locate that bookmark if it exists and move
    > 'the start of the range to the bookmark
    > If oDoc.Bookmarks.Exists("LastWord") = True Then
    > oRng.Start = oDoc.Bookmarks("LastWord").Range.End
    > End If
    > 'Check each word in the range for an upper case first letter
    > For Each oWord In oRng.Words
    > bFound = False
    > If Asc(Left(oWord, 1)) >= 65 And _
    > Asc(Left(oWord, 1)) <= 90 Then
    > 'if found check if the word is in the first column of the table
    > For i = 1 To oTable.Rows.Count
    > If InStr(1, oTable.Cell(i, 1).Range, Trim(oWord)) Then
    > 'if it is colour it red
    > oWord.Font.Color = wdColorRed
    > 'and set a flag to mark it as found
    > bFound = True
    > Exit For
    > End If
    > Next i
    > If bFound = False Then
    > 'the word was not in the first column, so check the second
    > For i = 1 To oTable.Rows.Count
    > If InStr(1, oTable.Cell(i, 2).Range, Trim(oWord)) Then
    > oRng.Start = oWord.End
    > 'and if found set a flag to mark it as found
    > bFound = True
    > Exit For
    > End If
    > Next i
    > End If
    > If bFound = False Then
    > 'the word was not in the table
    > oWord.Select
    > 'so ask the user what to do with it
    > sAsk = MsgBox("Tag " & oWord & "?", vbYesNoCancel)
    > If sAsk = vbYes Then
    > 'The user has asked to tag the word
    > bAdded = False
    > 'so colour it red
    > oWord.Font.Color = wdColorRed
    > 'and add it to the first empty cell in column 1
    > For Each oCell In oTable.Columns(1).Cells
    > If Len(oCell.Range) = 2 Then
    > Set rCell = oCell.Range
    > rCell.End = rCell.End - 1
    > rCell.Text = oWord
    > 'and set flag to mark it as entered
    > bAdded = True
    > Exit For
    > End If
    > Next oCell
    > If bAdded = False Then
    > 'there were no empty cells so add one and
    > 'insert the word in the new row
    > oTable.Rows.Add
    > Set rCell =
    > oTable.Columns(1).Cells(oTable.Rows.Count).Range
    > rCell.End = rCell.End - 1
    > rCell.Text = oWord
    > End If
    > End If
    > If sAsk = vbNo Then
    > 'the user responded no
    > bAdded = False
    > 'so add the word to the first empty cell of column 2
    > For Each oCell In oTable.Columns(2).Cells
    > If Len(oCell.Range) = 2 Then
    > Set rCell = oCell.Range
    > rCell.End = rCell.End - 1
    > rCell.Text = oWord
    > 'and set a flag to mark it as entered
    > bAdded = True
    > Exit For
    > End If
    > Next oCell
    > If bAdded = False Then
    > 'there were no empty cells so add one and
    > 'insert the word in the new row
    > oTable.Rows.Add
    > Set rCell =
    > oTable.Columns(2).Cells(oTable.Rows.Count).Range
    > rCell.End = rCell.End - 1
    > rCell.Text = oWord
    > End If
    > End If
    > If sAsk = vbCancel Then
    > 'the user selected Cancel
    > 'so add a bookmark in front of the word
    > oWord.End = oWord.Start
    > oWord.Bookmarks.Add "LastWord"
    > 'and quit the macro
    > Exit Sub
    > End If
    > End If
    > End If
    > Next oWord
    > 'Now find all the red colured words
    > Set oRng = ActiveDocument.Range
    > With oRng.Find
    > .Font.Color = wdColorRed
    > Do While .Execute(Forward:=True) = True
    > 'remove the red colour
    > oRng.Font.Color = wdColorAutomatic
    > 'and insert the prefix text
    > oRng.InsertBefore sTag
    > oRng.Collapse wdCollapseEnd
    > Loop
    > End With
    > 'close the dictionary with the added words saved.
    > oChanges.Close wdSaveChanges
    > End Sub
    >
    > --
    > <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
    > Graham Mayor - Word MVP
    >

    > <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
    >
    > "Jean-François Colas" <> wrote in message
    > news:i7tgug$30i$...
    > OK, I don't know for sure if I have the right to post the code (it's
    > not mine for most), so here is at least the scenario.
    >
    > The macro searches for the next word beginning with a capital letter.
    > The question is: do we need to insert a sequence of characters before
    > that word or not. To determine that, two ways :
    > 1) a dictionary, containing the words to "tag", and the words NOT to
    > "tag".
    > 2) User response, if the word is not present in the dictionary.
    > The user will say, by one-letter commands, if he wants the word to be
    > tagged here, always, not here, or never. The goal would be to have a way
    > for the macro (same if possible) to rely only on the dictionary, without
    > user input.
    >
    > When the user is requested a command, he can interrupt the macro by
    > pressing Escape. He can run it later from the point ha stopped it :
    > that's what I would like to have a chance to do even if I launched it in
    > a non-interactive way. I thought I woula have missed a basic point, but
    > considering my first question and your first response, I presume it's
    > not so basic.
    >
    > Hoping I provided some useful new clues even so...
    >
    > J.-F.
    >
    > Graham Mayor a écrit :
    >> If you tell us about what the macro does (post your code) and under what
    >> conditions you want to either stop processing or to provide some user
    >> input,
    >> we would be better able to assist.
    >>
    >> "Jean-François Colas"<> wrote in message
    >> news:i7qvuf$36q$...
    >>> Hi all,
    >>>
    >>> Hoping I'm in the right place for that question.
    >>>
    >>> I have a macro which scans a file, word by word, inserting some
    >>> sequences
    >>> of characters according to some conditions. This macro must sometimes
    >>> query the user for an action to do. I would like to implement one
    >>> command
    >>> that would ask the macro not to query for anything else and go through
    >>> the
    >>> end of the file. But... then I would like to have a chance to interrupt
    >>> it
    >>> and, moreover, having a way to know where we are.
    >>>
    >>> I was hoping I could check for a keystroke, for exemple the Escape key,
    >>> somewhere in a loop, but I didn't find anything about it.
    >>>
    >>> Thanks in advance if you have any suggestions.
    >>> J.-F.
    >>>

    >>

    >
    >
    >
     
    Graham Mayor, Oct 10, 2010
    #5
    1. Advertisements

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
  1. Steve

    a macro to extract specific lines?

    Steve, Jun 30, 2003, in forum: Word VBA Beginners
    Replies:
    2
    Views:
    179
    jesse
    Jul 1, 2003
  2. Ed

    Rename/create folder from Word macro?

    Ed, Jun 30, 2003, in forum: Word VBA Beginners
    Replies:
    0
    Views:
    147
  3. kurt

    MACRO to go to a specific template folder

    kurt, Jul 6, 2003, in forum: Word VBA Beginners
    Replies:
    1
    Views:
    177
    Word Heretic
    Jul 6, 2003
  4. Kim

    Converting Word basic macro to Visual basic

    Kim, Jul 7, 2003, in forum: Word VBA Beginners
    Replies:
    2
    Views:
    286
    Howard Kaikow
    Jul 8, 2003
  5. Andrzej
    Replies:
    1
    Views:
    153
    Charles Kenyon
    Jul 10, 2003
  6. Macro fails to produce any results

    , Jul 14, 2003, in forum: Word VBA Beginners
    Replies:
    1
    Views:
    123
    Jay Freedman
    Jul 15, 2003
  7. Rosemary

    One macro calling up another macro

    Rosemary, Apr 21, 2004, in forum: Word VBA Beginners
    Replies:
    6
    Views:
    146
    Doug Robbins - Word MVP
    Apr 22, 2004
  8. Paolo B

    Macro Vs. Macro, is one too strong?

    Paolo B, Sep 18, 2004, in forum: Word VBA Beginners
    Replies:
    2
    Views:
    124
    Paolo B
    Sep 18, 2004
Loading...