?
Solved

Copy only visible text

Posted on 2013-06-25
6
Medium Priority
?
496 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

752 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