Solved

C# MS Word Interop - Losing Formatting when pasting

Posted on 2014-03-27
9
1,434 Views
Last Modified: 2014-04-01
I have an application that opens a word document and copies it to a another word document.  The issue I am having is the original word document contains line drawings instead of tables with borders.  When the document is pasted those drawings move half way down the page instead of on top where its suppose to be.

//Copy
try
{
    wApp.Selection.WholeStory();
    wApp.Selection.Copy();
}
catch (Exception ex)
{
    MessageBox.Show("Error on Copy" + ex.Message);
    return -2;
}

//Paste
try
{
    masterDoc.Activate();
    masterDoc.Select();
    masterDoc.Application.Selection.NoProofing = 1; 
    masterDoc.Application.Selection.MoveEnd(ref missing, ref missing);
    masterDoc.Application.Selection.MoveRight(ref missing, ref missing, ref missing);   
    masterDoc.Application.Options.SuggestSpellingCorrections = false;
    masterDoc.Application.Options.CheckGrammarAsYouType = false;
    masterDoc.Application.Options.CheckSpellingAsYouType = false;
    masterDoc.Application.Options.CheckGrammarWithSpelling = false;
    masterDoc.Application.DisplayAlerts = 
          Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone;

    wApp.Selection.Paste();
}
catch (Exception ex)
{
    MessageBox.Show("Error on Reactivate paste" + ex.Message);
    return -2;
}

Open in new window


Any Ideas?
0
Comment
Question by:CipherIS
  • 4
  • 3
  • 2
9 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39959365
Instead of Paste there is a PasteAndFormat - you could try using that instead.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39959368
We would need to know what mechanism is used to ensure that the shapes appear at the top of the page. Is there any chance of posting the document (redacted if necessary)?
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39959370
0
 
LVL 1

Author Comment

by:CipherIS
ID: 39959733
Tried this

 wApp.Selection.PasteAndFormat(
               Microsoft.Office.Interop.Word.WdRecoveryType.wdFormatOriginalFormatting);

Still not working

 try
{
    masterDoc.Activate();

    masterDoc.Select();
    masterDoc.Application.Selection.NoProofing = 1;  
    masterDoc.Application.Selection.MoveEnd(ref missing, ref missing);
    masterDoc.Application.Selection.MoveRight(ref missing, ref missing, ref missing);  
    masterDoc.Application.Options.SuggestSpellingCorrections = false;
    masterDoc.Application.Options.CheckGrammarAsYouType = false;
    masterDoc.Application.Options.CheckSpellingAsYouType = false;
    masterDoc.Application.Options.CheckGrammarWithSpelling = false;
    masterDoc.Application.DisplayAlerts = 
            Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone;

    //wApp.Selection.Paste();
    wApp.Selection.PasteAndFormat(
               Microsoft.Office.Interop.Word.WdRecoveryType.wdFormatOriginalFormatting);
 }

Open in new window

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39959871
There are many factors that could be in play here, so do consider posting a document, either the one in question redacted for confidential data, or a similar one created for this purpose,
0
 
LVL 1

Author Comment

by:CipherIS
ID: 39960020
I've attache copies of the From document (Word 01) and the To document (Word 02).  I had to redact data but I think its enough to show difference.
Word01.jpg
Word02.jpg
0
 
LVL 1

Accepted Solution

by:
CipherIS earned 0 total points
ID: 39960192
Thanks for all your help.  I figured out how to fix it.  The original document was in rtf format.  So I created the new document in rtf format then performed the copy and paste.  Working now.

Thanks again
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39960406
Even with formatting marks showing, pictures of the documents would not have been enough to work out how the layout is achieved. We would have needed an actual document.
If some or all of the layout is implemented with drawing objects, then it is very likely to be somewhat delicate, as you have found out.
0
 
LVL 1

Author Closing Comment

by:CipherIS
ID: 39968814
Thanks for all your help.  I figured out how to fix it.  The original document was in rtf format.  So I created the new document in rtf format then performed the copy and paste.  Working now.

Thanks again
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.

706 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

22 Experts available now in Live!

Get 1:1 Help Now