+ Reply to Thread
Results 1 to 7 of 7

Prevent code from running on another open document

  1. #1
    Forum Contributor Marco-Kun's Avatar
    Join Date
    04-14-2009
    Location
    Holland
    MS-Off Ver
    Dutch:2007
    Posts
    298

    Prevent code from running on another open document

    Hello everyone. I have Word documents with a code like below. This code activates when one tries to save or print. It goes through a list of required fields which cannot be empy. The problem is that when two documents (or more) are open, and someone tries to save one document, it errors on fields which are in the code of the other document. These fields are not in the document which is the person is trying to save.

    How can I make it so that the code for the required fields only runs on the document where this code is located??

    Kind regards and thanks in advance,

    Marco

    Please Login or Register  to view this content.

  2. #2
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Prevent code from running on another open document

    Try:
    Please Login or Register  to view this content.
    Note that you don't need the overhead of creating a collection - you can simply add the formfield names to a string and use the Split function to retrieve them. I've also replaced a number of 'ActiveDocument' references with 'Doc', since that is what your ChkFlds sub is processing.
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Forum Contributor Marco-Kun's Avatar
    Join Date
    04-14-2009
    Location
    Holland
    MS-Off Ver
    Dutch:2007
    Posts
    298

    Re: Prevent code from running on another open document

    Thank you Paul. I will test this as soon as I can and I will report back here.
    At least I can already say that I like how you put the formfields in a string like that! I didn't think about the ActiveDocument references too, thanks!

  4. #4
    Forum Contributor Marco-Kun's Avatar
    Join Date
    04-14-2009
    Location
    Holland
    MS-Off Ver
    Dutch:2007
    Posts
    298

    Re: Prevent code from running on another open document

    Hello Paul

    I tested the code and it still triggers on another open document.
    I made two test files to make it easier to understand.

    In File1.doc are three fields (Test1 - Test4) and 2 checkboxes for selecting a gender.
    In File2.doc are six fields (Test4 - Test9) and also these checkboxes.

    When you open these files individually and you try to save or print, the message will be correct.

    But when you open both files and you try to save File1.doc, it will come with two messages. One saying that fields Test1 - Test4 are not completed yet, and one message saying that field Test4 is not completed yet. This message is probably created from File2.doc, since Test4 is also referenced in that code.

    Kind regards and thanks for you help!

    Marco
    Attached Files Attached Files

  5. #5
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Prevent code from running on another open document

    Ordinarily:
    1. all this code would go in a template, so only documents based on that template would trigger it;
    2. one would normally have only one such document open at a time; and
    3. you wouldn't have fields with the same names in the documents to which the macro applies.
    So on all three counts, your design isn't sound. That said, you can probably eliminate the problems by changing:
    Call ChkFlds(Doc)
    to:
    Call ChkFlds(ThisDocument)
    You can also make the code a bit more robust by changing:
    If Doc.FormFields.Count < (UBound(Split(FldsList, ",")) + 1) Then Exit Sub
    to:
    If Doc.FormFields.Count <> (UBound(Split(FldsList, ",")) + 1) Then Exit Sub
    and by changing the error-handling so that the code exits, rather than resuming, if a listed formfield isn't found. The 'ThisDocument' fix should be enough, though.

  6. #6
    Forum Contributor Marco-Kun's Avatar
    Join Date
    04-14-2009
    Location
    Holland
    MS-Off Ver
    Dutch:2007
    Posts
    298

    Re: Prevent code from running on another open document

    Hello Paul,

    I will take those 3 rules for granted as I'm not really sure what the usual case is, as this is the first time me trying to achieve this.

    The original documents are for example to register a new employee, and another document for his personal information.
    It does happen that 2 (or more) files has the same fields. I'm not really sure what you mean with point #2. Should I not give the fields the same name, even though they do have the same data? (for example; Date of Birth could be entered on several documents)

    I changed the things, but now the code doesn't do anything anymore.

    Kind regards,

    Marco
    Attached Files Attached Files

  7. #7
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Prevent code from running on another open document

    Try using 'Call ChkFlds(Doc)' with the 'ChkFlds' sub coded this way:
    Please Login or Register  to view this content.
    This way, the code should only intercept saves & prints on documents that exactly match the required formfields. Even this isn't going to be completely satisfactory, though. Ultimately, you should be using templates. In that case, the could could test either: (a)the template name; or, alternatively, if all the code was in a single template, (b) a custom document document property indicating which of the various document kinds requiring validation you were working with.
    Last edited by macropod; 12-20-2012 at 04:22 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1