Solved

Copy only visible text

Posted on 2013-06-25
6
456 Views
Last Modified: 2013-06-25
I use InsertFile to copy bookmarked text from one Word 2003 document to the active document as in the code below:
ActiveDocument.Bookmarks("Bookmark1").Range.InsertFile strDocName, "Bookmark1"

Open in new window

This copies the hidden text that is in the bookmark as well as the visible text.

Is there a way to copy only the visible text in the bookmark or delete the hidden text after it is copied into the active document?

TIA
0
Comment
Question by:thenelson
  • 3
  • 3
6 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 39274707
If there is no other hidden text in the receiving document, you could use Find and Replace to delete the hidden text.
Using a range in this case seems to be a bit quirky, so here is a macro that uses the Selection instead.
Sub DeleteHidden()
    Selection.WholeStory
    With Selection.Find
        .ClearFormatting
        .Text = "*"
        .Font.Hidden = True
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchWildcards = True
        .Execute Replace:=wdReplaceAll
    End With
End Sub

Open in new window

0
 
LVL 39

Author Comment

by:thenelson
ID: 39274871
I don't want to delete hidden text in the entire document, just the copied bookmark. So I'll try the code:
Sub DeleteHidden()
    Selection.GoTo wdGoToBookmark,,,"Bookmark1"
    With Selection.Find
        .ClearFormatting
        .Text = "*"
        .Font.Hidden = True
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchWildcards = True
        .Execute Replace:=wdReplaceAll
    End With
End Sub

Open in new window

Thanks!
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39274926
OK, I wasn't sure.
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 39

Author Comment

by:thenelson
ID: 39275027
I discovered find only finds text that is displayed (not hidden) and I want to select the text before I delete the hidden text so I changed the code to:
Sub DeleteHidden()
ActiveWindow.ActivePane.View.ShowAll = True    'display everything
With Selection.Find
    .ClearFormatting
    .Text = "*"
    .Font.Hidden = True
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .MatchWildcards = True
    .Execute Replace:=wdReplaceAll
End With
ActiveWindow.ActivePane.View.ShowAll = False
End Sub

Open in new window

Note I changed .Wrap = wdFindContinue to .Wrap = wdFindStop so it doesn't replace hidden text in the entire document.
And I call it like so:
Selection.GoTo wdGoToBookmark,,,"Bookmark1"
DeleteHidden

Open in new window

Thanks for your input! It does what I need.
0
 
LVL 39

Author Comment

by:thenelson
ID: 39275390
One more change:

I discovered that hidden fields are not deleted by the find command so I added code to handle that too:
Sub DeleteHidden()
Dim aField As Field

ActiveWindow.ActivePane.View.ShowAll = True
For Each aField In Selection.Fields
    If aField.Code.Font.Hidden Then aField.Delete
Next aField
With Selection.Find
    .ClearFormatting
    .Text = "*"
    .Font.Hidden = True
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .MatchWildcards = True
    .Execute Replace:=wdReplaceAll
End With
ActiveWindow.ActivePane.View.ShowAll = False
End Sub

Open in new window

0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39276543
Thanks, and well done.

It is always encouraging when a suggestion is taken up and expand upon.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

My experience with Windows 10 over a one year period and suggestions for smooth operation
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now