[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Copy only visible text

Posted on 2013-06-25
6
Medium Priority
?
503 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

This article describes a serious pitfall that can happen when deleting shapes using VBA.
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

656 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