Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Format change after copy in VBA

Posted on 2015-01-16
10
Medium Priority
?
78 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 52

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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 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…
Suggested Courses

715 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