Solved

Format change after copy in VBA

Posted on 2015-01-16
10
77 Views
Last Modified: 2015-01-16
I created a vba script to split up a document into multiple files at the section break after a mail merge.
Everything works fine, except it inserts weird formatting.  Some text becomes hyper text or under lined.
I stepped through the code, and found that it happens when I paste the selected text into a new document, but do not know how to prevent it.
Please advise.
Cancel
Top Expertssplitcode.txt
0
Comment
Question by:yballan
[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
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 14

Expert Comment

by:ThomasMcA2
ID: 40553362
Use Paste Special to paste just the text:

Selection.PasteSpecial Paste:=xlPasteValues

Open in new window

0
 
LVL 52

Expert Comment

by:Rgonzo1971
ID: 40553389
HI,

pls try

Selection.PasteSpecial Link:=False, DataType:=20, Placement:=wdInLine, _
        DisplayAsIcon:=False

'Or
Selection.PasteAndFormat (wdFormatPlainText)¨

Regards
0
 

Author Comment

by:yballan
ID: 40553444
Dear ThomasMcA2 and Rgonzo1971,

Thank you for your prompt responses, unfortunately, I cannot strip off all formats.
Please view my before and after documents.  I have some formats already in.  I just don't want extra stuff inserted.
Before.doc
After.doc
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:yballan
ID: 40553449
Sorry, Wrong Before.doc, here it the original, with the format I would like to retain.
0
 

Author Comment

by:yballan
ID: 40553450
Before Merge
Merge-Split.docx
0
 
LVL 48

Expert Comment

by:Martin Liss
ID: 40553590
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
0
 
LVL 52

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 40553676
Hi,
The problem are the styles instead of copying it you could

saveas the file first and delete the section not needed and reopen the file to continue

Sub BreakOnSection()
    Dim MyPath
    MyPath = ActiveDocument.Path

    'Used to set criteria for moving through the document by section.
    Application.Browser.Target = wdBrowseSection
    strFullName = ActiveDocument.FullName
    'A mailmerge document ends with a section break next page.
    'Subtracting one from the section count stop error message.
    For Idx1 = ActiveDocument.Sections.Count - 1 To 1 Step -1

        ActiveDocument.SaveAs FileName:="Agreement_" & Idx1 & ".doc"
        For Idx2 = ActiveDocument.Sections.Count - 1 To 1 Step -1
            If Idx2 <> Idx1 Then
                ActiveDocument.Sections(Idx2).Range.Delete
            End If
        Next
        ChangeFileOpenDirectory MyPath
        ActiveDocument.Close
        Documents.Open strFullName
    Next Idx1
    ActiveDocument.Close savechanges:=wdDoNotSaveChanges
End Sub

Open in new window

Regards
0
 

Author Comment

by:yballan
ID: 40553693
Dear Martin Liss,
Thank you for your reply, why am I getting compile error on "Paste:=" ?
The error message says "Named argument not found", but from the MSDN, the syntax is correct.  Please help.
CompileError.jpg
0
 

Author Closing Comment

by:yballan
ID: 40553723
You are absolutely right!   Thank you for solving this problem!!
0
 
LVL 48

Expert Comment

by:Martin Liss
ID: 40553724
Sorry but I didn't notice that you were doing this in Word and Word VBA apparently is different then Excel VBA, so I don't think I can help
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month5 days, 19 hours left to enroll

626 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