Beispiel #1
0
        private static void RemoveVSTOCustomization()
        {
            //<Snippet2>
            string documentPath = System.Environment.GetFolderPath(
                Environment.SpecialFolder.Desktop) + @"\WordDocument1.docx";
            int runtimeVersion = 0;

            try
            {
                runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath);

                if (runtimeVersion == 3)
                {
                    ServerDocument.RemoveCustomization(documentPath);
                    System.Windows.Forms.MessageBox.Show("The customization has been removed.");
                }
            }
            catch (FileNotFoundException)
            {
                System.Windows.Forms.MessageBox.Show("The specified document does not exist.");
            }
            catch (IOException)
            {
                System.Windows.Forms.MessageBox.Show("The specified document is read-only.");
            }
            catch (InvalidOperationException ex)
            {
                System.Windows.Forms.MessageBox.Show("The customization could not be removed.\n" +
                                                     ex.Message);
            }
            //</Snippet2>
        }
Beispiel #2
0
        //</Snippet9>


        //<Snippet10>
        private void ReadCachedStringValue(string documentPath)
        {
            int            runtimeVersion  = 0;
            ServerDocument serverDocument1 = null;

            try
            {
                runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath);

                if (runtimeVersion != 3)
                {
                    MessageBox.Show("This document does not have a Visual Studio Tools for " +
                                    "Office customization, or it has a customization that was created with " +
                                    "a version of the runtime that is incompatible with this version of the " +
                                    "ServerDocument class.");
                    return;
                }

                if (ServerDocument.IsCacheEnabled(documentPath))
                {
                    serverDocument1 = new ServerDocument(documentPath);
                    CachedDataHostItem hostItem1 =
                        serverDocument1.CachedData.HostItems["ExcelWorkbook1.Sheet1"];
                    CachedDataItem dataItem1 = hostItem1.CachedData["CachedString"];

                    if (dataItem1 != null &&
                        Type.GetType(dataItem1.DataType) == typeof(string))
                    {
                        using (System.IO.StringReader stringReader =
                                   new System.IO.StringReader(dataItem1.Xml))
                        {
                            System.Xml.Serialization.XmlSerializer serializer =
                                new System.Xml.Serialization.XmlSerializer(typeof(string));
                            string cachedString = serializer.Deserialize(stringReader) as string;
                            MessageBox.Show("The value of CachedString is: " + cachedString);
                        }
                    }
                }
                else
                {
                    MessageBox.Show("The specified document does not have cached data.");
                }
            }
            catch (System.IO.FileNotFoundException)
            {
                System.Windows.Forms.MessageBox.Show("The specified document does not exist.");
            }
            catch (UnknownCustomizationFileException)
            {
                System.Windows.Forms.MessageBox.Show("The specified document has a file " +
                                                     "extension that is not supported by Visual Studio Tools for Office.");
            }
            finally
            {
                if (serverDocument1 != null)
                {
                    serverDocument1.Close();
                }
            }
        }
Beispiel #3
0
        //<Snippet1>
        private void CreateServerDocumentFromPath(string documentPath)
        {
            int            runtimeVersion  = 0;
            ServerDocument serverDocument1 = null;

            try
            {
                runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath);
                if (runtimeVersion == 3)
                {
                    serverDocument1 = new ServerDocument(documentPath);
                    MessageBox.Show("The URL of the deployment manifest is: \n" +
                                    serverDocument1.DeploymentManifestUrl.ToString());
                }
            }
            catch (System.IO.FileNotFoundException)
            {
                System.Windows.Forms.MessageBox.Show("The specified document does not exist.");
            }
            catch (UnknownCustomizationFileException)
            {
                System.Windows.Forms.MessageBox.Show("The specified document has a file " +
                                                     "extension that is not supported by Visual Studio Tools for Office.");
            }
            finally
            {
                if (serverDocument1 != null)
                {
                    serverDocument1.Close();
                }
            }
        }
