Copy bookmark content and format from one doc to another without fields

F

Flemming

Hi, all

I have some content in a bookmark in document A - this content contains
ContentControls (office 2007), Fields with reference to other content and
other Bookmarks...!

From document B I open document A to collect the content and formats from
into document B. And that is easy done, BUT... the copy includes all
ContentControls, Fields, ref. fields and bookmarks. Because of this I loose
data or maybe get wrong data inserted. I can copy as plain text, but then I
loose formats of special characters like the degree character.

Do you know how to copy content (text only) and the correct format from one
document to another without getting contentcontrols and fields as well?

Thanks alot
Flemming
 
G

Graham Mayor

If I understand correctly you have a bookmarked section in one document that
you want to insert into another c/w the text entered in 2007 content
controls and the results of fields as text, whilst maintaining special
characters such as 'degrees'? The following should copy the content of a
bookmarked section (bBookmark) from the SourceDoc to a bookmark (bTarget) in
the TargetDoc. Change the names as required

Sub CopyBMarked()
Dim sRng As Range
Dim tRng As Range
Dim SourceDoc As Document
Dim TargetDoc As Document
Set SourceDoc = Documents.Open("D:\My Documents\Test\Versions\Test1.docx")
Set TargetDoc = Documents.Open("D:\My Documents\Test\Versions\Test2.docx")
Set sRng = SourceDoc.Bookmarks("bBookmark").Range
Set tRng = TargetDoc.Bookmarks("bTarget").Range
tRng.Text = sRng
TargetDoc.Bookmarks.Add "bTarget", tRng
End Sub

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
F

Flemming

Hi Graham

I do something very close to solution, but both your way and mine does not
copy the right formats. When I write 'degrees' I mean the little o in
supertext after a number or before C (Celcius) - this just comes as plain
text.

Any other ideas?
Flemming
 
F

Flemming

Maybe the shown method is the only way.. so I did some looking into the
posibility of running through each character in the SourceDoc bookmark to
check for Superscript or Subscript characters, to make the same format count
on the TargetDoc. However this is a bit funny..!

I've made a test document containing only 1d345f789 where "d" is within
a contentcontrol and "f" is within a formfield.

A test macro
Sub Test()
Dim lCount As Long
Debug.Print "***"
For lCount = 1 To
ActiveDocument.Bookmarks("bmTest").Range.Characters.Count
Debug.Print
ActiveDocument.Bookmarks("bmTest").Range.Characters(lCount)
Next
Debug.Print "***"
End Sub

Results:
***
1

d

3
4
5
f
7
8
9
***


The "funny" part is that a content control uses 2 characters more then what
are shown in the document.
If I take a ASC function on one of those extra "spaces" ASC returns with an
error.

What is that "space" / content control placeholder and how do I handle it
when running through the characters like this

Sub Testing()
Dim oChr As Object
For Each oChr In ActiveDocument.Bookmarks("bmTest").Range.Characters
if oChr ...
Next
End Sub

or like this

Sub Testing()
Dim lCount As Long
For lCount = 1 To
ActiveDocument.Bookmarks("bmTest").Range.Characters.Count
...
Next
End Sub


Anyone?

Thanks alot,
Flemming
 
G

Graham Mayor

OK - but if you use the degree font character ALT+0176 then that is
retained. If you have used superscript 'o' as a degree sign it can easily be
substituted in the macro

Dim sRng As Range
Dim tRng As Range
Dim SourceDoc As Document
Dim TargetDoc As Document
Set SourceDoc = Documents.Open("D:\My Documents\Test\Versions\Test1.docx")
Set TargetDoc = Documents.Open("D:\My Documents\Test\Versions\Test2.docx")
Set sRng = SourceDoc.Bookmarks("bBookmark").Range
With sRng.Find
.ClearFormatting
.Replacement.ClearFormatting
'**********************
.Text = "o"
.Font.Superscript = True
.Replacement.Text = Chr(176)
.Replacement.Font.Superscript = False
'**********************
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
.Execute Replace:=wdReplaceAll
End With
Set tRng = TargetDoc.Bookmarks("bTarget").Range
tRng.Text = sRng
TargetDoc.Bookmarks.Add "bTarget", tRng


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
F

Flemming

Hi Graham

How about the "o"'s in other words - they will be replaced as well with your
function.

I could use ALT+0176 for the degree sign in the SourceDoc, but I have
numbers and letters as sub text as well, so I need to come up with another
solution.

If you read my post from yesterday 21:09 - you'll see I'm trying to loop my
way through it, and tomorrow I will test my loop by handling error 5 since
it seems link that's the error occcuring when ever it meet a contentcontrol.

Cheers,
Flemming
 
G

Graham Mayor

Only superscripted 'o's are changed by the macro.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
F

Flemming

Right you are only superscripted 'o's are changed by the macro - this only
change a single part of the issue.

Thanks,
Flemming
 
F

Flemming

Hi all

'Some functions to move bookmark contents
ContentToBookmark docTarget, "bmTarget", ContentFromBookmark(docSource,
"bmSource")

Dim oChr As Object
Dim lChr As Long: lChr = 0
For Each oChr In docSource.Bookmarks("bmSource").Range.Characters
If Not oChr = ""
lChr = lChr + 1
If oChr.Font.Superscript Then
docTarget.Bookmarks("bmTarget").Range.Characters(lChr).Font.Superscript
= True
End If
If oChr.Font.Subscript Then
docTarget.Bookmarks("bmTarget").Range.Characters(lChr).Font.Subscript
= True
End If
End If
Next oChr


The issue about the ContentControl having two invisible characters that
cannot be converted to ASC.. are solved with this if sentence: If Not
oChr = ""


Cheers,
Flemming
 
G

Graham Mayor

maybe a bit late now but I was thinking of

Sub CopyBMarked()
Dim sRng As Range
Dim tRng As Range
Dim SourceDoc As Document
Dim TargetDoc As Document
Set SourceDoc = Documents.Open("D:\My Documents\Test\Versions\Test1.docx")
Set TargetDoc = Documents.Open("D:\My Documents\Test\Versions\Test2.docx")
Set sRng = SourceDoc.Bookmarks("bBookmark").Range
Set tRng = TargetDoc.Bookmarks("bTarget").Range
sRng.Copy
tRng.Paste
TargetDoc.Bookmarks.Add "bTarget", tRng
tRng.Fields.Unlink
For i = tRng.ContentControls.Count To 1 Step -1
tRng.ContentControls(i).Delete
Next i
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top