Solved

Format change after copy in VBA

Posted on 2015-01-16
10
73 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 49

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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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 46

Expert Comment

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

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 46

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…

778 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