Beispiel #4
0
        //</Snippet8>


        //<Snippet9>
        private void ModifyCachedString(string documentPath)
        {
            int            runtimeVersion  = 0;
            ServerDocument serverDocument1 = null;

            try
            {
                runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath);

                if (runtimeVersion != 3)
                {
                    MessageBox.Show("This document does not have a Visual Studio Tools for " +
                                    "Office customization, or it has a customization that was created with " +
                                    "a version of the runtime that is incompatible with this version of the " +
                                    "ServerDocument class.");
                    return;
                }

                if (ServerDocument.IsCacheEnabled(documentPath))
                {
                    //<Snippet11>
                    //<Snippet12>
                    serverDocument1 = new ServerDocument(documentPath);
                    CachedDataHostItem hostItem1 =
                        serverDocument1.CachedData.HostItems["ExcelWorkbook1.Sheet1"];
                    CachedDataItem dataItem1 = hostItem1.CachedData["CachedString"];
                    //</Snippet12>

                    if (dataItem1 != null &&
                        Type.GetType(dataItem1.DataType) == typeof(string))
                    {
                        dataItem1.SerializeDataInstance("This is the new cached string value.");
                        serverDocument1.Save();
                    }
                    //</Snippet11>
                }
                else
                {
                    MessageBox.Show("The specified document does not have cached data.");
                }
            }
            catch (System.IO.FileNotFoundException)
            {
                System.Windows.Forms.MessageBox.Show("The specified document does not exist.");
            }
            catch (UnknownCustomizationFileException)
            {
                System.Windows.Forms.MessageBox.Show("The specified document has a file " +
                                                     "extension that is not supported by Visual Studio Tools for Office.");
            }
            finally
            {
                if (serverDocument1 != null)
                {
                    serverDocument1.Close();
                }
            }
        }
Beispiel #5
0
        //</Snippet1>

        // Note: This works, but should come up with something a
        // little more useful.
        //<Snippet2>
        private void CreateServerDocumentFromByteArray(string documentPath)
        {
            int            runtimeVersion  = 0;
            ServerDocument serverDocument1 = null;

            System.IO.FileStream stream = null;

            try
            {
                runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath);
                if (runtimeVersion == 3)
                {
                    // Read the file into a byte array.
                    stream = new System.IO.FileStream(
                        documentPath, System.IO.FileMode.Open,
                        System.IO.FileAccess.Read);
                    byte[] buffer = new byte[(int)stream.Length];
                    stream.Read(buffer, 0, (int)buffer.Length);

                    // Display the number of bytes in the document.
                    serverDocument1 = new ServerDocument(buffer,
                                                         "*.xlsx");
                    MessageBox.Show("The Document property contains " +
                                    serverDocument1.Document.Length.ToString() +
                                    " bytes.");
                }
            }
            catch (System.IO.FileNotFoundException)
            {
                System.Windows.Forms.MessageBox.Show("The specified document does not exist.");
            }
            catch (UnknownCustomizationFileException)
            {
                System.Windows.Forms.MessageBox.Show("The specified document has a file " +
                                                     "extension that is not supported by Visual Studio Tools for Office.");
            }
            finally
            {
                if (serverDocument1 != null)
                {
                    serverDocument1.Close();
                }
                if (stream != null)
                {
                    stream.Close();
                }
            }
        }
Beispiel #6
0
        //</Snippet7>


        //<Snippet8>
        private static void RemoveAssembly(string documentPath)
        {
            int runtimeVersion = 0;

            try
            {
                runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath);

                // Make sure that this customization was created using the correct runtime.
                if (runtimeVersion != 3)
                {
                    MessageBox.Show("This document does not have a Visual Studio Tools for " +
                                    "Office customization, or it has a customization that was created with " +
                                    "a version of the runtime that is incompatible with this version of the " +
                                    "ServerDocument class.");
                    return;
                }

                ServerDocument.RemoveCustomization(documentPath);
                MessageBox.Show("The customization has been removed.");
            }
            catch (System.IO.FileNotFoundException)
            {
                System.Windows.Forms.MessageBox.Show("The specified document does not exist.");
            }
            catch (System.IO.IOException)
            {
                System.Windows.Forms.MessageBox.Show("The specified document is read-only.");
            }
            catch (UnknownCustomizationFileException)
            {
                System.Windows.Forms.MessageBox.Show("The specified document has a file " +
                                                     "extension that is not supported by Visual Studio Tools for Office.");
            }
            catch (InvalidOperationException ex)
            {
                System.Windows.Forms.MessageBox.Show("The customization could not be removed.\n" +
                                                     ex.Message);
            }
        }
Beispiel #7
0
        private static void AddVSTOCustomization()
        {
            //<Snippet3>
            string documentPath = System.Environment.GetFolderPath(
                Environment.SpecialFolder.Desktop) + @"\WordDocument1.docx";
            int runtimeVersion = 0;

            try
            {
                runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath);

                // Make sure that this document does not yet have any Visual Studio Tools
                // for Office customizations.
                if (runtimeVersion == 0)
                {
                    string deployManifestPath = System.Environment.GetFolderPath(
                        Environment.SpecialFolder.Desktop) + @"\Publish\WordDocument1.vsto";

                    Uri deploymentManifestUri = new Uri(deployManifestPath);
                    ServerDocument.AddCustomization(documentPath, deploymentManifestUri);
                    System.Windows.Forms.MessageBox.Show("The document was successfully customized.");
                }
                else
                {
                    System.Windows.Forms.MessageBox.Show("The document is already customized.");
                }
            }
            catch (FileNotFoundException)
            {
                System.Windows.Forms.MessageBox.Show("The specified document does not exist.");
            }
            catch (DocumentNotCustomizedException ex)
            {
                System.Windows.Forms.MessageBox.Show("The document could not be customized.\n" +
                                                     ex.Message);
            }
            //</Snippet3>
        }
