我需要制作一个宏,该宏将使用我创建的模板将数据从datagridview自动导出到word报告。 该程序工作正常,但它会在单独的文件中生成每一行,因为我需要将一个文件中的所有行。
我尝试包括邮件合并,但出现错误“类型不匹配” 这是我的代码
private void FindAndReplace(word.Application wordApp, string ToFindText, object replaceWithText)
{
object MatchCase = true;
object FindText = true;
object MatchWholeWord = true;
object MatchWildcards = false;
object MatchSoundsLike = false;
object MatchAllWordForms = false;
object Forward = true;
object Format = false;
object MatchKashida = false;
object MatchDiacritics = false;
object MatchAlefHamza = false;
object MatchControl = false;
object Read_only = false;
// object ReplaceWith = false;
object Visible = true;
object Replace = 2;
object Wrap = 1;
//wordApp.Selection.Find.Execute(ToFindText,replaceWithText);
wordApp.Selection.Find.Execute(ToFindText,
ref MatchCase,
ref MatchWholeWord,
ref MatchWildcards,
ref MatchSoundsLike,
ref MatchAllWordForms,
ref Forward,
ref Wrap,
ref Format,
ref Replace,
ref MatchKashida,
ref MatchDiacritics,
ref MatchAlefHamza,
ref MatchControl);
wordApp.Selection.Text = replaceWithText.ToString();
}
private void WordDoc(object filename, object SaveAs)
{
word.Application wordApp = new word.Application();
object missing = Missing.Value;
word.Document myWordDoc = null;
object readOnly = false;
object IsVisible = false;
wordApp.Visible = false;
try
{
foreach (DataGridViewRow selectedRow in dataGridView2.Rows)
{
myWordDoc = wordApp.Documents.Open(ref filename, ref missing, ref readOnly, 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);
myWordDoc.Activate();
string Column1 = selectedRow.Cells[0].Value.ToString();
string Column2 = selectedRow.Cells[1].Value.ToString();
string Column3 = selectedRow.Cells[2].Value.ToString();
string Column4 = selectedRow.Cells[3].Value.ToString();
string Column5 = selectedRow.Cells[4].Value.ToString();
string Column6 = selectedRow.Cells[5].Value.ToString();
string Column7 = selectedRow.Cells[6].Value.ToString();
string Column8 = selectedRow.Cells[7].Value.ToString();
string Column9 = selectedRow.Cells[8].Value.ToString();
string Column10 = selectedRow.Cells[9].Value.ToString();
string Column11 = selectedRow.Cells[10].Value.ToString();
string Column12 = selectedRow.Cells[11].Value.ToString();
string Column13 = selectedRow.Cells[12].Value.ToString();
string Column14 = selectedRow.Cells[13].Value.ToString();
string Column15 = selectedRow.Cells[14].Value.ToString();
string Column16 = selectedRow.Cells[15].Value.ToString();
string Column17 = selectedRow.Cells[16].Value.ToString();
string Column18 = selectedRow.Cells[17].Value.ToString();
string Column19 = selectedRow.Cells[18].Value.ToString();
string Column20 = selectedRow.Cells[19].Value.ToString();
string Column21 = selectedRow.Cells[20].Value.ToString();
this.FindAndReplace(wordApp, "<Type1>", Column1);
this.FindAndReplace(wordApp, "<Type2>", Column2);
this.FindAndReplace(wordApp, "<Type3>", Column3);
this.FindAndReplace(wordApp, "<Type4>", Column4);
this.FindAndReplace(wordApp, "<Type5>", Column5);
this.FindAndReplace(wordApp, "<Type6>", Column6);
this.FindAndReplace(wordApp, "<Type7>", Column7);
this.FindAndReplace(wordApp, "<Type8>", Column8);
this.FindAndReplace(wordApp, "<Type9>", Column9);
this.FindAndReplace(wordApp, "<Type10>", Column10);
this.FindAndReplace(wordApp, "<Type11>", Column11);
this.FindAndReplace(wordApp, "<Type12>", Column12);
this.FindAndReplace(wordApp, "<Type13>", Column13);
this.FindAndReplace(wordApp, "<Type14>", Column14);
this.FindAndReplace(wordApp, "<Type15>", Column15);
this.FindAndReplace(wordApp, "<Type16>", Column16);
this.FindAndReplace(wordApp, "<Type17>", Column17);
this.FindAndReplace(wordApp, "<Type18>", Column18);
this.FindAndReplace(wordApp, "<Type19>", Column19);
this.FindAndReplace(wordApp, "<Type20>", Column20);
this.FindAndReplace(wordApp, "<Type21>", Column21);
//Saving file
myWordDoc.SaveAs(SaveAs.ToString() + selectedRow.ToString() + ".docx", 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);
}
}
catch (Exception ex)
{
MessageBox.Show("Error : " + ex.Message);
}
myWordDoc.Close();
wordApp.Quit();
MessageBox.Show("File Created!");
}
private void AddButton_Click(object sender, EventArgs e)
{
WordDoc(filename: Getfilepath, SaveAs: Savingpath);
}
我不知道我需要进行什么更改才能将所有行的数据保存到一个文件中
答案 0 :(得分:0)
尝试在foreach
子句之外创建和保存文档文件。
myWordDoc = wordApp.Documents.Open(ref filename, ref missing, ref readOnly, 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);
myWordDoc.Activate();
try
{
foreach (DataGridViewRow selectedRow in dataGridView2.Rows)
{
string Column1 = selectedRow.Cells[0].Value.ToString();
string Column2 = selectedRow.Cells[1].Value.ToString();
string Column3 = selectedRow.Cells[2].Value.ToString();
string Column4 = selectedRow.Cells[3].Value.ToString();
string Column5 = selectedRow.Cells[4].Value.ToString();
string Column6 = selectedRow.Cells[5].Value.ToString();
string Column7 = selectedRow.Cells[6].Value.ToString();
string Column8 = selectedRow.Cells[7].Value.ToString();
string Column9 = selectedRow.Cells[8].Value.ToString();
string Column10 = selectedRow.Cells[9].Value.ToString();
string Column11 = selectedRow.Cells[10].Value.ToString();
string Column12 = selectedRow.Cells[11].Value.ToString();
string Column13 = selectedRow.Cells[12].Value.ToString();
string Column14 = selectedRow.Cells[13].Value.ToString();
string Column15 = selectedRow.Cells[14].Value.ToString();
string Column16 = selectedRow.Cells[15].Value.ToString();
string Column17 = selectedRow.Cells[16].Value.ToString();
string Column18 = selectedRow.Cells[17].Value.ToString();
string Column19 = selectedRow.Cells[18].Value.ToString();
string Column20 = selectedRow.Cells[19].Value.ToString();
string Column21 = selectedRow.Cells[20].Value.ToString();
this.FindAndReplace(wordApp, "<Type1>", Column1);
this.FindAndReplace(wordApp, "<Type2>", Column2);
this.FindAndReplace(wordApp, "<Type3>", Column3);
this.FindAndReplace(wordApp, "<Type4>", Column4);
this.FindAndReplace(wordApp, "<Type5>", Column5);
this.FindAndReplace(wordApp, "<Type6>", Column6);
this.FindAndReplace(wordApp, "<Type7>", Column7);
this.FindAndReplace(wordApp, "<Type8>", Column8);
this.FindAndReplace(wordApp, "<Type9>", Column9);
this.FindAndReplace(wordApp, "<Type10>", Column10);
this.FindAndReplace(wordApp, "<Type11>", Column11);
this.FindAndReplace(wordApp, "<Type12>", Column12);
this.FindAndReplace(wordApp, "<Type13>", Column13);
this.FindAndReplace(wordApp, "<Type14>", Column14);
this.FindAndReplace(wordApp, "<Type15>", Column15);
this.FindAndReplace(wordApp, "<Type16>", Column16);
this.FindAndReplace(wordApp, "<Type17>", Column17);
this.FindAndReplace(wordApp, "<Type18>", Column18);
this.FindAndReplace(wordApp, "<Type19>", Column19);
this.FindAndReplace(wordApp, "<Type20>", Column20);
this.FindAndReplace(wordApp, "<Type21>", Column21);
//Saving file
myWordDoc.SaveAs(SaveAs.ToString() + selectedRow.ToString() + ".docx", 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);
}
}
catch (Exception ex)
{
MessageBox.Show("Error : " + ex.Message);
}