Solved

Copy only visible text

Posted on 2013-06-25
6
462 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

No matter the version of Windows you are using, you may have some problems with Windows Search running too slow or possibly not running at all. Before jumping into how you can solve this issue, just know there are many other viable alternative deskt…
Outlook Free & Paid Tools
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

831 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