Solved

C# MS Word Interop - Losing Formatting when pasting

Posted on 2014-03-27
9
1,595 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
[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
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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
 
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

Industry Leaders: 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

Suggested Solutions

Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

734 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