Last active
November 14, 2023 17:00
-
-
Save felipeabajo/020f9962e9c05e7eea25e922bc3cac3b to your computer and use it in GitHub Desktop.
Snippets for working with documents in C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*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