none
Issue creating Master document with Subdocuments.AddFromFile RRS feed

  • Question

  • I have inherited a document production application that uses VS 2012 (C#) to generate a master document by merging multiple documents as subdocuments.  This works fine the first time around, but I am having issues when user tries it for a second time without closing the existing instance of word application.  For ex. the first time around, the user selects multiple "Forms" (which are in fact just .doc files on a network folder) and selects generate document (they are creating a policy document that consists of various forms).  The C# code then instantiates a word application object and a word document object (of wdMasterView view type).  It then copies the network "Form" .doc file to the current user's temp folder and uses wordDoc.Subdocuments.AddFromFile() method passing in as the first parameter the path to the document file in the temp folder.   This works just fine and the resulting master document is displayed for the user to work with.  Normally the user saves this document into their local folder (or sometimes just imports it into our document management application) and closes the word instance before starting work on the next policy.  But sometimes they just minimize the word application and start working on the next policy trying to repeat the above process.  If they selected the same "Form" .doc file, then they get an error message that the file is in use.

    It looks like the Subdocuments.AddFromFile() actually locks the file in the temp folder.  Is there a way to prevent the file from getting locked?

    Wednesday, October 9, 2013 2:33 PM

Answers

  • Hi arvindram

    Mmm, yes, not having saved to disk is probably the issue, here...

    The only other possibility that occurs to me would be to remove the Master/Subdocument structures, as discussed previously, then get the content of the document using the Document.Content.WordOpenXML property as a string.

    You should be able to "hold onto this", close the original document (thus, hopefully, releasing the lock), start a new document and write the WordOpenXML into the new file using the Range.InsertXML method. Worth a try, anyway...?


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, October 11, 2013 3:48 PM
    Moderator
  • I am exploring with management about the possibility to save the document to the user's temp folder and then re-opening it as an option.  This seems to be the safest way (and probably the easiest).  The issue may be raised about cluttering their temp folder with these documents, but maybe I can plan a cleanup (the first time it is invoked in a day or something like that).

    Thanks a lot for your help and suggestions.  Great forum here.

    Friday, October 11, 2013 3:58 PM

All replies

  • Some more information:

    Subdocuments.AddFromFile( copiedFileInTempFolder, False [CONFIRMCONVERSIONS], True [READONLY], Missing [PASSWORDDOCUMENT], Missing [PASSWORDTEMPLATE], True [REVERT], Missing [WRITEPASSWORDDOCUMENT], Missing [WRITEPASSWORDTEMPLATE]);

    Wednesday, October 9, 2013 2:43 PM
  • Master Documents are for people who enjoy corrupt files. See:
    http://word.mvps.org/FAQs/General/WhyMasterDocsCorrupt.htm
    http://word.mvps.org/FAQs/General/RecoverMasterDocs.htm
    http://www.addbalance.com/word/masterdocuments.htm

    Is there a reason you can't simply insert the content from all of the component documents into a single file, or use INCLUDTETEXT fields to link the content to the source files?


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Wednesday, October 9, 2013 10:01 PM
  • Like I mentioned in my first post, this is something that I inherited when I joined my current position recently.  Apparently this has been working like this for ever.  I am not looking for a total rewrite.  And the user almost always does not save the document but rather prints it a document management system (it stores it as tiff files or PDF - i.e. not editable).  The "master" document is just displayed (not even saved, but just on the screen) until the user prints it to off.  And since this master document is built from multiple documents that have their own header/section/font etc. I don't think it would be easy to insert the content from all component documents into this single file since it will probably inherit the header/font/section style of the first document.  And to confuse stuff even more, most of the component documents have merge fields that get data by a word merge process that happens after all the documents are inserted into a "master" document.

    Currently the users know to close the document that they are working on before starting a new one, but sometimes they forget (minimize it) and this causes problems.

    Wednesday, October 9, 2013 10:59 PM
  • A simple workaround for the code you're already using is to test whether there is already a Master Document open before opening the new one. In VBA, the code for this might look like:

    Dim wdDoc As Document
    For Each wdDoc In Application.Documents
      With wdDoc
        If .Subdocuments.Count <> 0 Then
          .Close SaveChanges:=False
          Exit For
        End If
      End With
    Next

    The above code simply closes any open Master Documents. If you'd prefer to return the user to the already-open master Document, instead of '.Close SaveChanges:=False' you might have:

          MsgBox "You already have a master document open:" & vbCr & _
          .Name & vbCr & _
          "Please finish working with this document, then close it before continuing."
          .Activate


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, October 10, 2013 1:18 AM
  • Thanks for your suggestion.  I already thought about that, but discarded it because the process currently creates another word application object and creates a new master document.  If the user selects subdocument A, B and C first time around and minimizes it and D, E, F second time around, there are no issues.  The second master document is generated with D, E and F subdocuments and they go on about their business.  But if they are trying to generate D, E and C (as an ex.), then it complains about "C" being locked by another process.  I was hoping that there might be a workaround or an alternative to the Subdocuments.AddFromFile or some sort of config or registry hack to prevent the added file from being locked.  I saw that there was a Subdocuments.Merge method available, but don't know how (or if) I can use it instead of the Subdocuments.AddFromFile.  I could not find any documentation regarding either methods that talk about what happens to the added document (i.e. why it is locked and if there is a way to only insert the contents and not lock it).

    Thursday, October 10, 2013 2:07 AM
  • Given that your code apparently already opens the subdocuments as read-only (as indicated by 'True [READONLY]' in your code), I'm surprised you're being warned they're in use by another process. The symptoms suggest to me that your OS is not respecting the read-only opening. Do you get the same warning if a different user has the subdocument opened?

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, October 10, 2013 3:13 AM
  • That won't happen because the original document (subdocument) is copied to the users local temp folder so that there is no contention between concurrent users of the system.
    Thursday, October 10, 2013 10:50 AM
  • Hi arvindram

    <<It looks like the Subdocuments.AddFromFile() actually locks the file in the temp folder.  Is there a way to prevent the file from getting locked?>>

    Yes, that is the case. If the user would close the document, the file should be released. Have you tried having your software check whether the file it generated is closed and, if not, close it?

    Another possibility would be to, as the last step generating the document, remove the sub document structures (turn the document into a "normal" file). That should release the sub-documents.

    Or, copy the *.doc again, to a different file name and use that in a next iteration?


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, October 10, 2013 12:49 PM
    Moderator
  • Hi Cindy,

    We cannot close the document because it is generated on the fly and not saved anywhere.  It is displayed as an unsaved document to the user and they normally (almost always) make minor changes (rarely) and print this into another application (document management system).  I have tried to save it to the drive but that did not seem to help (i.e. the lock is still there on the subdocument).  How can this be turned into a "normal" file without losing the formatting (headers/fonts/section breaks)?

    Thursday, October 10, 2013 1:35 PM
  • Hi avindram

    Sub RemoveAllSubDocs()
        Dim d As word.Document
        Set d = ActiveDocument
        d.Subdocuments.Delete
    End Sub

    This removes only the subdocument structures, none of the content. So everything should stay intact. What is lost is the link to the subdocuments (all the content is incorporated into the main document) and all the support structures for Master-/Subdocument.


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, October 10, 2013 3:44 PM
    Moderator
  • Hi Cindy,

    Sorry, could not respond earlier.  Fighting other fires.

    That did not seem to help.  Even after the delete, the lock is still on the document that is in the temp folder.  I will have to re-think this to see if I can either save the document somewhere on the user's machine and re-open it using winapi.

    Friday, October 11, 2013 3:35 PM
  • Hi arvindram

    Mmm, yes, not having saved to disk is probably the issue, here...

    The only other possibility that occurs to me would be to remove the Master/Subdocument structures, as discussed previously, then get the content of the document using the Document.Content.WordOpenXML property as a string.

    You should be able to "hold onto this", close the original document (thus, hopefully, releasing the lock), start a new document and write the WordOpenXML into the new file using the Range.InsertXML method. Worth a try, anyway...?


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, October 11, 2013 3:48 PM
    Moderator
  • I am exploring with management about the possibility to save the document to the user's temp folder and then re-opening it as an option.  This seems to be the safest way (and probably the easiest).  The issue may be raised about cluttering their temp folder with these documents, but maybe I can plan a cleanup (the first time it is invoked in a day or something like that).

    Thanks a lot for your help and suggestions.  Great forum here.

    Friday, October 11, 2013 3:58 PM