public static void AddStatusBox() { try { XLDocument.UpdateCurrentDoc(); Microsoft.Office.Interop.Word.Shape textBox; //Check whether the bookmark already exists and only if it doesn't add it if (!currentDoc.Bookmarks.Exists("Status")) { textBox = currentDoc.Shapes.AddTextbox(MsoTextOrientation.msoTextOrientationHorizontal, 400, 50, 100, 50); textBox.Line.Visible = MsoTriState.msoFalse; textBox.Name = "MLStatus"; textBox.TextFrame.TextRange.Text = "Status"; textBox.TextFrame.TextRange.Font.Hidden = 1; textBox.TextFrame.TextRange.Font.Italic = 1; textBox.TextFrame.TextRange.Font.Name = "Calibri"; textBox.TextFrame.TextRange.Font.Size = 16; textBox.TextFrame.TextRange.Bookmarks.Add("Status"); } } catch (Exception e) { MessageBox.Show("Unable to add the status box"); XLtools.LogException("AddStatusBox", e.ToString()); } }
public static string GetFileID() { try { XLDocument.UpdateCurrentDoc(); string fileID = ""; //Check whether the parameter has already been set fileID = ReadParameter("FileID"); fileID = currentDoc.Name; if (fileID.IndexOf("-") > -1) { int len = fileID.IndexOf("-"); //get the index of the version identifier fileID = fileID.Substring(0, len); //ignore everything after and including the - if (String.IsNullOrEmpty(fileID) || !int.TryParse(fileID, out int i)) { throw new ArgumentException("Document doesn't appear to be in VC"); } UpdateParameter("FileID", fileID); //add the parameter for next time. return(fileID); } else { return(null); } } catch (Exception e) { MessageBox.Show("Unable to discover file ID"); XLtools.LogException("GetFileID", e.ToString()); return(null); } }
public static void EndDocument() { try { XLDocument.UpdateCurrentDoc(); //close panel if open if (XLantWordRibbon.xlTaskPane1 != null) { XLantWordRibbon.xlTaskPane1.Visible = false; XLantWordRibbon.xlTaskPane1.Dispose(); //remove the task pane Globals.ThisAddIn.CustomTaskPanes.Remove(XLantWordRibbon.CustomxlTaskPane); } string str = currentDoc.FullName; //close document ((_Document)currentDoc).Close(SaveChanges: WdSaveOptions.wdDoNotSaveChanges); //no longer necessary to delete file if it is a VC doc as it does it itself //File.Delete(str); } catch (Exception e) { MessageBox.Show("Unable to close document and exit"); XLtools.LogException("EndDocument", e.ToString()); } }
public static void UpdateBookmark(string bName, string bValue, int bold = 2, string styleName = "") { try { XLDocument.UpdateCurrentDoc(); if (currentDoc.Bookmarks.Exists(bName)) { Range bRange = currentDoc.Bookmarks.get_Item(bName).Range; bRange.Text = bValue; //apply style first so that the bold can override if necessary if (styleName != "") { bRange.set_Style(currentDoc.Styles[styleName]); } //bold is not specified make no changes so it stickes with the style in the document if (bold < 2) { bRange.Bold = bold; } currentDoc.Bookmarks.Add(bName, bRange); } } catch (Exception e) { MessageBox.Show("Unable to update bookmark"); XLtools.LogException("UpdateBookmark", e.ToString()); } }
private void BioBtn_Click(object sender, RibbonControlEventArgs e) { XLForms.ClientForm selectForm = new ClientForm(); selectForm.ShowDialog(); XLMain.Client client = selectForm.selectedClient; MergeClient bioClient = new MergeClient(client); XLDocument.UpdateCurrentDoc(); Range range = XLDocument.currentDoc.Range(); XLDocument.UpdateFieldsFromRange(range, bioClient); }
public static void AddStyles() { try { XLDocument.UpdateCurrentDoc(); string path = StandardLocation() + "Letter.dotx"; Document sTemplate = OpenDoc(path, true, false, false); currentDoc.set_AttachedTemplate((object)sTemplate); sTemplate.Close(SaveChanges: false); currentDoc.UpdateStyles(); } catch (Exception e) { MessageBox.Show("Unable to add styles" + e.ToString()); XLtools.LogException("AddStyles", e.ToString()); } }
public static string ReadBookmark(string bName) { try { XLDocument.UpdateCurrentDoc(); string bText = ""; if (currentDoc.Bookmarks.Exists(bName)) { Range bRange = currentDoc.Bookmarks.get_Item(bName).Range; bText = bRange.Text; } return(bText); } catch (Exception e) { MessageBox.Show("Unable to read bookmark"); XLtools.LogException("ReadBookmark", e.ToString()); return(null); } }
public static string ReadParameter(string pName) { try { XLDocument.UpdateCurrentDoc(); DocumentProperties properties = (DocumentProperties)currentDoc.CustomDocumentProperties; foreach (DocumentProperty prop in properties) { if (prop.Name == pName) { return(prop.Value.ToString()); } } return(null); } catch (Exception e) { MessageBox.Show("Unable to read parameter"); XLtools.LogException("ReadParameter", e.ToString()); return(null); } }
public static void UpdateParameter(string pName, string pValue) { try { XLDocument.UpdateCurrentDoc(); //the cast is required in .NET 3.5 so is included here for ease although not strictly required DocumentProperties properties = (DocumentProperties)currentDoc.CustomDocumentProperties; if (properties.Cast <DocumentProperty>().Where(c => c.Name == pName).Count() == 0) { properties.Add(pName, false, MsoDocProperties.msoPropertyTypeString, pValue); } else { properties[pName].Value = pValue; } } catch (Exception e) { MessageBox.Show("Unable to update parameter"); XLtools.LogException("UpdateParameter", e.ToString()); } }
public static string TempSave(string fileType = ".docx", string fileLocation = "") { try { string location; XLDocument.UpdateCurrentDoc(); if (String.IsNullOrEmpty(fileLocation)) { string folder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\XLant\\temp\\"; if (!Directory.Exists(folder)) { Directory.CreateDirectory(folder); } location = TempFileName(folder, fileType); } else { location = fileLocation; } if (fileType == ".docx") { currentDoc.SaveAs(location, WdSaveFormat.wdFormatXMLDocument); } else if (fileType == ".pdf") { currentDoc.SaveAs(location, WdSaveFormat.wdFormatPDF); } return(location); } catch (Exception e) { MessageBox.Show("Unable to create a temporary save please do so manually"); XLtools.LogException("TempSave", e.ToString()); return(null); } }
public static void ChangeStatus(string Status) { try { XLDocument.UpdateCurrentDoc(); string currentStatus = ReadParameter("VCStatus"); if (currentStatus != Status) { if (currentDoc.Bookmarks.Exists("Status")) { Range bRange = currentDoc.Bookmarks.get_Item("Status").Range; bRange.Text = Status; currentDoc.Bookmarks.Add("Status", bRange); } UpdateParameter("VCStatus", Status); } } catch (Exception e) { MessageBox.Show("Unable to update status"); XLtools.LogException("OpenTemplate", e.ToString()); } }
public static void DeployHeader(Header header) { try { XLDocument.UpdateCurrentDoc(); try { //in some circumstances this is not setting the attribute //still adds the header and footer correctly so just warn the user. currentDoc.PageSetup.DifferentFirstPageHeaderFooter = -1; } catch { MessageBox.Show("Unable to set the first page to a different header please double check the document"); } Microsoft.Office.Interop.Word.Paragraph para = null; Microsoft.Office.Interop.Word.Range rng = null; // Setting First page Header //Clear any existing header rng = currentDoc.Sections[1].Headers[WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range; rng.Delete(); rng.ParagraphFormat.RightIndent = header.HeaderRightIndent; rng.ParagraphFormat.LeftIndent = header.HeaderLeftIndent; rng.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight; //Then build the new one line by line foreach (Line line in header.HeaderLines) { para = currentDoc.Sections[1].Headers[WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range.Paragraphs.Add(); para.Range.Font.Name = line.Font; para.Range.Font.Size = line.Size; para.Range.Font.Bold = line.Bold; para.Range.Text = line.Text + Environment.NewLine; } //Setting First page footer //First delete and existing footer rng = currentDoc.Sections[1].Footers[WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range; rng.Delete(); rng.ParagraphFormat.RightIndent = header.FooterRightIndent; rng.ParagraphFormat.LeftIndent = header.FooterLeftIndent; //Then build the new one line by line foreach (Line line in header.FooterLines) { para = currentDoc.Sections[1].Footers[WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range.Paragraphs.Add(); para.Range.Font.Name = line.Font; para.Range.Font.Size = line.Size; para.Range.Font.Bold = line.Bold; para.Range.Font.Color = WdColor.wdColorGray50; para.Range.Text = line.Text + Environment.NewLine; } foreach (Image logo in header.Images) { Microsoft.Office.Interop.Word.Range anchorRng = currentDoc.Sections[1].Headers[WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range; currentDoc.Shapes.AddPicture(logo.SourceLocation, false, true, logo.PointsFromLeftEdge, logo.PointsFromTop, logo.Width, logo.Height, anchorRng); } } catch (Exception e) { MessageBox.Show("Unable to deploy header"); XLtools.LogException("DeployHeader", e.ToString()); } }