• Home
  • About
  • Contact
  • Projects
« Merry Christmas!
When a good application won’t leave (or the brother-in-law app) »

Dealing with Tracked Changes in a Word Document

Posted by admin on December 21, 2007 – 2:53 am      
Filed under Metadata, Office, Troubleshooting

I recently had to rearrange some code that dealt with Tracked Changes in a Word document.

After refactoring, I ran through a series of test documents I use to verify things and was surprised to find that one of my primary test documents failed. I got an error that the method I was using (Document.AcceptAllRevisions) wasn’t available because the document was protected.

This particular test doc was, in fact, protected, but only with a blank password. It also had 2 sections, the second of which was “protected for form fields”. A bit complex, maybe, but certainly nothing horribly bizarre.

The reason the error surprised me is that my app attempts to unprotect any document it finds protected when it loads the file. If the doc is only protected with a blank password, my app will unprotect it and I can usually continue on with no problems.

If the doc is protected with a password, well, c’est la vie. I just throw up my hands and cry mercy!

But the unprotect didn’t seem to work anymore. Why?

I started with some googling but got nowhere.

Then I figured it had to be something I had been doing before attempting to AcceptAllRevisions originally, but that I was now doing afterwards. No luck there either.

Ok. It had to be something in the load up process (loading the doc into Word, and setting things up). So I commented out everything but the essentials. Still no luck.

In desperation, I coded up a quick test app to try and replicate the problem outside my app:

Public Sub Main()
   Dim x
   Dim w As Word.Application
   Dim doc As Word.Document
   
   Set w = New Word.Application
   
   w.ScreenUpdating = False
   w.Options.CreateBackup = False
   
   '---- NOTE Pagination must be true for certain RANGE INFO functions to return valid results
   '     It sure can slow some things down though.
   '     TODO investigate this
   w.Options.Pagination = True
   w.Options.BackgroundSave = False
   
   w.DisplayAlerts = wdAlertsNone
   
   w.WordBasic.DisableAutoMacros True
   Set doc = w.Documents.Open("c:\Users\Darin\DeskTop\000-Main-New (2).doc", ReadOnly:=False, AddToRecentFiles:=False, Visible:=True, PassWordDocument:="Blahblah", WritePassWordDocument:="BlahBlah")
   On Error Resume Next
   w.WordBasic.DisableAutoMacros False
   On Error GoTo 0

   doc.Unprotect
   doc.Sections(2).ProtectedForForms = False
   
   doc.TrackRevisions = True
   
   doc.AcceptAllRevisions
      
Cleanup:
   doc.Close SaveChanges:=wdDoNotSaveChanges, RouteDocument:=False
   
   Set doc = Nothing
   w.ScreenUpdating = True
   w.Quit False
   Set w = Nothing
   Exit Sub
   
ErrHandler:
   x = 1
   Resume Cleanup
End Sub

It worked perfectly. Grrr.

To recap, run the above code on it’s own, no problems. Run effectively the same sequence in my application, I get a “Document is protected” error when I execute the doc.AcceptAllRevisions line.

Now it’s time for hail marys…

I tried playing with the TrackRevisions state, with various incantations against the Unprotect method, even presaving the document. No joy.

In the process, I tried checking the ProtectionType of the document. It was -1 (no protection). Ok. Good. But a bit later, I tried checking the ProtectionType of the Application.ActiveDocument. Protected!

What?!

Then it hit me, when I run my app, it’s running as an AddIn within Word. So when I load this document to process, I’ve actually got 2 copies of the document loaded in Word, the original version, and a COPY of it (with a random filename) that I actually process.

When my test app ran, only one document was loaded in Word, so it HAD to be the active document.

But when I ran my app, even though the AcceptAllRevisions method on a Document object should certainly have nothing to do with some other loaded document (the active document), that does appear to be the case.

Long story short, I force the “active” document to be the document I’m about to AcceptAllRevisions on, like so: (Thanks to David for the heads up that my code snippet below wasn’t quite complete!)

Set Doc = {whatever DOCUMENT object you are currently working with}
Set aDoc = App.ActiveDocument 'Get the "active" document, to restore it below
Doc.Activate 'Activate the doc you're working with
Doc.AcceptAllRevisions

