none
Macro code(s) for 1) removing then 2) replacing headers. other body text, text info after removing for confidentiality to another user... RRS feed

  • Question

  • Sorry about the long-winded title, but I hope it says it all...!

    This was previously in a thread on the MS Word forum - see here: IT Pro General Discussions

    Can anyone recommend anything that might work - it's driving me mental having to go back and put all the logos, etc., back in afterwards - when just a simple click - boom, all done in one - might suffice? 

    Thanks, 

    FP 


    • Edited by Frecklepaw Tuesday, July 18, 2017 2:19 PM
    Tuesday, July 18, 2017 2:17 PM

All replies

  • You would need to provide more detail on what is to be deleted and how any implications for changes in page layout should be handled.

    And, if, having deleted he unwanted content, you save the document with a different filename, the original document will be unaffected and, hence, the need to 'go back and put all the logos, etc., back in afterwards' will be obviated.


    Cheers
    Paul Edstein
    [MS MVP - Word]


    • Edited by macropodMVP Wednesday, July 19, 2017 5:06 AM
    Wednesday, July 19, 2017 5:04 AM
  • Hi Paul, 

    Thanks for your reply.  I can explain - hopefully it's clear! 

    I have a MS Word document (not an “official” Word template but just a doc used as a template).  It contains sensitive/personal/confidential information that I need for my staff not to be able to view.  This information is contained in the header, footer and in a couple of sections of the body text within the document – one occurrence just being an address and also another piece of information contained within one of the numbered paras further down within the doc (body) text.

    Currently I don't send them the style/template doc due to the fact it has the sensitive information, but instead (as a way round the problem) supply them with a basic doc for use which has the correct formatting (line spacing, font, etc.) minus the sensitive info.  I then just ask them simply to add their text in and when I receive their contribution back I require to then cut and paste all of this information into my official "template" again and it’s very time-consuming and fiddly.  (It may not seem like it may be but it is as there are lots of these jobs to do!).

    How can we both work on the same document?  I'm aware there’s no way to use Word functions to "hide" this information from them but if I was to send them, again, a blank “template” or doc with the correct formatting but just the sensitive/confidential information missing, could a macro be created for use on all those documents that come back to me from staff that will enable me to just hit one button to add back in all the header/footer/other text information I need in one go? 

    ...or maybe there’s an easier way!  There might well be… could be a case of overthinking this but I don’t think so!

    Thanks again,

    FP  

    Wednesday, July 19, 2017 8:36 AM
  • In that case, I'd be inclined to create a generic template (i.e. dotx file) without headers etc. for the users, but including bookmarks, tables and/or or content controls for where the sensitive body content is to go. Then, when the users submit the documents they create from the template, you could use run a macro to automatically add the 'missing' content. Depending on the complexity of that 'missing' content, it could be hard-coded into the macro or simply held in a secondary document whence it is retrieved and inserted.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Wednesday, July 19, 2017 10:18 PM
  • Hi Paul, 

    That sounds like the right solution, unsure how to go ahead and get a macro that would be suitable though - could you recommend something?

    FP 

    Thursday, July 20, 2017 9:50 AM
  • Since your 'official' template apparently already has all the required content, simply bookmark the sensitive content there with the same bookmark names you assign to the corresponding ranges in the user version (you could even delete all other body content from your 'official' template if you wanted) and add the following macro to the 'ThisDocument' code module of your 'official' template, which you'll need to save in the dotm format:

    Private Sub Document_New()
    Application.ScreenUpdating = False
    Dim DocSrc As Document, DocTgt As Document
    Dim Sctn As Section, HdFt As HeaderFooter
    Dim BkMk As Bookmark, Rng As Range, StrNm As String
    Set DocSrc = ActiveDocument
    With Application.FileDialog(FileDialogType:=msoFileDialogFilePicker)
      .Title = "Select the user file"
      .AllowMultiSelect = False
      If .Show = -1 Then
        Set DocTgt = Documents.Open(.SelectedItems(1), ReadOnly:=False, AddToRecentFiles:=True)
      Else
        MsgBox "No target file selected. Exiting", vbExclamation
        GoTo CodeExit
      End If
    End With
    With DocTgt
      For Each Sctn In .Sections
        For Each HdFt In Sctn.Headers
          If HdFt.LinkToPrevious = False Then
            HdFt.Range.FormattedText = DocSrc.Sections.First.Headers(HdFt.Index).Range.FormattedText
          End If
        Next
        For Each HdFt In Sctn.Footers
          If HdFt.LinkToPrevious = False Then
            HdFt.Range.FormattedText = DocSrc.Sections.First.Footers(HdFt.Index).Range.FormattedText
          End If
        Next
      Next
      For Each BkMk In DocSrc.Bookmarks
        StrNm = BkMk.Name
        If .Bookmarks.Exists(StrNm) Then
          Set Rng = .Bookmarks(StrNm).Range
          Rng.FormattedText = DocSrc.Bookmarks(StrNm).Range.FormattedText
          .Bookmarks.Add StrNm, Rng
        Else
          MsgBox "Bookmark " & StrNm & " not found.", vbExclamation
        End If
      Next
    End With
    CodeExit:
    DocSrc.Close SaveChanges:=False
    Set HdFt = Nothing: Set Sctn = Nothing: Set Rng = Nothing
    Set BkMk = Nothing: Set DocSrc = Nothing: Set DocTgt = Nothing
    Application.ScreenUpdating = True
    End Sub

    Then, when a completed document is submitted by a user, simply create a new document from your 'official' template. You'll be prompted to select the user document, which will then be updated with the headers, footers and bookmarked content.

    As mentioned, the above approach assumes you're using bookmarks. The only drawback with this is that any that they're easy for your users delete from their copies of the documents without realising they've done so, in which case the corresponding ranges won't get updated. The code warns of any missing bookmarks.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Proposed as answer by macropodMVP Monday, August 28, 2017 8:01 AM
    Thursday, July 20, 2017 10:54 PM
  • Hi Paul, 

    Thanks so much - I can try and give it a go - never having used bookmarks before it might be a lot to learn. 

    I just wanted to check: 

    If I just made up a simple document which they can adapt (so, not a template but just a doc with all the required formats, i.e. numbering, font, spacing, etc., but MINUS all the sensitive info, then they can work on this and send it back to me... will creating a macro enable me to put all of the header/footer/body text info I need back into the document as this might be an easier way if I can create a macro to do this instead? Rather than working with the templates and bookmarks? 

    I appreciate all your help, I just am unfamiliar with a lot of the bookmarks, etc. so trying to think of the easiest way possible - could the above be an option?  

    Thanks, 

    FP 

    Thursday, July 27, 2017 10:01 AM
  • Hi Paul, 

    I've just tried to give this a go but I have no idea what I'm doing and it didn't work for me - and now I'm not sure what I've done wrong with it! I wasn't sure how to save as dotm and I just probably don't have enough experience to try all this stuff out.  Would there be an easier way I could try? See above enquiry - do you think it would be possible to simply create a macro just to insert the text I need back in each time the user sends me back their doc?

    Thanks again! 

    FP  

    Thursday, July 27, 2017 10:17 AM
  • I think you really do need to learn how to use Word. Saving documents as templates and creating bookmarks are trivial undertakings. See:

    https://support.office.com/en-us/article/Save-a-Word-document-as-a-template-CB17846D-ECEC-49D4-82EA-A6F5E3E8B9AE
    https://support.office.com/en-us/article/Add-or-delete-bookmarks-f68d781f-0150-4583-a90e-a4009d99c2a0

    As for 'would be possible to simply create a macro just to insert the text' that's basically what the macro I posted does - using bookmarks in the template and user document. This approach makes it far easier to maintain your formatting and to do such maintenance as changing the content that gets replicated (if/when the need arises) that coding all that into the body of the macro and leaving you to edit that anytime a change is needed.

    For PC macro installation & usage instructions, see: http://www.gmayor.com/installing_macro.htm
    For Mac macro installation & usage instructions, see: http://wordmvp.com/Mac/InstallMacro.html


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, July 27, 2017 10:13 PM
  • I'm sorry Paul, I'm so useless! I've just managed to "get by" on the basic info I've managed to glean over the years and never had any formal training. So it's embarrassing, I know!

    I am just struggling to understand the process from beginning to end - you may need to break it down for me in step-by-step... er steps.  I fully understand if you don't want to go into this much detail and appreciate all your input so far.  I just don't really know where to start in order to utilise your instructions; I gave it a go, it didn't end well, but I think I saved the macro over another one I had (and then had to put this back in).  I think I wasn't sure how to create/add the new macro (what option do I select? Is it a new "module"?) or save as DOTM - what is/how do I do this?

    Woeful, I know, but hopefully I can learn!  Normally a quick learner! 

    FP  

     
    Friday, July 28, 2017 1:40 PM
  • Steps:

    1. Make a copy of your document containing the sensitive/personal/confidential information.

    2. Open the copy and delete the sensitive/personal/confidential information, assigning a unique bookmark name to each of the deleted ranges (except for the headers/footers).

    3. Save the edited copy as a Word Template, using the .dotx file format.

    4. Open the original and assign the same unique bookmark names to each of the ranges spanning the delete the sensitive/personal/confidential information that you deleted from the copy.

    5. Add the macro code I posted to the document's 'ThisDocument' code module

    6. Save the edited original as a macro-enabled Word Template, using the .dotm file format.

    7. Close both files.

    8. Double-click on the edited copy (i.e. the .dotx template). This will create a new document.

    9. Make some edits to the new document, then save & close it. You will need to assign a new filename.

    10. Double-click on the edited original (i.e. the .dotm template). This will create a new document. The macro should run automatically and you'll be prompted to select an input document.

    11. Choose the new document you just saved. The bookmarked content (containing the sensitive/personal/confidential information), headers and footers from your dotm template should now be transferred to the document you just opened.

    Assuming this has all worked well, you can now distribute the .dotx template to your users, who can now create & edit new documents from it. When they submit their documents to you, you can apply Steps 10 & 11 to those documents.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Proposed as answer by macropodMVP Monday, August 28, 2017 8:01 AM
    Sunday, July 30, 2017 10:35 PM
  • Hi Paul, 

    I really appreciate you outlining these steps for me.  I have followed them exactly as stated; the only issue I am not sure I am entering the macro in properly as it's not asking me when I open up the original which document I would like to select.  

    I should point out (apologies for not mentioning it earlier) the document original has a password set up on it, and so now does the template also; will this be interfering in any way stopping the macro from running properly?  If so, I can try it without but would be better to have the password set up. 

    Alternatively, if this isn't affecting things maybe I am doing something wrong when adding the macro - are you able to break down the steps for adding this macro to the original? 

    Thanks again, 

    FP 

    Saturday, August 5, 2017 11:05 AM
  • A document-opening password in the template shouldn't prevent the macro running once you've input the password. If it's not running, you should check that you've added the macro to the template's 'ThisDocument' code module (not an ordinary one); you should also check your macro security settings to ensure they'll allow macros to run (e.g. 'Disable all macros with notification').

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Saturday, August 5, 2017 9:46 PM
  • Thanks. Will check out how to do this properly and update with progress in the next few days.

    FP 


    Monday, August 7, 2017 2:09 PM