Solved

Copy only visible text

Posted on 2013-06-25
6
454 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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

The System Center Operations Manager 2012, known as SCOM, is a part of the Microsoft system center product that provides the user with infrastructure monitoring and application performance monitoring. SCOM monitors:   Windows or UNIX/LinuxNetwo…
User Beware!  This is a rather permanent solution to removing your email from an exchange server.  The only way to truly go back is to have your exchange administrator restore your mailbox from backups.  This is usually the option of last resort.  A…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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

18 Experts available now in Live!

Get 1:1 Help Now