Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-12-2017, 08:51 PM
puff puff is offline Save highlights within the current selected regions into a fixed document Windows 7 64bit Save highlights within the current selected regions into a fixed document Office 2013
Advanced Beginner
Save highlights within the current selected regions into a fixed document
 
Join Date: Apr 2017
Posts: 60
puff is on a distinguished road
Question Save highlights within the current selected regions into a fixed document

My goal: copy and paste the highlights within the selected regions to another doc file (see the picture below). This means that when I select the region and run my macro, I want only "group together as" to be the result I get.

My current macro:

Sub CopyHighlightsSelectedToOtherDoc()
Dim ThisDoc As Document
Dim ThatDoc As Document
Dim r As Range
Set ThisDoc = ActiveDocument
Set r = ActiveDocument.Range(Selection.Range.Start, Selection.Range.End)
Set ThatDoc = Documents.Add
With r.Find
.Text = ""
.Highlight = True
Do While .Execute(Forward:=True) = True


ThatDoc.Range.InsertAfter r.Text & vbCrLf
r.Collapse 0
Loop
End With
End Sub

Problem: I'm a newbie and just found the original code somewhere else and modified it a bit. But there will always be an error on the Range Selection part. In this case, is that possible to let the macro use the currently selected sections only not the whole document? Some posts said macro cannot do things like that. My current code always goes to the end of the document, which gives "group together as" and also "accomplish a task automatically".

Additionally, how to make the macro to save the results in another fixed document? Let's say I want to save it in a file named "container.docx" on the desktop. I tried Set ThatDoc = C:\Users\apple\Desktop\container.docx, but that didn't work. How to solve this then?

I really appreciate anyone who can provide some help.
Attached Images
File Type: jpg Pic1.jpg (137.7 KB, 32 views)
Reply With Quote
  #2  
Old 04-12-2017, 11:29 PM
gmayor's Avatar
gmayor gmayor is offline Save highlights within the current selected regions into a fixed document Windows 10 Save highlights within the current selected regions into a fixed document Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,144
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

The following should work (at least on the PC version of Word), provided the folder you mentioned exists:

Code:
Option Explicit

Sub CopyHighlightsSelectedToOtherDoc()
Dim ThisDoc As Document
Dim ThatDoc As Document
Dim r As Range, orng As Range
Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    Const strDoc As String = "C:\Users\apple\Desktop\container.docx"    'folder must exist
    Set ThisDoc = ActiveDocument
    Set r = Selection.Range
    Set orng = Selection.Range
    If Len(r) > 0 Then
        If fso.FileExists(strDoc) Then
            Set ThatDoc = Documents.Open(strDoc)
        Else
            On Error GoTo err_Handler
            Set ThatDoc = Documents.Add
            ThatDoc.SaveAs2 strDoc
        End If
        With r.Find
            .Highlight = True
            Do While .Execute(Forward:=True) = True And r.InRange(orng)
                ThatDoc.Range.InsertAfter r.Text & vbCrLf
                r.Collapse 0
            Loop
        End With
        ThatDoc.Save
    Else
        MsgBox "Nothing selected"
    End If
lbl_Exit:
    Set r = Nothing
    Set orng = Nothing
    Exit Sub
err_Handler:
    ThatDoc.Close 0
    MsgBox "Target document not available"
    Err.Clear
    GoTo lbl_Exit
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 04-13-2017, 03:41 AM
macropod's Avatar
macropod macropod is offline Save highlights within the current selected regions into a fixed document Windows 7 64bit Save highlights within the current selected regions into a fixed document Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Cross-posted at: http://www.tek-tips.com/viewthread.cfm?qid=1776432
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #4  
Old 04-19-2017, 10:34 AM
puff puff is offline Save highlights within the current selected regions into a fixed document Windows 7 64bit Save highlights within the current selected regions into a fixed document Office 2013
Advanced Beginner
Save highlights within the current selected regions into a fixed document
 
Join Date: Apr 2017
Posts: 60
puff is on a distinguished road
Default

Thank you so much! It really works and saves me lots of time
Reply With Quote
Reply

Tags
highlighted-text



Similar Threads
Thread Thread Starter Forum Replies Last Post
Chart with Actuals and Goals for Multiple INTL Regions Italiano329 Excel 4 01-22-2016 09:48 AM
Save highlights within the current selected regions into a fixed document VBA to move selected emails or conversation to current year pst folder webharvest Outlook 1 08-15-2015 01:55 AM
Save highlights within the current selected regions into a fixed document Fixed duration, fixed units, fixed work, effort driven. How, why and when use it all Evgeny Project 3 01-03-2013 06:45 AM
Save highlights within the current selected regions into a fixed document How to use fixed duration, fixed work and have resources/units leveled automatically? Joeb Project 3 06-25-2012 04:18 PM
Save Current Slide as Picture excelledsoftware PowerPoint 2 01-18-2012 02:42 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:10 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft