Results 1 to 5 of 5
  1. #1
    Star Lounger
    Join Date
    Feb 2011
    Location
    Washington, DC
    Posts
    98
    Thanks
    8
    Thanked 6 Times in 6 Posts

    Word 2010 macro stops execution after opening a file

    I have a macro run from one file that opens a second file, checks it for a property, then activates the first file and does some stuff.

    It was working on one machine, but when transferred to another machine, it stops executing after opening the second file, and the cursor pops to the top of the macro.

    Does the same thing (ie stops executing) whether I execute from the VBA editor or by chosing the macro from the list. Does the same thing whether I step through or just run.

    Does the same thing whether I allow users to pick a file using the file dialog or specify the file path in code.

    Word 2010.

    Ideas?

    I already tried rebooting and reimporting, just in case there was some file corruption someplace.

    Thanks,

    Jessica

  2. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    Can you share the code so we can see what the problem might be? Depending on how the code might be written, it is possible that the ActiveDocument changing as a second file opens is what is causing the problem.

    Where does the code itself reside? Is that document remaining open throughout the macro running phase?
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  3. #3
    Star Lounger
    Join Date
    Feb 2011
    Location
    Washington, DC
    Posts
    98
    Thanks
    8
    Thanked 6 Times in 6 Posts
    The code resides in a template that remains open throughout execution. As soon as the second file opens, the macro stops executing. If the second file is already open, the macro works. Is there a way to open the second file without making it the active document?

    Here it is:
    Code:
    Sub ReplaceFromTableList2()
    Dim ChangeDoc as Document
    Dim RefDoc As Document
    Dim cTable As Table
    Dim oFind, oReplace As Range
    Dim I As Long
    Dim wasTracking As Boolean
    Dim sFname As String
     
    Dim dlgOpen As FileDialog
    Set dlgOpen = Application.FileDialog(FileDialogType:=msoFileDialogOpen)
       
        With dlgOpen
            .Filters.Clear
            .Filters.Add "Word Files", "*.docx"
            .AllowMultiSelect = False
            .Show
            On Error Resume Next
            sFname = .SelectedItems(1)
        End With
        On Error GoTo 0
       
        If sFname = "" Then
        MsgBox "You didn't pick a file"
            Exit Sub
        End If
     
    'sFname = "H:\Macros\correctionTable.docx"  ' tried it with this statement rather than the file picker; didn't work either
    wasTracking = ActiveDocument.TrackRevisions
    'Define the document to be processed
    Set RefDoc = ActiveDocument
     
    'Open the document with the changes
    Set ChangeDoc = Documents.Open(sFname)
    ' quick check to make sure it is a good doc
    If ChangeDoc.Tables.Count = 0 Then
        MsgBox "There are no tables in the document you picked"
        ChangeDoc.Close wdDoNotSaveChanges  'close the bad doc
        Exit Sub
    End If
    'Define the table to be used
    Set cTable = ChangeDoc.Tables(1)
     
    'Activate the document to be processed
    RefDoc.Activate
    ActiveDocument.TrackRevisions = False
    replaceUnderline
    ActiveDocument.TrackRevisions = wasTracking
    
    For I = 1 To cTable.Rows.Count
     
         'Define the cell containing the word/phrase to be replaced
         Set oFind = cTable.Cell(I, 1).Range
      
         oFind.End = oFind.End - 1  ' get rid of the table cell end marker
     
         'Define the cell containing the replacement word/phrase
         Set oReplace = cTable.Cell(I, 2).Range
          
         oReplace.End = oReplace.End - 1  ' get rid of the table cell end marker
         
         With Selection
     
              'Start at the top of the document
              .HomeKey wdStory
     
              'Replace the words/phrases
              With .Find
                   .ClearFormatting
                  ' .Replacement.ClearFormatting
                   .Execute findtext:=oFind, replacewith:=oReplace, Replace:=wdReplaceAll, MatchWholeWord:=True, MatchWildcards:=False, MatchCase:=True, Forward:=True, Wrap:=wdFindContinue
              End With
         End With
    Next I
    ActiveDocument.TrackRevisions = False
    unreplaceUnderline
    ActiveDocument.TrackRevisions = wasTracking
     
    'Close the document with the table
    ChangeDoc.Close wdDoNotSaveChanges
    end sub
    Last edited by RetiredGeek; 2015-02-28 at 06:39. Reason: Added Code Tags

  4. #4
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    Jessica

    Your code is almost there. You have named both documents so that part is taken care of but then you go back to using ActiveDocument and Selection which gets confusing.

    For starters - move Set RefDoc earlier in the macro to avoid confusion
    Then remove all mentions of ActiveDocument and replace with either ChangeDoc or RefDoc depending on what you want to achieve.

    Next, change the macro replaceUnderline to work with a passed in document rather than what it is currently doing (you didn't post that code). This means that particular macro should change along these lines
    Sub replaceUnderline()
    With ActiveDocument.....
    .....
    End Sub

    changes to
    Function replaceUnderline(aDoc as Document)
    With aDoc...
    .....
    End Function

    Finally, get rid of the selection object and replace it with a range. The selection object is unclear when we don't know which document is active.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  5. #5
    Star Lounger
    Join Date
    Feb 2011
    Location
    Washington, DC
    Posts
    98
    Thanks
    8
    Thanked 6 Times in 6 Posts

    Thanks

    Thanks, Andrew! I

    bet it worked on the other machine because I had fewer documents open and they all had the same template. For production I am going to isolate this routine in its own template and train the technical writers to switch templates temporarily or load an add-in. I'm lucky in that the other tech writers on hand are savvy enough to do that.

    Anyway, I can make those code changes, and remember to avoid Selection when I can.

    For what it's worth, the replaceUnderline and unReplaceUnderline bits substitute unlikely strings for the underline character. It turns out that Word thinks that "OPS__detra" is a single word. The brute force way of stopping that was to temporarily eliminate underlines in the doc.

    Is there another way to tell Word not to treat _ as a separator? Of course I need it to still treat periods, commas, colons, question marks and semicolons as separators.

    - Jessica
    Last edited by jweissmn1; 2015-02-28 at 07:21. Reason: Fix typo

Tags for this Thread

Posting Permissions

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