Outlook VSTO - MS Word - Обычное предупреждение о шаблоне - Word не может сохранить файл, потому что он уже открыт в другом месте (normal.dotm) - PullRequest
0 голосов
/ 18 ноября 2018

Я разрабатываю VSTO в Outlook, который призывает MSWord создать текстовый документ, сохранить его в формате PDF, после чего VSTO выходит из слова.Я использую VS2017 Professional и Office365 - MSWord версии 16.

Если MSWord не открыт - приведенный ниже код работает как угощение.Однако если MSWord открыт, я получаю предупреждение, что «Word не может сохранить файл, потому что он уже открыт в другом месте (normal.dotm)» .Теперь я знаю, что это было вызвано много раз, и я попробовал все решения, которые я могу найти на StackExchange и в других местах.Проблема была ранее решена путем ссылки на ответы в на этот вопрос , но после недавнего обновления Office365, то же самое предупреждение вернулось.

Ниже приведено подмножество кода, который я использую -все имена файлов и т. д. действительны.

                // start word to create PDF file
                Word.Application wordApp = new Word.Application();
                Word.Document wrdDoc = new Word.Document();
                object saveOption = false; 
                //Word.WdSaveOptions.wdDoNotSaveChanges;
                object oMissing = Type.Missing;
                Word.Documents d = wordApp.Documents;

                try
                { 
                    // set the word app invisible
                    wordApp.Visible = false;
                    // open the document with the tmpfilename
                    wrdDoc = d.Open(FileNameIn, Visible: true);
                    //set the page size ...
                    //wrdDoc.PageSetup.PaperSize = Word.WdPaperSize.wdPaperA4;

                }
                catch (COMException es)
                {
                    throw (es);
                }

                try
                {
                    //Save/Export our document to a PDF
                    wrdDoc.ExportAsFixedFormat(OutputFileName: FileNameOut, ExportFormat: Word.WdExportFormat.wdExportFormatPDF,
                    OpenAfterExport: false, OptimizeFor: Word.WdExportOptimizeFor.wdExportOptimizeForPrint,
                    Range: Word.WdExportRange.wdExportAllDocument, From: 0, To: 0, Item: Word.WdExportItem.wdExportDocumentContent,
                    IncludeDocProps: true, KeepIRM: true, CreateBookmarks: Word.WdExportCreateBookmarks.wdExportCreateNoBookmarks,
                    DocStructureTags: true, BitmapMissingFonts: true, UseISO19005_1: false);

                    // trick our Word App into thinking that we have been saved
                    wordApp.NormalTemplate.Saved = true;

                }
                catch (COMException es)
                {
                    // there is an actual error reporting mechanism here
                    throw (es);
                }
                finally
                {
                    try
                    {
                       // make our document think its been saved
                       wrdDoc.Saved = true;
                       // close our document
                       wrdDoc.Close(ref saveOption, ref oMissing, ref oMissing);

                       // trick our Word App into thinking that we have been saved
                       wordApp.NormalTemplate.Saved = true;
                       wordApp.Quit(ref saveOption, ref oMissing, ref oMissing);
                       // release our document object
                       Marshal.ReleaseComObject(wrdDoc);
                       // release our documents object
                       Marshal.ReleaseComObject(d);
                       // release our application object
                       Marshal.ReleaseComObject(wordApp);

                    }
                    catch (Exception es)
                    {
                       // there is an actual error reporting mechanism here
                       throw (es);
                    }
                    finally
                    {
                        //
                    }
                }

Я был бы признателен за любую помощь в решении этой проблемы.Заранее большое спасибо DWE

1 Ответ

0 голосов
/ 27 ноября 2018

Ну, за последние несколько дней я узнал довольно много об ошибке, которую я искал выше.

Что я узнал, так это то, что для правильной отладки проблемы с Outlook и / или MSWord необходимо выгрузить все сторонние надстройки, которые в данный момент работают, а затем попытаться решить проблему. В моем конкретном случае я обнаружил, что в программе под названием Nuance Paperport есть надстройка для преобразования текстовых документов в PDF, которая блокирует шаблон normal.dotm и не соблюдает правила при запросе слова выйти из кода. Хотя для некоторых это может быть тривиально, для меня это был ценный урок.

