Menu

#37 Word docs with Master/Slave

v1.7
open
None
5
2016-12-21
2016-12-21
ISACSRL
No

Hi, I found a bug in compiling Word document with Master/Slave architecture.
I solved the bug in msword.cs, method SaveWord;
Fixed version is:

    public bool SaveWord(string wordFileSrc, string htmlFileDst, object format)
    {
        Document aDoc = null;
        object missing = System.Reflection.Missing.Value;
        object saveChanges = false;
        int currentWaitTime = 0;

        try
        {
            object fileName = wordFileSrc;
            object readOnly = true;
            object isVisible = false;
            //object openandrepair;

            // Open the document that was chosen by the dialog
            try
            {
                aDoc = wordApp.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
            }
            catch (System.Exception e)
            {
                //second attempt: open in R/W. This seems to be mandatory with MSWord opening Master/slaves documents
                readOnly = false;
                aDoc = wordApp.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
            }
            if (wordApp.ActiveDocument.Subdocuments.Count > 0)
            {
                wordApp.ActiveWindow.ActivePane.View.Type = WdViewType.wdOutlineView;
                wordApp.ActiveDocument.Subdocuments.Expanded = true;
                wordApp.ActiveWindow.ActivePane.View.Type = WdViewType.wdNormalView;
                string local_dst = htmlFileDst as string;
                int pos = local_dst.LastIndexOf('\\');
                string path = local_dst.Substring (0, pos);
                path += "\\temp.docx";
                object obj_dst = path, obj_format = WdSaveFormat.wdFormatDocumentDefault;
                wordApp.Selection.WholeStory();
                wordApp.Selection.Copy();
                wordApp.Selection.PasteAndFormat(WdRecoveryType.wdFormatOriginalFormatting);
                aDoc.SaveAs(ref obj_dst, ref obj_format, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

            }
            // Unlink images:
            UnlinkFields(aDoc);

            // Save document as filtered html:
            object dst = htmlFileDst;
            aDoc.SaveAs(ref dst, ref format, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
        }
        catch (System.Exception e)
        {
            System.Diagnostics.Debug.Print(e.Message);
        }
        finally
        {
            if (aDoc != null)
            {
                // Close the document:
                ((_Document)aDoc).Close(ref saveChanges, ref missing, ref missing);

                // Be sure the document is closes (paranoic check to avoid bug with large files)
                if (AppSettings.WaitForWordCloseDocs)
                {
                    System.Windows.Forms.Application.DoEvents();
                    const int waitTimeStep = 100;
                    Thread.Sleep(waitTimeStep);
                    currentWaitTime += waitTimeStep;
                    while (IsOpen(wordFileSrc) && currentWaitTime < MAXWAITOPENDOCUMENT)
                    {
                        System.Windows.Forms.Application.DoEvents();
                        Thread.Sleep(waitTimeStep);
                        currentWaitTime += waitTimeStep;
                    }
                }
            }
        }

        // There was a timeout?
        return currentWaitTime < MAXWAITOPENDOCUMENT;
    }

Discussion


Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.