1
votes

I have inherited a VB6 app and I could do with some help with part of it.

The code opens a word document and copies its contents. Once this is complete it will open another document and paste the contents from the first document into the second. The opening, copying and pasting works ok, the issue comes with the formatting of the pasted text and the section break it follows. Instead of appearing straight after the section break it is being put on another page, the section break does however still say it is continuous. I've done some digging and tried what it says in the following

Stop Margin Adjustment when pasting - Microsoft Community

Problems with margins when I copy and paste a document into template - Microsoft Community

Section break causes unexpected page break in word

Troubleshoot page breaks and section breaks - Word - Office.com

None of these have helped. A cut down version of the code is as follows:

    GetWord97Object objWordApp

objWordApp.Visible = True
objWordApp.documents.Open strCopyFromDoc

DeleteHeadersAndFooters objWordApp.documents(strCopyFromDoc)

objWordApp.documents(strCompyFromDoc).content.Copy

objWordApp.documents.Open strCopyToDoc
objWordApp.documents(strCopyToDoc).characters(objWordApp.ActiveDocument.characters.Count).Select

Set objRng = objWordApp.ActiveDocument.content   ' Range used so as not to overwrite original text
objRng.Collapse Direction:=0

If IsWordAppVersionLessThan2002(CInt(objWordApp.Version)) Then
    objRng.Paste
Else
    objRng.PasteAndFormat wdPasteDefault
End If

I've tried the paste and format but that hasn't helped. The version of Word I am using is 2002 SP3 but I need it to work with 2002 and up. The VB6 is at SP6.

Thanks in advance for your help.

2

2 Answers

0
votes

I've managed to get rid of the problem. It looks like it was something to do with the document, rather than the code. I've given copying the header and footer from one document to another and that seems to have worked this time. Previous attempts at copying didn't seem to make any difference. Not an ideal solution but at least it is sorted.

0
votes

I've found the solution and It's more simple that I thought. Just save the document before you paste your content. That makes Word to keep the original margins definitions. In my code I did that.

Private Sub CommandButton4_Click()
Dim Item As String
Dim i As Integer
For i = 0 To ProcList.ListCount - 1
Dim docNew As Document
Dim docproc As Document
Set docNew = Word.ActiveDocument
docNew.Content.Copy
Set docproc = Documents.Add

With docproc
        .SaveAs FileName:=ProcList.List(i)
        Selection.ClearParagraphAllFormatting
        Selection.Paste

End With

Next i"