Skip to content

Instantly share code, notes, and snippets.

@felipeabajo
Last active November 14, 2023 17:00
Show Gist options
  • Save felipeabajo/020f9962e9c05e7eea25e922bc3cac3b to your computer and use it in GitHub Desktop.
Save felipeabajo/020f9962e9c05e7eea25e922bc3cac3b to your computer and use it in GitHub Desktop.
Snippets for working with documents in C#
/*TEMPLATES FOR WORKING WITH DOCUMENTS*/
/*Word*/
private void TemplateUsageWordDocument(string filePath)
{ //Open app and document
Word.Application WordApp= new Word.Application();
Word.Document wordDoc = WordApp.Documents.Open(filePath, ReadOnly: true, Visible: false);
//Include operations here
//Close document and app
wordDoc.Close();
NAR(wordDoc);
WordApp.Quit();
NAR(WordApp);
}
/*Excel*/
private void TemplateUsageExcelDocument(string filePath, string sheetName)
{ //Open app and book
Excel.Application ExcelApp = new Excel.Application();
Excel.Workbook excelBook = ExcelApp.Workbooks.Open(filePath);
//Open sheet
Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelBook.Worksheets[sheetName];
//Include operations here
//Close book and app
excelBook.Close();
NAR(excelBook);
ExcelApp.Quit();
NAR(ExcelApp);
}
/*SAVING DOCUMENTS*/
/*Common*/
/*Word*/
/*.docx*/
wordDoc.SaveAs(FilePath);
/*.pdf*/
Object FilePathObject = (object)FilePath;
wordDoc.SaveAs(ref FilePathObject, WdSaveFormat.wdFormatPDF, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment