zaaReloaded2/zaaReloaded2/Demo/Demo.cs

73 lines
2.4 KiB
C#
Executable File

/* Demo.cs
* part of zaaReloaded2
*
* Copyright 2015 Daniel Kraus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System.IO;
using Microsoft.Office.Interop.Word;
namespace zaaReloaded2.Demo
{
/// <summary>
/// Static class that serves to load an embedded Word document
/// into a temporary file and open that temporary file in Word.
/// </summary>
static class Demo
{
#region Public methods
/// <summary>
/// Opens a demo document. The document's Saved property is set to
/// false so that a user who wants to save changes will be prompted
/// for a file name.
/// </summary>
public static void OpenDemoDocument()
{
string tempFile = CreateTempFileFromResource();
Document demoDoc = Globals.ThisAddIn.Application.Documents
.Add(Template: tempFile);
File.Delete(tempFile);
Globals.ThisAddIn.Ribbon.ActivateZaaTab();
}
#endregion
#region Private methods
/// <summary>
/// Copies the built-in demo file from a resource stream to a
/// temporary file and returns the path to that file.
/// </summary>
/// <returns>Path to the newly created temporary file.</returns>
static string CreateTempFileFromResource()
{
string resourceName = "zaaReloaded2.Demo.Demo.docx";
Stream resourceStream = typeof(Demo).Assembly
.GetManifestResourceStream(resourceName);
if (resourceStream == null)
{
throw new IOException("Unable to open stream " + resourceName);
}
string fileName = Path.GetTempFileName() + ".docx";
Stream tempStream = File.Create(fileName);
resourceStream.CopyTo(tempStream);
tempStream.Close();
return fileName;
}
#endregion
}
}