Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-26-2017, 06:40 AM
brent chadwick brent chadwick is offline Error with exit macro Windows 8 Error with exit macro Office 2013
Advanced Beginner
Error with exit macro
 
Join Date: Mar 2015
Posts: 86
brent chadwick is on a distinguished road
Default Error with exit macro

Hey guys,
I have this exit macro which has worked flawlessly in both Mac and Windows until yesterday when I used it in a Windows table (Mac works flawlessly in tables). The code stalls on the fourth line "Selection.Fields.Unlink." If I reverse the sequence of the third (ActiveDocument.Unprotect) and fourth lines, it works for the tables but stalls in the body of the text. I'm stumped. Suggestions?

Code:
Sub UnlinkFieldsCensus()
' Unlink Fields Macro
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Selection.MoveUp Unit:=wdParagraph, Count:=1, Extend:=wdExtend
    ActiveDocument.Unprotect
    Selection.Fields.Unlink
    Selection.MoveRight Unit:=wdCharacter, Count:=2
          Dim Bookmark As Bookmark
      ActiveDocument.Bookmarks.ShowHidden = True
      If ActiveDocument.Bookmarks.Count >= 1 Then
         For Each Bookmark In ActiveDocument.Bookmarks
            Bookmark.Delete
         Next Bookmark
      End If
End Sub

Reply With Quote
  #2  
Old 01-26-2017, 03:52 PM
macropod's Avatar
macropod macropod is offline Error with exit macro Windows 7 64bit Error with exit macro Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try:
Code:
Sub UnlinkFieldsCensus()
Dim Rng As Range, bHid As Boolean
Set Rng = Selection.Paragraphs(1).Range
With ActiveDocument
  .Unprotect
  Rng.Fields.Unlink
  With .Bookmarks
    bHid = .ShowHidden
    .ShowHidden = True
    While .Count > 0
      .Item(1).Delete
    Wend
    .ShowHidden = bHid
  End With
End With
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 01-27-2017, 05:51 AM
brent chadwick brent chadwick is offline Error with exit macro Windows 8 Error with exit macro Office 2013
Advanced Beginner
Error with exit macro
 
Join Date: Mar 2015
Posts: 86
brent chadwick is on a distinguished road
Default Still stalls

Hey Paul, with the code you gave me to try, both Mac and windows stall at "Rng.Fields.Unlink", but only if run in a table.
Thanks-Brent
Reply With Quote
  #4  
Old 01-27-2017, 02:49 PM
macropod's Avatar
macropod macropod is offline Error with exit macro Windows 7 64bit Error with exit macro Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

After:
Set Rng = Selection.Paragraphs(1).Range
insert:
Rng.End = Rng.End - 1
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 01-28-2017, 06:02 AM
brent chadwick brent chadwick is offline Error with exit macro Windows 8 Error with exit macro Office 2013
Advanced Beginner
Error with exit macro
 
Join Date: Mar 2015
Posts: 86
brent chadwick is on a distinguished road
Default

Thanks Paul, works like a charm. Can you explain to me why the macro I posted would only stall in a Windows Table?
Reply With Quote
  #6  
Old 01-28-2017, 01:49 PM
macropod's Avatar
macropod macropod is offline Error with exit macro Windows 7 64bit Error with exit macro Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Possibly it was stalling because 'Selection.MoveRight Unit:=wdCharacter, Count:=2' tried to move into or beyond the end-of-cell marker.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error with exit macro Help with Macro error jar208 Excel Programming 2 12-06-2016 03:57 PM
Pictures animated exit abc3132 PowerPoint 0 02-26-2015 12:27 AM
Error with exit macro VBA error Content Control On Exit jec1 Word VBA 5 08-16-2014 08:14 PM
Error with exit macro Drop-Downs: Run Macro on Exit Andrew H Word VBA 15 11-16-2012 08:34 AM
Is there an exit on close ? mhdadmh Word 0 10-12-2006 04:28 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:31 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft