Word Interop C#:使用现有页面插入新页面
Word Interop C#: Insert new page using existing page
我有一个包含 table 的 word 模板文件,它有 11 行和 3 列。所以第一页的 table 中有 33 个单元格。我用一些数据填充这些单元格,但是如果记录数大于 33(比如 66),我将创建两个文件,每个文件包含 33 条记录。我想创建一个包含所有 66 条记录的文件,一个文件中应该有两页,而不是两个单独的文件。我应该怎么做?
以下是我用来创建一个 doc 文件的代码。
private static string FillTemplate(List<ClientListItem> clients, string fileName)
{
//Filled document file name
var savedFileName = string.Empty;
//Search template file in current directory
var templateFilePath = System.AppDomain.CurrentDomain.BaseDirectory + "templateFile.doc";
#region Generate file for labels
object missing = System.Type.Missing;
// Create Word application
WORD._Application app = new WORD.Application();
try
{
Print("Creating label document.");
// Create new file
WORD._Document doc = app.Documents.Open(templateFilePath);
//Print("Creating app selection object");
WORD.Selection selection = app.Selection;
//If the template file contains tables
if (selection.Tables.Count > 0)
{
//Use first table
//Table's are accessed with starting index as 1 not 0
var tableToUse = selection.Tables[1];
//Counter for number of parent caregivers inserted
var counter = 0;
//Number of parent caregivers
var numberOfParentCaregivers = clients.Count;
//Loop on each row
//Rows are accessed with starting index as 1 not 0
for (int rowIndex = 1; rowIndex <= tableToUse.Rows.Count; rowIndex++)
{
//Loop on each column
//Columns are accessed with starting index as 1 not 0
for (int columnIndex = 1; columnIndex <= tableToUse.Columns.Count; columnIndex++)
{
//If counter has reached to its limit
if (counter + 1 > numberOfParentCaregivers)
{
//Stop
break;
}
//If current column index is even
if (columnIndex % 2 == 0)
{
//Do not process it
//Why? Check template file for yourself
continue;
}
//Get parent caregiver to set
var parentCaregiver = clients[counter];
Print("Generating label to add to document.");
//Get label to add to document
var labelToAdd = string.Format("{0} {1},{6}{2},{6} {3}, {4} {5}", parentCaregiver.Parent1LastName, parentCaregiver.Parent1FirstName,
parentCaregiver.Parent1StreetAddress, parentCaregiver.Parent1City, parentCaregiver.Parent1State, parentCaregiver.Parent1Zip, Environment.NewLine);
//Print(string.Format("Adding label {0} at {1}, {2} position.", labelToAdd, rowIndex, columnIndex));
//Get cell to set value
var tableCell = tableToUse.Cell(rowIndex, columnIndex);
//Set text in cell
tableCell.Range.Text = labelToAdd;
//Middle align text
tableCell.Range.ParagraphFormat.Alignment = WORD.WdParagraphAlignment.wdAlignParagraphCenter;
Print(string.Format("Label added {0} at {1}, {2} position.", labelToAdd, rowIndex, columnIndex));
counter++;
}
//If counter has reched to its limit
//i.e. no parent caregivers to process - all processed
if (counter + 1 > numberOfParentCaregivers)
{
//stop
break;
}
}
}
// Set file name to save
savedFileName = string.Format(@"{0}{1}{2}", Path.GetTempPath(), fileName, Path.GetExtension(templateFilePath));
object fname = savedFileName;
Print(string.Format("Saving new document at {0}", savedFileName));
// SaveAs new file
doc.SaveAs(ref fname, 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, ref missing);
Print(string.Format("{0} saved successfully.", savedFileName));
app.Documents.Close(ref missing, ref missing, ref missing);
}
catch (Exception exc)
{
Print("Exception while generating label document");
Print(exc.ToString());
//Set file Name to empty string
savedFileName = string.Empty;
}
finally
{
// Close Word application
app.Quit(ref missing, ref missing, ref missing);
Marshal.ReleaseComObject(app);
app = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
//Return saved file name
return savedFileName;
}
请提出更改建议,以便我可以根据要添加到模板文件中的记录数创建任意数量的页面。
谢谢,
尼克尔.
我更改了您的代码,循环遍历要插入 table(s) 中的所有项目,而不是循环遍历 table 的单元格。在此循环中,列和行的索引递增,当所有单元格都已填充时,将在文档末尾插入一个分页符,其中粘贴了原始 table 的副本。此时两个索引都重置为1,新的table设置为要使用的,一切正常继续。
这是代码:
private static string FillTemplate(List<ClientListItem> clients, string fileName)
{
//Filled document file name
var savedFileName = string.Empty;
//Search template file in current directory
var templateFilePath = System.AppDomain.CurrentDomain.BaseDirectory + "templateFile.doc";
object missing = System.Type.Missing;
// Create Word application
WORD._Application app = new WORD.Application();
try
{
app.Visible = true;
Print("Creating label document.");
// Create new file
WORD._Document doc = app.Documents.Open(templateFilePath);
//Print("Creating app selection object");
WORD.Selection selection = app.Selection;
//If the template file contains tables
if (selection.Tables.Count > 0)
{
//Use first table
var tableToUse = selection.Tables[1];
//copy the empty table in the clipboard
WORD.Range range = tableToUse.Range;
range.Copy();
int rowIndex = 1;
int columnIndex = 1;
// loop over all the items to insert
foreach (ClientListItem parentCaregiver in clients)
{
// check if all the cells in the current row have been used
if (columnIndex > tableToUse.Columns.Count)
{
// if this is the case increment the row index and restart from the first column
columnIndex = 1;
rowIndex++;
// check if all the rows in the current table have been used
if (rowIndex > tableToUse.Columns.Count)
{
// if this is the case create a new table and restart from the first row
rowIndex = 1;
// first go to end of document
selection.EndKey(WORD.WdUnits.wdStory, WORD.WdMovementType.wdMove);
// then add page break
object breakType = WORD.WdBreakType.wdPageBreak;
selection.InsertBreak(ref breakType);
// add a new table (initially with 1 row and one column) at the end of the document
// i.e. on the new page
WORD.Table tableCopy = doc.Tables.Add(selection.Range, 1, 1, ref missing, ref missing);
// paste the original empty table over the new one
tableCopy.Range.Paste();
// makes the copied table the one to use
tableToUse = tableCopy;
}
}
Print("Generating label to add to document.");
//Get label to add to document
var labelToAdd = string.Format("{0} {1},{6}{2},{6} {3}, {4} {5}", parentCaregiver.Parent1LastName, parentCaregiver.Parent1FirstName,
parentCaregiver.Parent1StreetAddress, parentCaregiver.Parent1City, parentCaregiver.Parent1State, parentCaregiver.Parent1Zip, Environment.NewLine);
//Get cell to set value
var tableCell = tableToUse.Cell(rowIndex, columnIndex);
//Set text in cell
tableCell.Range.Text = labelToAdd;
//Middle align text
tableCell.Range.ParagraphFormat.Alignment = WORD.WdParagraphAlignment.wdAlignParagraphCenter;
Print(string.Format("Label added {0} at {1}, {2} position.", labelToAdd, rowIndex, columnIndex));
columnIndex++;
}
}
// Set file name to save
savedFileName = fileName; // string.Format(@"{0}{1}{2}", Path.GetTempPath(), fileName, Path.GetExtension(templateFilePath));
object fname = savedFileName;
Print(string.Format("Saving new document at {0}", savedFileName));
// SaveAs new file
doc.SaveAs(ref fname, 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, ref missing);
Print(string.Format("{0} saved successfully.", savedFileName));
app.Documents.Close(ref missing, ref missing, ref missing);
}
catch (Exception exc)
{
Print("Exception while generating label document");
Print(exc.ToString());
//Set file Name to empty string
savedFileName = string.Empty;
}
finally
{
// Close Word application
app.Quit(ref missing, ref missing, ref missing);
Marshal.ReleaseComObject(app);
app = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
//Return saved file name
return savedFileName;
}
另一种方法是,您可以Add
模板文件到Document
而不是打开它,然后只需使用Range.InsertFile
将模板文件插入页面后每次到达 table 的最后一个单元格时中断。我使用了一个 while
循环只是为了保持您的当前流程并使用索引变量枚举项目。
private static string FillTemplate(List<ClientListItem> clients, string fileName)
{
//Filled document file name
var savedFileName = string.Empty;
//Search template file in current directory
var templateFilePath = System.AppDomain.CurrentDomain.BaseDirectory + "templateFile.doc";
//#region Generate file for labels
object missing = System.Type.Missing;
// Create Word application
WORD._Application app = new WORD.Application();
try
{
Print("Creating label document.");
// Create new file
//WORD._Document doc = app.Documents.Open(templateFilePath);
// Instead of creating a new file, just add our template to the document
WORD._Document doc = app.Documents.Add(templateFilePath);
//Print("Creating app selection object");
WORD.Selection selection = app.Selection;
//If the template file contains tables
if (selection.Tables.Count > 0)
{
//Use first table
//Table's are accessed with starting index as 1 not 0
var tableToUse = selection.Tables[selection.Tables.Count];
//Counter for number of parent caregivers inserted
var counter = 0;
//Number of parent caregivers
var numberOfParentCaregivers = clients.Count;
//Loop on each row
int rowIndex = 1, columnIndex = 1;
while (counter < numberOfParentCaregivers)
{
if(columnIndex > tableToUse.Columns.Count)
{
// Reset column index if we have reached the last column
columnIndex = 1;
// And go to the next row
rowIndex++;
if(rowIndex > tableToUse.Rows.Count)
{
// Reset row index if we have reached the last row
rowIndex = 1;
// Go the the end of the document, add a page break and insert our empty table template
object startPoint = 0;
WORD.Range range = doc.Range(ref startPoint, ref missing);
range.Collapse(WORD.WdCollapseDirection.wdCollapseEnd);
range.InsertBreak(WORD.WdBreakType.wdSectionBreakNextPage);
range.InsertFile(templateFilePath);
// Assign the new inserted template table to our current table to use
tableToUse = range.Tables[selection.Tables.Count];
}
}
var parentCaregiver = clients[counter];
Print("Generating label to add to document.");
//Get label to add to document
var labelToAdd = string.Format("{0} {1},{6}{2},{6} {3}, {4} {5}", parentCaregiver.Parent1LastName, parentCaregiver.Parent1FirstName,
parentCaregiver.Parent1StreetAddress, parentCaregiver.Parent1City, parentCaregiver.Parent1State, parentCaregiver.Parent1Zip, Environment.NewLine);
//Print(string.Format("Adding label {0} at {1}, {2} position.", labelToAdd, rowIndex, columnIndex));
//Get cell to set value
var tableCell = tableToUse.Cell(rowIndex, columnIndex);
//Set text in cell
tableCell.Range.Text = labelToAdd;
//Middle align text
tableCell.Range.ParagraphFormat.Alignment = WORD.WdParagraphAlignment.wdAlignParagraphCenter;
Print(string.Format("Label added {0} at {1}, {2} position.", labelToAdd, rowIndex, columnIndex));
// Increate items counter and columns counter on each loop
counter++;
columnIndex++;
}
}
// Set file name to save
savedFileName = string.Format(@"{0}{1}{2}", Path.GetTempPath(), fileName, Path.GetExtension(templateFilePath));
object fname = savedFileName;
Print(string.Format("Saving new document at {0}", savedFileName));
// SaveAs new file
doc.SaveAs(ref fname, 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, ref missing);
Print(string.Format("{0} saved successfully.", savedFileName));
app.Documents.Close(ref missing, ref missing, ref missing);
}
catch (Exception exc)
{
Print("Exception while generating label document");
Print(exc.ToString());
//Set file Name to empty string
savedFileName = string.Empty;
}
finally
{
// Close Word application
app.Quit(ref missing, ref missing, ref missing);
Marshal.ReleaseComObject(app);
app = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
//Return saved file name
return savedFileName;
}
我有一个包含 table 的 word 模板文件,它有 11 行和 3 列。所以第一页的 table 中有 33 个单元格。我用一些数据填充这些单元格,但是如果记录数大于 33(比如 66),我将创建两个文件,每个文件包含 33 条记录。我想创建一个包含所有 66 条记录的文件,一个文件中应该有两页,而不是两个单独的文件。我应该怎么做?
以下是我用来创建一个 doc 文件的代码。
private static string FillTemplate(List<ClientListItem> clients, string fileName)
{
//Filled document file name
var savedFileName = string.Empty;
//Search template file in current directory
var templateFilePath = System.AppDomain.CurrentDomain.BaseDirectory + "templateFile.doc";
#region Generate file for labels
object missing = System.Type.Missing;
// Create Word application
WORD._Application app = new WORD.Application();
try
{
Print("Creating label document.");
// Create new file
WORD._Document doc = app.Documents.Open(templateFilePath);
//Print("Creating app selection object");
WORD.Selection selection = app.Selection;
//If the template file contains tables
if (selection.Tables.Count > 0)
{
//Use first table
//Table's are accessed with starting index as 1 not 0
var tableToUse = selection.Tables[1];
//Counter for number of parent caregivers inserted
var counter = 0;
//Number of parent caregivers
var numberOfParentCaregivers = clients.Count;
//Loop on each row
//Rows are accessed with starting index as 1 not 0
for (int rowIndex = 1; rowIndex <= tableToUse.Rows.Count; rowIndex++)
{
//Loop on each column
//Columns are accessed with starting index as 1 not 0
for (int columnIndex = 1; columnIndex <= tableToUse.Columns.Count; columnIndex++)
{
//If counter has reached to its limit
if (counter + 1 > numberOfParentCaregivers)
{
//Stop
break;
}
//If current column index is even
if (columnIndex % 2 == 0)
{
//Do not process it
//Why? Check template file for yourself
continue;
}
//Get parent caregiver to set
var parentCaregiver = clients[counter];
Print("Generating label to add to document.");
//Get label to add to document
var labelToAdd = string.Format("{0} {1},{6}{2},{6} {3}, {4} {5}", parentCaregiver.Parent1LastName, parentCaregiver.Parent1FirstName,
parentCaregiver.Parent1StreetAddress, parentCaregiver.Parent1City, parentCaregiver.Parent1State, parentCaregiver.Parent1Zip, Environment.NewLine);
//Print(string.Format("Adding label {0} at {1}, {2} position.", labelToAdd, rowIndex, columnIndex));
//Get cell to set value
var tableCell = tableToUse.Cell(rowIndex, columnIndex);
//Set text in cell
tableCell.Range.Text = labelToAdd;
//Middle align text
tableCell.Range.ParagraphFormat.Alignment = WORD.WdParagraphAlignment.wdAlignParagraphCenter;
Print(string.Format("Label added {0} at {1}, {2} position.", labelToAdd, rowIndex, columnIndex));
counter++;
}
//If counter has reched to its limit
//i.e. no parent caregivers to process - all processed
if (counter + 1 > numberOfParentCaregivers)
{
//stop
break;
}
}
}
// Set file name to save
savedFileName = string.Format(@"{0}{1}{2}", Path.GetTempPath(), fileName, Path.GetExtension(templateFilePath));
object fname = savedFileName;
Print(string.Format("Saving new document at {0}", savedFileName));
// SaveAs new file
doc.SaveAs(ref fname, 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, ref missing);
Print(string.Format("{0} saved successfully.", savedFileName));
app.Documents.Close(ref missing, ref missing, ref missing);
}
catch (Exception exc)
{
Print("Exception while generating label document");
Print(exc.ToString());
//Set file Name to empty string
savedFileName = string.Empty;
}
finally
{
// Close Word application
app.Quit(ref missing, ref missing, ref missing);
Marshal.ReleaseComObject(app);
app = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
//Return saved file name
return savedFileName;
}
请提出更改建议,以便我可以根据要添加到模板文件中的记录数创建任意数量的页面。
谢谢, 尼克尔.
我更改了您的代码,循环遍历要插入 table(s) 中的所有项目,而不是循环遍历 table 的单元格。在此循环中,列和行的索引递增,当所有单元格都已填充时,将在文档末尾插入一个分页符,其中粘贴了原始 table 的副本。此时两个索引都重置为1,新的table设置为要使用的,一切正常继续。
这是代码:
private static string FillTemplate(List<ClientListItem> clients, string fileName)
{
//Filled document file name
var savedFileName = string.Empty;
//Search template file in current directory
var templateFilePath = System.AppDomain.CurrentDomain.BaseDirectory + "templateFile.doc";
object missing = System.Type.Missing;
// Create Word application
WORD._Application app = new WORD.Application();
try
{
app.Visible = true;
Print("Creating label document.");
// Create new file
WORD._Document doc = app.Documents.Open(templateFilePath);
//Print("Creating app selection object");
WORD.Selection selection = app.Selection;
//If the template file contains tables
if (selection.Tables.Count > 0)
{
//Use first table
var tableToUse = selection.Tables[1];
//copy the empty table in the clipboard
WORD.Range range = tableToUse.Range;
range.Copy();
int rowIndex = 1;
int columnIndex = 1;
// loop over all the items to insert
foreach (ClientListItem parentCaregiver in clients)
{
// check if all the cells in the current row have been used
if (columnIndex > tableToUse.Columns.Count)
{
// if this is the case increment the row index and restart from the first column
columnIndex = 1;
rowIndex++;
// check if all the rows in the current table have been used
if (rowIndex > tableToUse.Columns.Count)
{
// if this is the case create a new table and restart from the first row
rowIndex = 1;
// first go to end of document
selection.EndKey(WORD.WdUnits.wdStory, WORD.WdMovementType.wdMove);
// then add page break
object breakType = WORD.WdBreakType.wdPageBreak;
selection.InsertBreak(ref breakType);
// add a new table (initially with 1 row and one column) at the end of the document
// i.e. on the new page
WORD.Table tableCopy = doc.Tables.Add(selection.Range, 1, 1, ref missing, ref missing);
// paste the original empty table over the new one
tableCopy.Range.Paste();
// makes the copied table the one to use
tableToUse = tableCopy;
}
}
Print("Generating label to add to document.");
//Get label to add to document
var labelToAdd = string.Format("{0} {1},{6}{2},{6} {3}, {4} {5}", parentCaregiver.Parent1LastName, parentCaregiver.Parent1FirstName,
parentCaregiver.Parent1StreetAddress, parentCaregiver.Parent1City, parentCaregiver.Parent1State, parentCaregiver.Parent1Zip, Environment.NewLine);
//Get cell to set value
var tableCell = tableToUse.Cell(rowIndex, columnIndex);
//Set text in cell
tableCell.Range.Text = labelToAdd;
//Middle align text
tableCell.Range.ParagraphFormat.Alignment = WORD.WdParagraphAlignment.wdAlignParagraphCenter;
Print(string.Format("Label added {0} at {1}, {2} position.", labelToAdd, rowIndex, columnIndex));
columnIndex++;
}
}
// Set file name to save
savedFileName = fileName; // string.Format(@"{0}{1}{2}", Path.GetTempPath(), fileName, Path.GetExtension(templateFilePath));
object fname = savedFileName;
Print(string.Format("Saving new document at {0}", savedFileName));
// SaveAs new file
doc.SaveAs(ref fname, 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, ref missing);
Print(string.Format("{0} saved successfully.", savedFileName));
app.Documents.Close(ref missing, ref missing, ref missing);
}
catch (Exception exc)
{
Print("Exception while generating label document");
Print(exc.ToString());
//Set file Name to empty string
savedFileName = string.Empty;
}
finally
{
// Close Word application
app.Quit(ref missing, ref missing, ref missing);
Marshal.ReleaseComObject(app);
app = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
//Return saved file name
return savedFileName;
}
另一种方法是,您可以Add
模板文件到Document
而不是打开它,然后只需使用Range.InsertFile
将模板文件插入页面后每次到达 table 的最后一个单元格时中断。我使用了一个 while
循环只是为了保持您的当前流程并使用索引变量枚举项目。
private static string FillTemplate(List<ClientListItem> clients, string fileName)
{
//Filled document file name
var savedFileName = string.Empty;
//Search template file in current directory
var templateFilePath = System.AppDomain.CurrentDomain.BaseDirectory + "templateFile.doc";
//#region Generate file for labels
object missing = System.Type.Missing;
// Create Word application
WORD._Application app = new WORD.Application();
try
{
Print("Creating label document.");
// Create new file
//WORD._Document doc = app.Documents.Open(templateFilePath);
// Instead of creating a new file, just add our template to the document
WORD._Document doc = app.Documents.Add(templateFilePath);
//Print("Creating app selection object");
WORD.Selection selection = app.Selection;
//If the template file contains tables
if (selection.Tables.Count > 0)
{
//Use first table
//Table's are accessed with starting index as 1 not 0
var tableToUse = selection.Tables[selection.Tables.Count];
//Counter for number of parent caregivers inserted
var counter = 0;
//Number of parent caregivers
var numberOfParentCaregivers = clients.Count;
//Loop on each row
int rowIndex = 1, columnIndex = 1;
while (counter < numberOfParentCaregivers)
{
if(columnIndex > tableToUse.Columns.Count)
{
// Reset column index if we have reached the last column
columnIndex = 1;
// And go to the next row
rowIndex++;
if(rowIndex > tableToUse.Rows.Count)
{
// Reset row index if we have reached the last row
rowIndex = 1;
// Go the the end of the document, add a page break and insert our empty table template
object startPoint = 0;
WORD.Range range = doc.Range(ref startPoint, ref missing);
range.Collapse(WORD.WdCollapseDirection.wdCollapseEnd);
range.InsertBreak(WORD.WdBreakType.wdSectionBreakNextPage);
range.InsertFile(templateFilePath);
// Assign the new inserted template table to our current table to use
tableToUse = range.Tables[selection.Tables.Count];
}
}
var parentCaregiver = clients[counter];
Print("Generating label to add to document.");
//Get label to add to document
var labelToAdd = string.Format("{0} {1},{6}{2},{6} {3}, {4} {5}", parentCaregiver.Parent1LastName, parentCaregiver.Parent1FirstName,
parentCaregiver.Parent1StreetAddress, parentCaregiver.Parent1City, parentCaregiver.Parent1State, parentCaregiver.Parent1Zip, Environment.NewLine);
//Print(string.Format("Adding label {0} at {1}, {2} position.", labelToAdd, rowIndex, columnIndex));
//Get cell to set value
var tableCell = tableToUse.Cell(rowIndex, columnIndex);
//Set text in cell
tableCell.Range.Text = labelToAdd;
//Middle align text
tableCell.Range.ParagraphFormat.Alignment = WORD.WdParagraphAlignment.wdAlignParagraphCenter;
Print(string.Format("Label added {0} at {1}, {2} position.", labelToAdd, rowIndex, columnIndex));
// Increate items counter and columns counter on each loop
counter++;
columnIndex++;
}
}
// Set file name to save
savedFileName = string.Format(@"{0}{1}{2}", Path.GetTempPath(), fileName, Path.GetExtension(templateFilePath));
object fname = savedFileName;
Print(string.Format("Saving new document at {0}", savedFileName));
// SaveAs new file
doc.SaveAs(ref fname, 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, ref missing);
Print(string.Format("{0} saved successfully.", savedFileName));
app.Documents.Close(ref missing, ref missing, ref missing);
}
catch (Exception exc)
{
Print("Exception while generating label document");
Print(exc.ToString());
//Set file Name to empty string
savedFileName = string.Empty;
}
finally
{
// Close Word application
app.Quit(ref missing, ref missing, ref missing);
Marshal.ReleaseComObject(app);
app = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
//Return saved file name
return savedFileName;
}