Solved

C# MS Word Interop - Losing Formatting when pasting

Posted on 2014-03-27
9
1,466 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…

914 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

17 Experts available now in Live!

Get 1:1 Help Now