Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1781
  • Last Modified:

C# MS Word Interop - Losing Formatting when pasting

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
CipherIS
Asked:
CipherIS
  • 4
  • 3
  • 2
1 Solution
 
AndyAinscowCommented:
Instead of Paste there is a PasteAndFormat - you could try using that instead.
0
 
GrahamSkanCommented:
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
Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

 
CipherISAuthor Commented:
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
 
GrahamSkanCommented:
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
 
CipherISAuthor Commented:
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
 
CipherISAuthor Commented:
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
 
GrahamSkanCommented:
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
 
CipherISAuthor Commented:
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now