Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Star Lounger
    Join Date
    Oct 2011
    Location
    Auckland. New Zealand
    Posts
    71
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Copy text with a particular style within document

    I am inserting a list of documents from a particular directory into my Word document. I have set it up so the inserted lines have a particular style that will be used only for the list.

    So far so good...

    Now I want to select that list (i.e. all text with that style) and copy it to a bookmark further on the document.

    I have been trying to use the set range command but have come a cropper.

    What's the best way to do this?

  2. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,888
    Thanks
    4
    Thanked 272 Times in 249 Posts
    Is your list still in the clipboard? If so, you just paste it a second time. Do you know anything about the range eg where it starts?

    Can you show us the code you are using to 'insert' the list.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  3. #3
    Star Lounger
    Join Date
    Oct 2011
    Location
    Auckland. New Zealand
    Posts
    71
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Hi Andrew (Welcome back and Happy New Year!)

    This is the code I use to insert the list (complete with comments):
    Code:
    'Go to the bookmark called FileList
        ActiveDocument.Bookmarks("FileList").Select
        Set OurRange = ActiveDocument.Bookmarks("FileList").Range
        'Set the NewDirectory field to the contents of the FileList bookmark
        NewDirectory = OurRange.Text
        'Change directory to the contents of the FileList bookmark
        ChDir NewDirectory
        'Only find files with a suffix of pdf
        myName = Dir("*.pdf")
        'Go to the bookmark that indicates where the files should be inserted
        ActiveDocument.Bookmarks("InsertFilesHere").Select
               
        Do While myName <> ""
            'set two names - one is a display name and the other has the full name including suffix so I can use it to add a hyperlink
            NameWithExtension = myName
            NameWithoutExtension = CreateObject("Scripting.FileSystemObject").GetBaseName(myName)
            Selection.InsertAfter NameWithoutExtension
            ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:=NameWithExtension
                        
            myName = Dir()
            If myName <> "" Then
                Selection.Paragraphs.Add
                'Change the style of the inserted files so that I can identify them later (to insert into another location)
                Selection.Paragraphs.Style = "FileBullet"
            End If
        Loop
    BUT.....

    The requirement is to insert the list into the document and copy it to the appendices.
    Then, the author wants to be able to edit the list (i.e. delete any files not required) in the body of the document and the list in the appendices will be updated 'automatically'.
    The person requesting this has used a template with this functionality in the past so he wants it again (he's changed companies). Having said that, he did ask for the list in the main part of the document to have 'buttons like you get in Excel' (radio buttons?) so we can select the ones that should be inserted into the appendix.

    Unfortunately the information is coming to me via a third party (not the actual person who will be using the template) which is extremely frustrating and I've tried very hard to get to speak to the actual person so I can find out exactly what outcome they want (i.e. why they want the list duplicated). If it was up to me I'd either just insert the list once into the appendices and they can edit it there, or I'd put the list in the body of the document and let them manually copy and paste into the appendices. I haven't ruled out just giving them one of these options.

    The above code is in the AutoNew sub. Somehow I have to know when they've edited the list and then copy it to the appendices (or maybe just keep copying it). My concern is that they will edit the list in the document, and then keep working through the document until they get to the appendix. Now, because they haven't actually done anything to tell the template to duplicate the list, they'll think the template doesn't work and they'll copy the list manually anyway.

    The third party giving me the instructions (i.e. the boss) says that I can come up with a 'workable solution'.

    My option would be to 'keep it simple': create one list in the body of the document and let them copy and paste it manually into the appendix once they've edited it. But, as I thought they had one originally, I thought I should try to give them an automated version....
    Last edited by macropod; 2017-01-10 at 15:36. Reason: Added code tags

  4. #4
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,888
    Thanks
    4
    Thanked 272 Times in 249 Posts
    Hi Niki

    Your terminology is very unclear but from what you've said and looking at your code it looks like the requirement is:
    1. Read the text at bookmark "FileList" to get a folder path
    2. Search that folder path to get a list of all the PDFs in that location and write the list to the bookmark "InsertFilesHere"

    is that what you want to do?
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  5. #5
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,888
    Thanks
    4
    Thanked 272 Times in 249 Posts
    You can run a macro when the filelist location is changed if you put it into a ContentControl instead of a bookmark. Then is it easy to add a macro triggered by exiting a ContentControl so that any change runs the code to update the list.

    Using a bookmark to hold the folder location is fraught with danger since the user can easily edit the text but it may or may not be included in the bookmark.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  6. #6
    Star Lounger
    Join Date
    Oct 2011
    Location
    Auckland. New Zealand
    Posts
    71
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Yes. This part of it works fine. It's the requirement to then copy this list of files into another location that's causing me problems, especially as the author will want to 'edit' the list (i.e. delete some files from the list) first. I'm thinking that I just tell them I will insert the files into one location within the document and if they want them copied to another location they can use copy and paste (after they've manually edited the list).

  7. #7
    Star Lounger
    Join Date
    Oct 2011
    Location
    Auckland. New Zealand
    Posts
    71
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Yes - I wasn't sure about using a bookmark but I figured it was better than hard-coding the location. I've looked up ContentControls. I'm not entirely sure how to use them for this purpose but I'll give it a go. It sounds like the functionality the guy had originally.

  8. #8
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,888
    Thanks
    4
    Thanked 272 Times in 249 Posts
    This code works with a ContentControl if you give it a title of Path. Put the macro into your ThisDocument module and it will run when the user moves the cursor out of the content control. If they then want to delete any of the items they can do that but they will be restored if they go back to the content control.

    Code:
    Private Sub Document_ContentControlOnExit(ByVal aCC As ContentControl, Cancel As Boolean)
      Dim sDir As String, aRng As Range, lngListStart As Long, i As Integer, aHL As Hyperlink
      Dim oFSO As Object, oFolder As Object, colFiles As Object, oFile As Object, sFile As String
        
      If aCC.Title = "Path" Then
        sDir = aCC.Range.Text
        Set aRng = ActiveDocument.Bookmarks("InsertFilesHere").Range
        lngListStart = aRng.Start
        aRng.Text = ""
        aRng.Style = "List"
        
        Set oFSO = CreateObject("Scripting.FileSystemObject")
        Set oFolder = oFSO.GetFolder(sDir)
        Set colFiles = oFolder.Files
        
        For Each oFile In colFiles
          If LCase(oFSO.GetExtensionName(oFile.Name)) = "pdf" Then
            If i > 0 Then aRng.InsertParagraphAfter
            aRng.Collapse wdCollapseEnd
            sFile = oFSO.GetBaseName(oFile)
            Set aHL = aRng.Hyperlinks.Add(Anchor:=aRng.Paragraphs.Last.Range, Address:=oFile.Path, TextToDisplay:=sFile)
            Set aRng = aHL.Range
            i = 1 + 1
          End If
        Next
        aRng.Start = lngListStart
        ActiveDocument.Bookmarks.Add Name:="InsertFilesHere", Range:=aRng
      End If
    End Sub
    If you want the user to decide which files to include *while* the macro is running, you will need to construct a userform to list the hits and allow selection.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  9. #9
    Star Lounger
    Join Date
    Oct 2011
    Location
    Auckland. New Zealand
    Posts
    71
    Thanks
    8
    Thanked 0 Times in 0 Posts
    I've just realised that my reply didn't post for some reason. It's been a few days since I played around with it last so it's not very fresh in my memory any more but we'll see how we go. Bottom line - it didn't work.

    I played around with the code and couldn't get it to work. I did a crash course in Content Controls and have set up a rich text control that contains the hidden text with the file location, and the bookmark 'InsertFilesHere'. However, nothing is being inserted.

    I have just moved the bookmark out of content control. Nothing came up but when I clicked in the Content Control I got a Run-time error '76' message at the line: Set oFolder = oFSO.GetFolder(sDir). sDir is being set correctly (to the path in my hidden text).

    Last week (when I was playing around just prior to typing my initial reply, nothing was inserted initially but when I clicked in the Content Control I got these file names: Document1, Document2, Document3. None of these are in my directory, and none have a suffix of pdf. I haven't been able to recreate that this morning.

    Also, another strange thing... I have a bookmark called 'CursorPosition' that (oddly enough) positions the cursor when the document is first created, and whenever it's opened. For some reason the Private Sub statement (at the very top of your code) is being populated where the cursor is being positioned.

    To clarify:
    1. When the document is first created, I want the list of files to be inserted. (i.e. it'll be in the AutoNew sub - I have tried moving the code to here but I came a cropper as I had to set up dims for Content Control and aCC. I can't remember what I put now but it didn't work.
    2. The author can update the file list (by deleting files or adding new ones).
    3. The updated list is copied to the appendices.

  10. #10
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,888
    Thanks
    4
    Thanked 272 Times in 249 Posts
    Can you upload the file you are working with?

    The code worked fine for me but then again, I made sure the CC contained a valid folder path and nothing else. Are you perhaps putting the found files into that location too?

    AutoNew doesn't fire after the document has already been created. Did you want it to update every time the document is opened or just the first time? If it is happening the first time only, when did the folder path get supplied?
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  11. #11
    Star Lounger
    Join Date
    Oct 2011
    Location
    Auckland. New Zealand
    Posts
    71
    Thanks
    8
    Thanked 0 Times in 0 Posts
    I have stripped the template bare and moved the bookmark to outside of the Content Control (so the CC contains only the path).

    The first time the document is created (opened?), the list should be populated.
    Then the author will edit the list (by deleting and/or adding files).
    Then the list should be copied to the appendices (or to the next Heading 1 in my attachment). This is why I put the list in the Content Control - thinking that then you would know the author had moved out of the CC and the list could be copied. If, for some reason the author went back into the CC and updated the list again, the updated list would be copied again.
    Attached Files Attached Files

  12. #12
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,888
    Thanks
    4
    Thanked 272 Times in 249 Posts
    The macro works fine in your stripped down doc so the issue is just getting it to run at the right time. IMO, the macro needs to run after the file path is changed. New documents might as well have the list already in there if you aren't going to change the path before building the list.

    If you want to run the macro ONLY when a new document is created and BEFORE the author edits the folder path then you need to use the AutoNew macro instead of trying to capture the event of the author changing the path.
    Code:
    Sub AutoNew()
      ' This macro runs when a new document is created from the template
      Dim sDir As String, aRng As Range, lngListStart As Long, i As Integer, aHL As Hyperlink
      Dim oFSO As Object, oFolder As Object, colFiles As Object, oFile As Object, sFile As String
      Dim aCC As ContentControl
      
      For Each aCC In ActiveDocument.ContentControls
        If aCC.Title = "Path" Then
          sDir = aCC.Range.Text
          Set aRng = ActiveDocument.Bookmarks("InsertFilesHere").Range
          lngListStart = aRng.Start
          aRng.Text = ""
          aRng.Style = "List"
          
          Set oFSO = CreateObject("Scripting.FileSystemObject")
          If (oFSO.FolderExists(sDir)) Then
            Set oFolder = oFSO.GetFolder(sDir)
            Set colFiles = oFolder.Files
            For Each oFile In colFiles
              If LCase(oFSO.GetExtensionName(oFile.Name)) = "pdf" Then
                If i > 0 Then aRng.InsertParagraphAfter
                aRng.Collapse wdCollapseEnd
                sFile = oFSO.GetBaseName(oFile)
                Set aHL = aRng.Hyperlinks.Add(Anchor:=aRng.Paragraphs.Last.Range, Address:=oFile.Path, TextToDisplay:=sFile)
                Set aRng = aHL.Range
                i = 1 + 1
              End If
            Next
            aRng.Start = lngListStart
            ActiveDocument.Bookmarks.Add Name:="InsertFilesHere", Range:=aRng
          Else
            MsgBox "Folder doesn't exist"
          End If
        End If
      Next aCC
    End Sub
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  13. #13
    Star Lounger
    Join Date
    Oct 2011
    Location
    Auckland. New Zealand
    Posts
    71
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Hi Andrew

    Thanks for this - yes, I will have to put the procedure in the AutoNew sub. I already had a procedure that populated the list. My problems were:
    1. How do I know when/if the list has been updated? You've suggested using a CC but surely to do this my code needs to be in the CC and if I do that I can't get it to work at all.

    2. How do I copy the list to the appendices? I initially thought I could do this by using a Style only for this list and then saying 'copy any text from within this document with this Style to this bookmark' but I came a cropper when I tried to select a range of text with the required style.

  14. #14
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,888
    Thanks
    4
    Thanked 272 Times in 249 Posts
    Niki

    1. How do you WANT to know the list has been updated? You could add a msgbox to tell you if you didn't trust the autonew macro to run. Or you could leave the template with no listed PDFs and when you see it with some entries you will know that it has been populated.

    It seems like you have three parts to this:
    - the folder path (which IMO should be just hard coded into the vba since you want the list of pdfs built from the same path every time)
    - the list of PDFs displayed in the body of the document and refreshed when a new document is created
    - a cross-reference in the appendix to the list of PDFs

    The code I provided takes care of the first two parts. All you need to do is add the cross-reference to 'InsertFilesHere' in the appendix and add a line of code to update the fields.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  15. #15
    Star Lounger
    Join Date
    Oct 2011
    Location
    Auckland. New Zealand
    Posts
    71
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Hi Andrew

    We aren't quite talking about adding the same functionality. Taking it right back....
    1. When a new document is created from the template, the template will insert a list of files from a folder - so the coding will be in the AutoNew macro.
    2. Also within the AutoNew macro, the initial list will be copied to an appendix.
    3. To make it easier for the client we have used hidden text and a bookmark with the path. This means it can be changed easily if required (as the client won't have access to the code). It doesn't matter if the document's author writes over it/loses the bookmark as the path is only used once when a new document is created from the template. So long as the actual template isn't updated it'll be fine (and if they do lose the bookmark, the instructions we provide will tell them how to re-insert it) - I'll add an error routine if the bookmark can't be found.

    3. Once the document is created and the author (i.e the person who created the document from the template) is making changes to it, they want to be able to manually delete files from the list, or add other files to the list. They have used a template with this functionality before and have said they updated it in the body of the document.
    4. We need to know that the list has been updated so we can copy the full updated list to the appendices (again). This is where I thought content controls could be used - there is an exit procedure so you know if the author is in the cc and when they exit. (Is this correct?)

    So as far as your 3 parts go:
    1. Folder path - we don't want to hard code it for a variety of reasons. I am happy with the current use of the bookmark and hidden text for the reasons outlined above.
    2. We want the list to be generated only when a new document is created. We won't be refreshing the list automatically at any stage. The file names will be inserted automatically right at the start but they may be changed manually by the document's author (i.e. the person who has created the document from the template and who is now updating the document)
    3. I guess we could add a cross-reference to the list (from the appendix). As mentioned, I haven't spoken to the client myself but I understand they may want to print some of the files from the appendices, hence the term 'copied' - they want a hyperlink so they can open it easily. If it was up to me I'd just have one list in one location in the document and if they want to, they could just copy it. But then I'm not the one liaising with the client.

    I hope this helps to clarify our requirements.

Page 1 of 2 12 LastLast

Posting Permissions

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