API:word insertfile lead format changed

gxhllj 1 Reputation point
2022-03-01T03:37:24.13+00:00

We use microsoft.office.interop.word.selection.insertfile api to insert file insertFile.docx into file srcfile.docx bookmark ,but the format changed after insertfile.
the demo code

const string BOOK_MARK_NAME = @"TESTBOOKMARK";  
        public static void insertFileToBookMark(string srcfile, string insertFile)  
        {  
            object fileName = srcfile;  
            ApplicationClass o = new ApplicationClass  
            {  
                Options = {  
                        CheckSpellingAsYouType = false,  
                        CheckGrammarAsYouType = false,  
                        SuggestFromMainDictionaryOnly = false,  
                        CheckGrammarWithSpelling = false,  
                        ShowReadabilityStatistics = false,  
                        SuggestSpellingCorrections = false,  
                         AutoFormatAsYouTypeDefineStyles = true,  
                          AutoFormatPreserveStyles=true,  
                          PasteFormatWithinDocument= WdPasteOptions.wdMatchDestinationFormatting  
  
                    }  
            };  
            object confirmConversions = Missing.Value;  
            object readOnly = Missing.Value;  
            object addToRecentFiles = Missing.Value;  
            object passwordDocument = Missing.Value;  
            object passwordTemplate = Missing.Value;  
            object revert = Missing.Value;  
            object writePasswordDocument = Missing.Value;  
            object writePasswordTemplate = Missing.Value;  
            object format = Missing.Value;  
            object encoding = Missing.Value;  
            object visible = Missing.Value;  
            object openAndRepair = Missing.Value;  
            object documentDirection = Missing.Value;  
            object noEncodingDialog = Missing.Value;  
            object xMLTransform = Missing.Value;  
            Document mDoc = o.Documents.Open(ref fileName, ref confirmConversions, ref readOnly, ref addToRecentFiles, ref passwordDocument, ref passwordTemplate, ref revert, ref writePasswordDocument, ref writePasswordTemplate, ref format, ref encoding, ref visible, ref openAndRepair, ref documentDirection, ref noEncodingDialog, ref xMLTransform);  
            mDoc.Application.Visible = false;  
            bool flag = false;  
  
  
            object What = WdGoToItem.wdGoToBookmark;  
            object Name = BOOK_MARK_NAME;  
            object Which = Missing.Value;  
            mDoc.ActiveWindow.Selection.GoTo(ref What, ref Which, ref Which, ref Name);  
            mDoc.Bookmarks.ShowHidden = false;  
            object Type = WdBreakType.wdSectionBreakOddPage;  
            object ConfirmConversions = false;  
            object Link = false;  
            object Attachment = false;  
            try  
            {  
                mDoc.GrammarChecked = false;  
                mDoc.ShowGrammaticalErrors = false;  
                mDoc.ShowSpellingErrors = false;  
                //mDoc.ActiveWindow.Selection.PasteAndFormat(WdRecoveryType.wdUseDestinationStylesRecovery);  
                mDoc.ActiveWindow.Selection.InsertFile(insertFile, ref Which, ref ConfirmConversions, ref Link, ref Attachment);  
                //mDoc.ActiveWindow.Selection.Paste();  
  
            }  
            catch  
            { }  
            finally  
            {  
                //inSert.Close();  
                mDoc.Close();  
            }  
        }  

We discover that when we use office word software to copy the manually,it will tips “记录剪贴板样式表要求复制多种样式。是否改用正文样式?”

178702-%E6%8D%95%E8%8E%B7.png

How can I upload the docx testfile???? I only upload the pdf file that convert form the docx file.178655-insertfile.pdf178656-srcfile.pdf

{count} votes