Beispiel #8
0
        //</Snippet6>


        //<Snippet7>
        private void AddCustomizationUsingAssemblyPath(string documentPath, string assemblyName,
                                                       Guid solutionID, string deployManifestPath)
        {
            int runtimeVersion = 0;

            try
            {
                runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath);

                // Make sure that this document does not yet have any Visual Studio Tools
                // for Office customizations.
                if (runtimeVersion == 0)
                {
                    Uri deploymentManifestUri = new Uri(deployManifestPath);
                    ServerDocument.AddCustomization(documentPath, assemblyName, solutionID, deploymentManifestUri);
                    MessageBox.Show("The document was successfully customized.");
                }
                else
                {
                    System.Windows.Forms.MessageBox.Show("The document is already customized.");
                }
            }
            catch (System.IO.FileNotFoundException)
            {
                System.Windows.Forms.MessageBox.Show("The specified document does not exist.");
            }
            catch (UnknownCustomizationFileException)
            {
                System.Windows.Forms.MessageBox.Show("The specified document has a file " +
                                                     "extension that is not supported by Visual Studio Tools for Office.");
            }
            catch (DocumentNotCustomizedException ex)
            {
                System.Windows.Forms.MessageBox.Show("The document could not be customized.\n" +
                                                     ex.Message);
            }
        }
        ConvertNodeXLWorkbook
        (
            String otherWorkbookFile,
            String convertedWorkbookFile
        )
        {
            Debug.Assert(!String.IsNullOrEmpty(otherWorkbookFile));
            Debug.Assert(File.Exists(otherWorkbookFile));
            Debug.Assert(!String.IsNullOrEmpty(convertedWorkbookFile));

            // The application's template is needed to get the customization
            // information.

            String sTemplatePath;

            if (!ApplicationUtil.TryGetTemplatePath(out sTemplatePath))
            {
                throw new NodeXLWorkbookConversionException(
                          ApplicationUtil.GetMissingTemplateMessage());
            }

            try
            {
                File.Copy(otherWorkbookFile, convertedWorkbookFile, true);
            }
            catch (UnauthorizedAccessException)
            {
                throw new NodeXLWorkbookConversionException(
                          "The converted copy already exists and is read-only.  It can't"
                          + " be overwritten."
                          );
            }
            catch (IOException oIOException)
            {
                if (oIOException.Message.Contains(
                        "it is being used by another process"))
                {
                    throw new NodeXLWorkbookConversionException(
                              "The converted copy already exists and is open in Excel."
                              + "  It can't be overwritten."
                              );
                }

                throw (oIOException);
            }

            // Remove the other customization.

            try
            {
                if (ServerDocument.GetCustomizationVersion(
                        convertedWorkbookFile) > 0)
                {
                    ServerDocument.RemoveCustomization(convertedWorkbookFile);
                }
            }
            catch (Microsoft.VisualStudio.Tools.Applications.Runtime.
                   UnknownCustomizationFileException)
            {
                throw new NodeXLWorkbookConversionException(
                          "The file doesn't appear to be an Excel workbook."
                          );
            }

            // Create a ServerDocument from the application's template.  The
            // solution ID and deployment manifest name will be obtained from this.

            using (ServerDocument oTemplateServerDocument =
                       new ServerDocument(sTemplatePath, FileAccess.Read))
            {
                // For some reason, ServerDocument.AddCustomization() also requires
                // a path to the NodeXL assembly file, even though it doesn't get
                // embedded in the document.

                String sAssemblyFile = new Uri(
                    Assembly.GetExecutingAssembly().CodeBase).LocalPath;

                String [] asNonPublicCachedDataMembers;

                ServerDocument.AddCustomization(convertedWorkbookFile,
                                                sAssemblyFile, oTemplateServerDocument.SolutionId,
                                                oTemplateServerDocument.DeploymentManifestUrl, false,
                                                out asNonPublicCachedDataMembers);
            }
        }
Beispiel #10
0
        //</Snippet4>

        //<Snippet5>
        private void DisplayDataCacheContents(string documentPath)
        {
            int            runtimeVersion  = 0;
            ServerDocument serverDocument1 = null;

            try
            {
                runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath);

                if (runtimeVersion != 3)
                {
                    MessageBox.Show("This document does not have a Visual Studio Tools for " +
                                    "Office customization, or it has a customization that was created with " +
                                    "a version of the runtime that is incompatible with this version of the " +
                                    "ServerDocument class.");
                    return;
                }

                if (ServerDocument.IsCacheEnabled(documentPath))
                {
                    serverDocument1 = new ServerDocument(documentPath);
                    System.Text.StringBuilder stringBuilder1 =
                        new System.Text.StringBuilder();

                    // Display all of the cached data items
                    // in the document.
                    foreach (CachedDataHostItem hostItem1 in
                             serverDocument1.CachedData.HostItems)
                    {
                        stringBuilder1.Append("\nNamespace and class: ");
                        stringBuilder1.Append(hostItem1.Id + "\n");
                        foreach (CachedDataItem dataItem1 in
                                 hostItem1.CachedData)
                        {
                            stringBuilder1.Append("     Data item: ");
                            stringBuilder1.Append(dataItem1.Id + "\n");
                        }
                    }
                    MessageBox.Show(stringBuilder1.ToString());
                }
                else
                {
                    MessageBox.Show("The specified document does not have cached data.");
                }
            }
            catch (System.IO.FileNotFoundException)
            {
                System.Windows.Forms.MessageBox.Show("The specified document does not exist.");
            }
            catch (UnknownCustomizationFileException)
            {
                System.Windows.Forms.MessageBox.Show("The specified document has a file " +
                                                     "extension that is not supported by Visual Studio Tools for Office.");
            }
            finally
            {
                if (serverDocument1 != null)
                {
                    serverDocument1.Close();
                }
            }
        }