Disappearing text when doing a paste

J

jag

Hello all,

Having a problem coping data from one document to another. I need to
automate the following task. I have a document that my customer opens and
clicks a button that does a copy. When the button is clicked, I need the
following to happen.

The file open dialog box displays so they can pick the file that has data to
be copied from
The selected file is opened
Data from an enclosed bookmark in the newly opened file is copied
The data is then pasted to a bookmark location in the intial document

I have this working, the problem is when I do the past into the initial
document, it deletes all the text in the document and then paste the new
text.
What am I doing wrong?? I needed to be sure the copied data retains all
formatting, like bullet list, etc. My code is listed below.

thanks


Sub Copy_Description()
Dim DescriptionDoc As Document
Dim pTarget As Range

Dialogs(wdDialogFileOpen).Show
Set DescriptionDoc = ActiveDocument

Set pTarget =
ActiveDocument.Bookmarks("BM_TheDescription").Range.FormattedText
pTarget.Copy

If ThisDocument.Bookmarks.Exists("BM_Description") Then
ThisDocument.Bookmarks("BM_Description").Select
ThisDocument.Range.Paste
End If

ActiveDocument.Close

End Sub
 
W

Word Heretic

G'day jag <[email protected]>,

<Grin>

If ThisDocument.Bookmarks.Exists("BM_Description") Then
ThisDocument.Bookmarks("BM_Description").Select

'____________________

ThisDocument.Range.Paste
' _____________________

End If



Look at that line. It is pasting OVER the entire Range of
ThisDocument!

Replace it with

Selection.Paste


Steve Hudson - Word Heretic

steve from wordheretic.com (Email replies require payment)
Without prejudice


jag reckoned:
 
G

G.G.Yagoda

Why not use the bookmark ranges instead of copying and pasting?

Sub CopyUsingRanges ()
Dim TargetDoc As Document
Set TargetDoc = ActiveDocument
Dialogs(wdDialogFileOpen).Show
If ActiveDocument.Bookmarks.Exists("BM_Description") And
TargetDoc.Bookmarks.Exists("BM_Description") Then
TargetDoc.Bookmarks("BM_Description").Range.FormattedText =
ActiveDocument.Bookmarks("BM_Description").Range
ActiveDocument.Close
End If
End Sub
 
J

jag

Thanks, this works.
However there is one problem, it retains the bullet list (if there is one)
but the text comes over as a different font. In this case courier, the
original text was Times New Roman.. Any suggetions??

Thanks
 
G

G.G.Yagoda

It's interesting that the original font is carried over in bulleted
paragraphs. Assuming that the style for the bulleted paragraphs has
the word "Bullet" in it and that the font you want is Times New Roman,
this should provide the cure:

Dim TargetDoc As Document, R As Range, P As Paragraph
Set TargetDoc = ActiveDocument
Dialogs(wdDialogFileOpen).Show
If ActiveDocument.Bookmarks.Exists("BM_Description") And
TargetDoc.Bookmarks.Exists("BM_Description") Then
TargetDoc.Bookmarks("BM_Description").Range.FormattedText =
ActiveDocument.Bookmarks("BM_Description").Range
ActiveDocument.Close
For Each P In
TargetDoc.Bookmarks("BM_Description").Range.Paragraphs
If InStr(P.Style.NameLocal, "Bullet") > 0 Then
P.Range.Font.Name = "Times New Roman"
Next
End If
 
J

jag

The original font is not carried over at all, I was just saying the
bullets do carrie over.
Can I just select the text then change the font???

thanks
 
G

G.G.Yagoda

Because the bulleted list is part of a larger range, I don't see how
you could select it to operate on except by hand. Maybe if you changed
the For Each paragraph loop you could achieve what you want. For
example, to change the bullet-styled paragraphs to Normal:

For Each P In
TargetDoc.Bookmarks("BM_Description").Range.Paragraphs
If InStr(P.Style.NameLocal, "Bullet") > 0 Then
P.Style = "Normal"
Next

Sorry, I did not understand about the fonts.
 

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