Код для преобразования текстового документа в PDF приведен ниже и является тем, на чем я остановился на данный момент.

        /// <summary>
    /// A test to see if word is working
    /// this function takes a valid word document and converts it to a PDF 
    /// and takes the user to explorer with the converted file selected
    /// </summary>
    /// <param name="WordDocIn">a valid path\filename to an existing word document (doc, docx, rtf, htm, mhtml</param>
    /// <returns>returns False if there are no errors and the function completed successfully</returns>
    private bool WordToPDF(string WordDocIn)
    {
        // check and make sure our file parameter actually exists
        if(!File.Exists(WordDocIn))
        {
            MessageBox.Show("The file does not exist\n\n" + WordDocIn + "\n\n", "Office Assist: Error()", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return true; // true meaning there are errors
        }
        string tmpFilename = WordDocIn;
        // create a temp filename in temp directory
        string opFileName = GetTempFilePathWithExtension("pdf");
        // set our return value
        bool ErrorsPresent = false; // false meaning there are NO errors

        // create our word app
        Word.Application wordApp = new Word.Application();
        // get our documents collection (if any)
        Word.Documents d = wordApp.Documents;
        // create our new document
        Word.Document wrdDoc = new Word.Document();
        // create our word options
        object saveOption = false;// Word.WdSaveOptions.wdDoNotSaveChanges;
        // create our missing object
        object oMissing = Type.Missing; ;// Type.Missing;

        try
        {
            // set the word app visisble for the moment sp
            // we can see what we're doing
            wordApp.Visible = false;

            // suppress our alerts
            wordApp.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;

            // we dont need any addins - unload all of our addins
            // need to open word with no addins loaded ... 
            wordApp.AddIns.Unload(true);   // True to remove the unloaded add-ins from the AddIns collection 
                                            // (the names are removed from the Templates and Add-ins dialog box). 
                                            // False to leave the unloaded add-ins in the collection.

            // open the document with the tmofilename
            wrdDoc = d.Open(tmpFilename, Visible: true);
            //set the page size ... to A4 (note this will throw an error if the default printer does not produce A4)
            wrdDoc.PageSetup.PaperSize = Word.WdPaperSize.wdPaperA4;

            //
            // export our document to a PDF
            //
            wrdDoc.ExportAsFixedFormat(OutputFileName: opFileName, ExportFormat: Word.WdExportFormat.wdExportFormatPDF,
            OpenAfterExport: false, OptimizeFor: Word.WdExportOptimizeFor.wdExportOptimizeForPrint,
            Range: Word.WdExportRange.wdExportAllDocument, From: 0, To: 0, Item: Word.WdExportItem.wdExportDocumentContent,
            IncludeDocProps: true, KeepIRM: true, CreateBookmarks: Word.WdExportCreateBookmarks.wdExportCreateNoBookmarks,
            DocStructureTags: true, BitmapMissingFonts: true, UseISO19005_1: false);

            // trick our Word App into thinking that the document has been saved
            wrdDoc.Saved = true;
            // close our word document
            wrdDoc.Close(ref saveOption, ref oMissing, ref oMissing);
            // close our documents collection
            //d.Close(ref saveOption, ref oMissing, ref oMissing);

            foreach (Word.Template template in wordApp.Templates)
            {
                switch (template.Name.ToLower())
                {
                    case "normal.dotm":
                        template.Saved = true;
                        break;
                }
            }
            // quit word
            wordApp.Quit(ref saveOption, ref oMissing, ref oMissing);
        }
        // catch our errors
        catch (COMException es)
        {
            string errcode = "ComException : " + es.Message + "\n\n" + "Show this message to your admin";
            MessageBox.Show("Check the default printer has the ability to print A4\nSelect a new printer and try again\n\n" + errcode, "Office Assist: Error()", MessageBoxButtons.OK, MessageBoxIcon.Error);
            ErrorsPresent = true;
            //throw (es);
        }
        catch (Exception es)
        {
            string errcode = "Exception : " + es.Message + " \n\n " + "Show this message to your admin";
            MessageBox.Show(errcode, "Office Assist: Error()", MessageBoxButtons.OK, MessageBoxIcon.Error);
            ErrorsPresent = true;
            //throw (es);
        }
        finally
        {
            try
            {
                // release our objects
                Marshal.ReleaseComObject(wrdDoc);
                Marshal.ReleaseComObject(d);
                Marshal.ReleaseComObject(wordApp);
            }
            catch (Exception es)
            {
                string errcode = "Exception : " + es.Message + " \n\n " + "Show this message to your admin";
                MessageBox.Show(errcode, "Office Assist: Error()", MessageBoxButtons.OK, MessageBoxIcon.Error);
                ErrorsPresent = true;
            }
        }


        // if there are no errors present
        // open explorer and select the pdf created
        //
        if (!ErrorsPresent)
        {
            // set our switches
            string argument = "/select, \"" + opFileName + "\"";
            // open explorer and select the file
            Process.Start("explorer.exe", argument);
        }

        return ErrorsPresent;
    }

Учитывая количество времени, которое я потратил, пытаясь решить эту проблему, и количество сообщений, которые я вижу, которые касаются преобразования текстового документа в PDF и аналогичных проблем или аналогичных проблем с шаблоном нормы, я подумал, что было бы целесообразно прокомментировать на том, что я узнал, и опубликуйте код, который работает для меня, в надежде, что он поможет кому-то еще в будущем.

...