Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Format change after copy in VBA

Posted on 2015-01-16
10
Medium Priority
?
79 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 53

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
Technology Partners: 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 49

Expert Comment

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

Accepted Solution

by:
Rgonzo1971 earned 2000 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 49

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

Independent Software Vendors: 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!

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

885 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