Results 1 to 8 of 8

Thread: [RESOLVED] closing files when error 5981 occrs

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2016
    Posts
    3

    Resolved [RESOLVED] closing files when error 5981 occrs

    I have word vb code which navigates a directory structure and opens all word files in read only mode for text string content checking. Occasionally a word document fails to open and reports error code 5981. I wish to close the file and continue the run but activedocument.close and documents(myfilename).close have no effect and all such files remain open and have to be closed manually at end of run. Immediately after opening the active document is shown as <Cannot open macro storage.> in the Documents collection.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: closing files when error 5981 occrs

    have you tried application.displayalerts = false?

    what version of word /windows?

    i found this from some other forum

    I found the solution. Right click the docx, select properties then UNBLOCK. It would seem that word puts a block on the file if its downloaded from the internet.
    Last edited by westconn1; Jul 2nd, 2016 at 06:15 PM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: closing files when error 5981 occrs

    I assume you have error handling code wrapping your document open routine?
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: closing files when error 5981 occrs

    there is methods by code to unblock documents
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2016
    Posts
    3

    Re: closing files when error 5981 occrs

    Thanks for the prompt replies. Yes I do set the error trap. I am using Word 2010. I will try display alerts but as I do not get a display when running under VB I don't understand why this is a fix - but worth a try. Manually unblocking the document is not an option in the middle of a VB controlled run examining several thousand files and finding a handful fail with error 5981. I think that one solution is to open the files in a separate instance of Word but in a thin client environment with 2003 and 2010 versions of word I have a problem on how to code to always obtain a 2010 instance and how to test this code in a variety of different user environments.

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: closing files when error 5981 occrs

    i do not know how, by code, to test if the documents are blocked, but it would be possible to unblock all files prior to opening them in word, within the code

    whether the time overhead of doing this would make it unviable i can not say, it may, to some degree at least, depend on the size of the files, if the whole process is being run in background it may not matter at all
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: closing files when error 5981 occrs

    Out of curiosity is your word document format docx?
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

  8. #8

    Thread Starter
    New Member
    Join Date
    Jul 2016
    Posts
    3

    Re: closing files when error 5981 occrs

    Docx - I have recoded to open in a separate instance of word and this has overcome the problem - thanks for you help

Posting Permissions

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



Click Here to Expand Forum to Full Width