Copying the contents of a document.

M

Malik Al-Amin

Hi all,

Here's my situation. I'm trying to copy the contents of one document into
another document. My problem is that I keep copying over the contents that
are already there. I don't want to copy over the paragraphs that are already
there, instead I want to add a new paragraph. This is the code I have so
far.

Dim TraceTree As Document
Dim UseCase As Document
Dim CaseTraceMerge As Document
Dim para As Paragraph
Dim strParaToFind As String
Dim pTraceToCopy As String
Dim sMyPara As String

' open the titles document in the background
Set TraceTree = Application.Documents.Open(FileName:="C:\TraceTree.doc",
Visible:=False)

' open the info doc in the background
Set UseCase = Application.Documents.Open(FileName:="C:\UseCase.doc",
Visible:=False)

Documents.Add.SaveAs FileName:="C:\Documents and
Settings\Malik\Desktop\fastsave.doc"
Set CaseTraceMerge = Documents("fastsave.doc")
iparacount = UseCase.Paragraphs.Count
For j = 1 To iparacount
sMyPara = UseCase.Paragraphs(j).Range.Text
For Each para In TraceTree.Paragraphs
If para.Range.Text <> sMyPara Then
pTraceToCopy = para.Range.Text
With CaseTraceMerge


.Range.Text = ptracecopy
.Range.ParagraphFormat.Alignment = wdAlignParagraphRight
.Range.InsertParagraphAfter
.Range.Collapse Direction:=wdCollapseEnd

End With
End If
Next
Next

I'm trying to open up a use case document and beginning with the first title
i want to open up the trace tree and find the matching title. Once found, I
want to copy the title and description (from the tracetree.doc) to a third
document. Then I want to loop to the next paragraph in the UseCase.doc and
repeat the process.
This is my first time trying to do any type of word programming so any
advice would be greatly appreciated.

Thanks
 
W

Word Heretic

G'day "Malik Al-Amin" <[email protected]>,

Collapse the paste range to its end and then insert.


Steve Hudson - Word Heretic

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


Malik Al-Amin reckoned:
 
M

Malik Al-Amin

I've tried modifying the code to the following:
Sub Combine()
Dim TraceTree As Document
Dim UseCase As Document
Dim CaseTraceMerge As Document
Dim para As Paragraph
Dim strParaToFind As String
Dim pTraceToCopy As String
Dim sMyPara As String

' open the titles document in the background
Set TraceTree = Application.Documents.Open(FileName:="C:\Documents and
Settings\A021871\Desktop\TraceTree.doc", Visible:=False)

' open the info doc in the background
Set UseCase = Application.Documents.Open(FileName:="C:\Documents and
Settings\A021871\Desktop\UseCase.doc", Visible:=False)

Documents.Add.SaveAs FileName:="C:\Documents and
Settings\A021871\Desktop\fastsave.doc"
Set CaseTraceMerge = Documents("fastsave.doc")
'ActiveDocument.SaveAs FileName:="UseCaseTrace"

iparacount = UseCase.Paragraphs.Count
For j = 1 To iparacount
sMyPara = UseCase.Paragraphs(j).Range.Text
For Each para In TraceTree.Paragraphs
If para.Range.Text <> sMyPara Then
pTraceToCopy = para.Range.Text
With CaseTraceMerge


.Range.ParagraphFormat.Alignment = wdAlignParagraphRight
.Range.Collapse Direction:=wdCollapseEnd
.Range.InsertParagraphAfter
.Range.Text = pTraceToCopy

End With
End If
Next
Next
End Sub


I'm collapsing the range before the insert yet I'm still writing over the
contents already there. What am I doing wrong?

Malik
 
J

Jay Freedman

Hi Malik,

The .Range property of a document (in your case, CaseTraceMerge.Range) by
definition refers to the entire range of the document, and it can't be
collapsed. You need to declare a separate Range object, assign the document
range to it, and then use that range object to collapse and insert text.
Here's the minimum modification of your code that will work:

Dim myRange As Range
...

Set myRange = CaseTraceMerge.Range
With myRange

.ParagraphFormat.Alignment = wdAlignParagraphRight
.Collapse Direction:=wdCollapseEnd
.InsertParagraphAfter
.Text = pTraceToCopy

End With
 
M

Malik Al-Amin

Thank you very much! So far everything is working beautifully.
Unfortunately for me the requirements have now changed but this has still
been an invaluable learning experience. If I run into any other problems
I'll post back. On a closing note, I want to thank all of the contributors
to these forums. On countless occasions I see people coming to these forums
and getting answers to real life business problems. I extend a big thanks to
all!
 

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