protected override void BeginProcessing() { base.BeginProcessing(); if (!(GetOfficeVersion.GetVersion("Word")).IsInstalled) { ThrowTerminatingError(new ErrorRecord(new FileNotFoundException("Excel is not installed"), "1", ErrorCategory.InvalidOperation, null)); } msWord = BindingFactory.CreateAutomationBinding("Word.Application"); if (parDisplay) { WriteVerbose("Display have been set, making application visible"); msWord.Property("Visible").Set(true); } switch (parFormat.ToLower()) { case "word": format = 12; formatName = "docx"; break; case "word97": format = 0; formatName = "doc"; break; case "pdf": format = 17; formatName = "pdf"; break; case "xps": format = 18; formatName = "xps"; break; case "odf": format = 23; formatName = "odf"; break; case "rtf": format = 6; formatName = "rtf"; break; } WriteVerbose(string.Format("Setting output format to {0} (.{1})", parFormat, formatName)); }
protected override void BeginProcessing() { base.BeginProcessing(); if (!(GetOfficeVersion.GetVersion("Excel")).IsInstalled) { ThrowTerminatingError(new ErrorRecord(new FileNotFoundException("Excel is not installed"), "1", ErrorCategory.InvalidOperation, null)); } //if (!ValidLicense()) // ThrowTerminatingError(new ErrorRecord(new Licensing.InvalidLicenseException("Product licence could not be verified"), "10", ErrorCategory.SecurityError, null)); msExcel = BindingFactory.CreateAutomationBinding("Excel.Application"); if (parDisplay) { WriteVerbose("Display have been set, making application visible"); msExcel.Property("Visible").Set(true); } currentCulture = Thread.CurrentThread.CurrentCulture; int langSettings = msExcel.Property("LanguageSettings").Get().Property("LanguageID").PropertyParam(2).Get <int>(); CultureInfo newCulture = new CultureInfo(langSettings); WriteVerbose(string.Format("Changing culture ({0}->{1})", currentCulture.DisplayName, newCulture.DisplayName)); Thread.CurrentThread.CurrentCulture = newCulture; switch (parFormat.ToLower()) { case "excel": format = 51; formatName = "xlsx"; break; case "excel97": format = 56; formatName = "xls"; break; case "pdf": format = 57; formatName = "pdf"; break; case "xps": format = 58; formatName = "xps"; break; } WriteVerbose(string.Format("Setting output format to {0} (.{1})", parFormat, formatName)); }
protected override void BeginProcessing() { base.BeginProcessing(); if (!(GetOfficeVersion.GetVersion("Excel")).IsInstalled) { ThrowTerminatingError(new ErrorRecord(new FileNotFoundException("Excel is not installed use Out-OExcel instead."), "1", ErrorCategory.InvalidOperation, null)); } if (parPath.StartsWith(@".\")) { parPath = System.IO.Path.Combine(this.CurrentProviderLocation("FileSystem").ProviderPath, parPath.Substring(2)); } parPath = System.IO.Path.GetFullPath(parPath); WriteVerbose(string.Format("Checking for existance of file {0}", parPath)); WriteProgress(new ProgressRecord(10, "Out-Excel", "Initializing..")); if (File.Exists(parPath)) { if (!parOverwrite) { WriteVerbose("File exists and -Overwrite have not been specified"); ThrowTerminatingError(new ErrorRecord(new System.IO.IOException("The file already exists and -Overwrite have not been specified"), "100", ErrorCategory.ResourceExists, parPath.ToString())); } else { WriteVerbose("File exists and -Override have been specified, will delete file"); File.Delete(Path); } } WriteVerbose("Starting Microsoft Excel"); msExcel = BindingFactory.CreateAutomationBinding("Excel.Application"); currentCulture = Thread.CurrentThread.CurrentCulture; int langSettings = msExcel.Property("LanguageSettings").Get().Property("LanguageID").PropertyParam(2).Get <int>(); CultureInfo newCulture = new CultureInfo(langSettings); WriteVerbose(string.Format("Changing culture ({0}->{1})", currentCulture.DisplayName, newCulture.DisplayName)); Thread.CurrentThread.CurrentCulture = newCulture; if (parDisplay) { WriteVerbose("Display have been set, making application visible"); msExcel.Property("Visible").Set(true); } WriteProgress(new ProgressRecord(10, "Out-Excel", "Creating document..")); try { WriteVerbose("Creating new document"); workBook = msExcel.Property("Workbooks").Get().Method("Add").Invoke(); } catch { workBook.Method("Close").AddParameter(0).Invoke(); msExcel.Method("Quit").AddParameter(0).Invoke(); WriteVerbose("Document creation failed "); ThrowTerminatingError(new ErrorRecord(new System.IO.IOException("Document creation failed"), "101", ErrorCategory.ResourceUnavailable, msExcel)); } WriteVerbose("Activating document"); workBook.Method("Activate").Invoke(); }
protected override void BeginProcessing() { base.BeginProcessing(); if (!(GetOfficeVersion.GetVersion("Excel")).IsInstalled) { ThrowTerminatingError(new ErrorRecord(new FileNotFoundException("Excel is not installed use Import-OExcelWorkbook instead."), "1", ErrorCategory.InvalidOperation, null)); } msExcel = BindingFactory.CreateAutomationBinding("Excel.Application"); currentCulture = Thread.CurrentThread.CurrentCulture; int langSettings = msExcel.Property("LanguageSettings").Get().Property("LanguageID").PropertyParam(2).Get <int>(); CultureInfo newCulture = new CultureInfo(langSettings); WriteVerbose(string.Format("Changing culture ({0}->{1})", currentCulture.DisplayName, newCulture.DisplayName)); Thread.CurrentThread.CurrentCulture = newCulture; if (parFile.StartsWith(@".\")) { parFile = System.IO.Path.Combine(this.CurrentProviderLocation("FileSystem").ProviderPath, parFile.Substring(2)); } parFile = System.IO.Path.GetFullPath(parFile); if (!System.IO.File.Exists(parFile)) { msExcel.Method("Quit").Invoke(); ThrowTerminatingError(new ErrorRecord(new FileNotFoundException(string.Format("The source file does not exist", parFile)), "200", ErrorCategory.OpenError, parFile)); } IDynamic nativeWorkbook = null; try { nativeWorkbook = msExcel.Property("Workbooks").Get().Method("Open").AddParameter(parFile).AddMissingParameters(14).Invoke(); FileInfo fileInfo = new FileInfo(parFile); Workbook workbook = new Workbook(); workbook.Filepath = parFile; workbook.FileIsReadOnly = fileInfo.IsReadOnly; workbook.FileLastModified = fileInfo.LastWriteTime; workbook.FileCreated = fileInfo.CreationTime; workbook.Author = nativeWorkbook.Property("Author").Get <string>(); workbook.Title = nativeWorkbook.Property("Title").Get <string>(); workbook.Comments = nativeWorkbook.Property("Comments").Get <string>(); workbook.Keywords = nativeWorkbook.Property("Keywords").Get <string>(); workbook.Worksheets = GetWorksheets(nativeWorkbook); WriteObject(workbook); } catch (Exception ex) { WriteError(new ErrorRecord(new InvalidDataException(string.Format("Conversion failed: {0}", ex.Message)), "201", ErrorCategory.ParserError, parFile)); } finally { if (nativeWorkbook != null) { nativeWorkbook.Method("Close").AddParameter(0).Invoke(); } msExcel.Method("Quit").Invoke(); } }
protected override void BeginProcessing() { base.BeginProcessing(); if (!(GetOfficeVersion.GetVersion("Word")).IsInstalled) { ThrowTerminatingError(new ErrorRecord(new FileNotFoundException("Excel is not installed use Out-OWord instead."), "1", ErrorCategory.InvalidOperation, null)); } if (parPath.StartsWith(@".\")) { parPath = System.IO.Path.Combine(this.CurrentProviderLocation("FileSystem").ProviderPath, parPath.Substring(2)); } parPath = System.IO.Path.GetFullPath(parPath); WriteVerbose(string.Format("Checking for existance of file {0}", parPath)); WriteProgress(new ProgressRecord(10, "Out-Word", "Initializing..")); if (File.Exists(parPath)) { if (!parOverwrite) { WriteVerbose("File exists and -Overwrite have not been specified"); ThrowTerminatingError(new ErrorRecord(new System.IO.IOException("The file already exists and -Overwrite have not been specified"), "100", ErrorCategory.ResourceExists, parPath.ToString())); } else { WriteVerbose("File exists and -Override have been specified, will delete file"); File.Delete(Path); } } WriteVerbose("Starting Microsoft Word"); msWord = BindingFactory.CreateAutomationBinding("Word.Application"); if (parDisplay) { WriteVerbose("Display have been set, making application visible"); msWord.Property("Visible").Set(true); } WriteProgress(new ProgressRecord(10, "Out-Word", "Creating document..")); try { WriteVerbose("Creating new document"); wordDoc = msWord.Property("Documents").Get().Method("Add").AddParameter(Template).AddParameter(false).AddParameter(0).AddParameter(true).Invoke(); } catch { msWord.Method("Quit").AddParameter(0).Invoke(); WriteVerbose("Document creation failed (error in template most likely)"); ThrowTerminatingError(new ErrorRecord(new System.IO.IOException("Template not valid/found"), "101", ErrorCategory.ResourceUnavailable, parTemplate)); } //WriteVerbose("Activating document"); //msWord.Method("Activate").Invoke(); if (wordTitleStyle == string.Empty) { wordTitleStyle = wordDoc.Property("Styles").Get().Index(-63).Get().Property("NameLocal").Get <string>(); //wordTitleStyle = wordDoc.Styles[WdBuiltinStyle.wdStyleTitle].NameLocal; WriteVerbose(string.Format("Default localized TITLE style is: {0}", wordTitleStyle)); } if (wordHeaderStyle == string.Empty) { //wordHeaderStyle = wordDoc.Styles[WdBuiltinStyle.wdStyleNormal].NameLocal; wordHeaderStyle = wordDoc.Property("Styles").Get().Index(-1).Get().Property("NameLocal").Get <string>(); WriteVerbose(string.Format("Default localized HEADER style is: {0}", wordHeaderStyle)); } if (wordFooterStyle == string.Empty) { //wordFooterStyle = wordDoc.Styles[WdBuiltinStyle.wdStyleQuote].NameLocal; wordFooterStyle = wordDoc.Property("Styles").Get().Index(-181).Get().Property("NameLocal").Get <string>(); WriteVerbose(string.Format("Default localized FOOTER style is: {0}", wordFooterStyle)); } if (wordTableStyle == string.Empty) { //wordTableStyle = wordDoc.Styles[WdBuiltinStyle.wdStyleTableMediumList1Accent1].NameLocal; wordTableStyle = wordDoc.Property("Styles").Get().Index(-178).Get().Property("NameLocal").Get <string>(); WriteVerbose(string.Format("Default localized TABLE style is: {0}", wordTableStyle)); } if (wordTableHeaderStyle == string.Empty) { //wordTableHeaderStyle = wordDoc.Styles[WdBuiltinStyle.wdStyleIntenseEmphasis].NameLocal; wordTableHeaderStyle = wordDoc.Property("Styles").Get().Index(-262).Get().Property("NameLocal").Get <string>(); WriteVerbose(string.Format("Default localized TABLEHEADER style is: {0}", wordTableHeaderStyle)); } if (wordTableRowStyle == string.Empty) { //wordTableRowStyle = wordDoc.Styles[WdBuiltinStyle.wdStyleEmphasis].NameLocal; wordTableRowStyle = wordDoc.Property("Styles").Get().Index(-89).Get().Property("NameLocal").Get <string>(); WriteVerbose(string.Format("Default localized TABLEROW style is: {0}", wordTableRowStyle)); } currentRange = msWord.Property("Selection").Get().Property("Range").Get(); WriteProgress(new ProgressRecord(10, "Out-Word", "Creating title and headers..")); if (parTitle != String.Empty) { WriteVerbose("Adding title"); IDynamic newPara = wordDoc.Property("Paragraphs").Get().Method("Add").Invoke(); try { newPara.Property("Style").Set(wordTitleStyle); } catch { WriteWarning(string.Format("The style specified for document title ('{0}') was not found", wordTitleStyle)); } newPara.Property("Range").Get().Property("Text").Set(parTitle); newPara.Property("Range").Get().Method("InsertParagraphAfter").Invoke(); currentRange = newPara.Property("Range").Get(); } if (parHeader != String.Empty) { WriteVerbose("Adding header"); IDynamic newPara = wordDoc.Property("Paragraphs").Get().Method("Add").Invoke(); try { newPara.Property("Style").Set(wordHeaderStyle); } catch { WriteWarning(string.Format("The style specified for document header ('{0}') was not found", wordHeaderStyle)); } newPara.Property("Range").Get().Property("Text").Set(parHeader); newPara.Property("Range").Get().Method("InsertParagraphAfter").Invoke(); currentRange = newPara.Property("Range").Get(); } }