Example #1
0
        /// <summary>
        /// Reloads the associated Office document, but keeping the OfficeParts currently shown in the GUI. This
        /// ensures that, if the files have been modified externally, the program is still looking at their latest
        /// version. Otherwise, we might accidentally lose those external changes when saving
        /// </summary>
        public void Reload()
        {
            // Store the file name (otherwise, it will have been erased after calling Dispose)
            var fileName = Document.Name;

            // Dispose current document (not needed as references to its parts are stored in their View models anyway)
            Document.Dispose();

            // Then, reload it
            Document = new OfficeDocument(fileName);

            // Delete all its original parts
            foreach (var type in Enum.GetValues(typeof(XmlPart)).OfType <XmlPart>())
            {
                Document.RemoveCustomPart(type);
            }

            // Instead, use the parts currently shown in the editor
            foreach (var part in Children.OfType <OfficePartViewModel>())
            {
                if (part.Part == null)
                {
                    continue;
                }

                Document.SaveCustomPart(part.Part.PartType, part.OriginalContents, true);

                // Re-map the Part. This ensures that the PackagePart stored internally in OfficePart points to
                // the right location, in case it is needed
                part.Reload();
            }
        }