Solved

Format change after copy in VBA

Posted on 2015-01-16
10
71 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
  • 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 48

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
 

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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 45

Expert Comment

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

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 45

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now