'And finally, reactivate the originally active doc (if this is appropriate for you)
If Not aDoc Is Nothing Then aDoc.Activate

And AcceptAllRevisions works perfectly well once again.

Comments RSS Feed   Post a comment   Trackback URL   Share on Twitter   Share on Facebook

Post a Comment

Click here to cancel reply.

Your email is never published nor shared. Required fields are marked *

*
*
  • Subscribe

  • Recent Posts

    • Running Visual Studio as Admin Without the UAC prompts
    • Laying Asphalt
    • Hacking Hack
    • Upgrading to WordPress 6.1
    • Starting VSCode from the command line
  • Recent Comments

    • Laying Asphalt — darinhiggins.com on Hacking Hack
    • Hacking Hack — darinhiggins.com on Best Programming Font
    • admin on Authoring VB6 IDE Addins in VB.net
    • admin on Launching into a Zoom Meeting directly from a Windows Shortcut
    • Daniel on Launching into a Zoom Meeting directly from a Windows Shortcut
  • Archives

    • February 2023
    • November 2022
    • October 2022
    • August 2022
    • May 2021
    • December 2020
    • November 2020
    • September 2020
    • August 2020
    • April 2020
    • July 2018
    • June 2014
    • February 2013
    • December 2012
    • September 2012
    • August 2012
    • July 2012
    • June 2012
    • May 2012
    • April 2012
    • March 2012
    • February 2012
    • January 2012
    • November 2011
    • October 2011
    • September 2011
    • August 2011
    • July 2011
    • June 2011
    • May 2011
    • April 2011
    • March 2011
    • February 2011
    • January 2011
    • November 2010
    • October 2010
    • September 2010
    • August 2010
    • July 2010
    • June 2010
    • May 2010
    • April 2010
    • March 2010
    • February 2010
    • January 2010
    • December 2009
    • November 2009
    • October 2009
    • September 2009
    • August 2009
    • July 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • October 2008
    • September 2008
    • August 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • March 2008
    • February 2008
    • January 2008
    • December 2007
    • November 2007
    • October 2007
    • September 2007
    • August 2007
    • July 2007
    • June 2007
    • May 2007
    • April 2007
    • January 2007
  • Categories

    • .NET
    • ActiveX
    • amBX
    • Apophysis
    • Arcade
    • ASP
    • AutoHotKey
    • Blogging
    • Cisco
    • Code Garage
    • CSS
    • Cuckoo Clocks
    • dasBlog
    • DMS
    • DOCSOpen
    • EMail
    • Error Handling
    • Expression Blend
    • Firefox
    • Font
    • Fonts
    • Fractals
    • Frets On Fire
    • Games
    • Guitar
    • Hack of the Day
    • Hardware
    • Helicopters
    • Hiring
    • Icons
    • Installations
    • Javascript
    • KnockOut
    • Languages
    • log4net
    • Maps/GIS
    • Media
    • Metadata
    • Misc
    • MP3s
    • MSBuild
    • Networking
    • nuget
    • Office
    • p/Invoke
    • Panoramas
    • PDF
    • Phones
    • Photography
    • Pool
    • Python
    • Rants
    • RawMouse
    • Regular Expressions
    • Resource Files
    • Roomba
    • Security
    • Software Architecture
    • SQL
    • Stepmania
    • Subversion
    • Telecommuting
    • Terragen
    • Text Editors
    • Tivo
    • Troubleshooting
    • Tweaks
    • TypeLibs
    • Uncategorized
    • Utilities
    • VB Feng Shui
    • VB6
    • Version Control
    • Vista
    • Visual Studio
    • VSTO
    • Web
    • Windows 7
    • Windows Phone 7
    • WindowsMediaPlayer
    • Word
    • WordPress
    • WPF
    • XML
  • Meta

    • Log in
    • Entries feed
    • Comments feed
    • WordPress.org


Content © darinhiggins.com and Darin Higgins. Proudly powered by WordPress. To bookmark this site, press Control+D.

"StarGate" theme by Darin Higgins based on "Black Hat" by Nicki Faulk. For best results, please view with Firefox.   [ Log in ]
Comment antispam by