Bug in MS Word's VBA Document Collection, not sure why this workaround crashes -


ms word 2010 has bug in ability correctly maintain (of things) documents collection (link earliest report found - social.msdn.microsoft.com).

as far can tell bug impacts word 2010. although documents collection not maintained, turns out application.windows collection is. hence, word 2010 following code based on original reporters investigation (see below) , this question on answers.microsoft.com seem provide alternative buggy documents collection:

' purpose: ' return document collection, work-around word 2010 bug public function doccollection() vba.collection     dim indexofavailableappwindows long     dim resultdoc vba.collection     dim founddoc word.document     set resultdoc = new collection      indexofavailableappwindows = 1 application.windows.count         if application.windows(indexofavailableappwindows).document.type = wdtypedocument             set founddoc = application.windows(indexofavailableappwindows).document             resultdoc.add founddoc, founddoc.fullname ' can not use 'name' - fails unique          end if     next indexofavailableappwindows      set doccollection = resultdoc     set resultdoc = nothing  end function 

however, , here's question, above code times fails error 457 key associated element of collection on line resultdoc.add founddoc, founddoc.fullname. what circumstances possibly lead such failure?

so far code has failed on 1 pc running word 2016. don't have access pc. did discover original version used document.name key (which not unique, changed document.full name)

assumptions:

  • document.fullname unique

things i've ruled out:

  • use of split window
  • opening downloaded documents (protected window documents not counted)

code can used demonstrate issue in word 2010 (adapted original report).

' function credit bas258 (https://social.msdn.microsoft.com/profile/bas258) function test01() boolean     'adapted vba original: 03-11-2012 1.0 visual studio 2008 vb code      dim odoc word.document     dim odoc0 word.document     dim odoc1 word.document     dim odoc2 word.document     dim odoc3 word.document     dim odoc4 word.document      dim n integer      set wdapp = application      wdapp         debug.print (format(now(), "dd-mm-yyyy") & "  ms office " & .application.version)          set odoc0 = .documents.add: debug.print ("add " & odoc0.name)         set odoc1 = .documents.add: debug.print ("add " & odoc1.name)         set odoc2 = .documents.add: debug.print ("add " & odoc2.name)         set odoc3 = .documents.add: debug.print ("add " & odoc3.name)         set odoc4 = .documents.add: debug.print ("add " & odoc4.name)          n = 1 .documents.count             debug.print ("count " & n & "  " & .documents(n).name)         next n          debug.print ("close " & odoc4.name)         odoc4.close         set odoc4 = nothing         debug.print ("close " & odoc3.name)         odoc3.close         set odoc3 = nothing          n = 1 .documents.count             debug.print ("count " & n & "  " & .documents(n).name)         next n          n = 0         each odoc in .documents             n = n + 1             debug.print ("doc " & n & "  " & odoc.name)         next odoc          n = 0         each owin in .windows             n = n + 1             debug.print ("win " & n & " " & owin.document.name)         next owin          debug.print ("close " & odoc2.name)         odoc2.close         set odoc2 = nothing          debug.print ("close " & odoc1.name)         odoc1.close         set odoc1 = nothing          debug.print ("close " & odoc0.name)         odoc0.close         set odoc0 = nothing      end      set wdapp = nothing  end function 

this not going accepted answer. although answer broader question (what cause code crash) not address specific crash trying isolate. either way there appears bug in ms word seemed worth capturing common good.

this time bug windows collection; , joy of joys, i've confirmed both word 2010 , word 2016 - both 64 bit apps.

steps reproduce bug follows:

  • in windows explorer enable preview pane
  • select word document file 'previewed'
  • open same document (without losing 'preview view')
  • run code op, crash on line:
    • if application.windows(indexofavailableappwindows).document.type = wdtypedocument then

it turns out when word file being previewed application.windows.count property incremented preview; attempt property of window results in error 5097 - word has encountered problem.

so, improvement original code therefore be:

' purpose: ' returns healthy document collection '   - work-around word 2010 bug '   - excludes hits windows explorer preview pane public function doccollection() vba.collection     on error goto doccollectionerror     dim indexofavailableappwindows long     dim resultdoc vba.collection     dim founddoc word.document     set resultdoc = new collection      ' use index instead of each avoid loop not initialised error, preview pane     indexofavailableappwindows = 1 application.windows.count         if application.windows(indexofavailableappwindows).document.type = wdtypedocument             set founddoc = application.windows(indexofavailableappwindows).document             resultdoc.add founddoc, founddoc.fullname ' key must not 'name' - fails unique see bug: 1315         end if lblskipthisdoc:     next indexofavailableappwindows      set doccollection = resultdoc     set resultdoc = nothing  exit:     exit function  doccollectionerror:     if err.number = 5097   ' open document open in windows explorer preview pane         err.clear         resume lblskipthisdoc   ' - skip window     end if      if err.number = 457    ' key used, how? unknown cause of error         err.clear         stop 'resume lblskipthisdoc   ' safe skip document, why there duplicate?     end if  end function 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -