Hello,
I am trying to test a DLL that I wrote and I'm having problems with the following initialization code:
private void button1_Click(object sender, EventArgs e) { String[] sFieldList = {"CustomerName", "CustomerAddress", "CustomerCityStateZip"}; String[] sValues = {"Stuart Little", "1 Mouse Hole Drive", "Anytown, AS, 00000"}; try { MWPOfficeUtils.WordDoc mwpDoc = new MWPOfficeUtils.WordDoc(); mwpDoc.Merge("c:\\PolicyChange.dotx", "c:\\NewPolicyChange.html", sFieldList, sValues); MessageBox.Show("Document Created"); } catch(System.Exception ex) { MessageBox.Show(ex.Message + " " + System.IO.Directory.GetCurrentDirectory()); } }
When the code attempts to execute the mwpDoc.Merge method it throws the error:
"Index was outside the bounds of the array". Any help that can be provided will be greatly appreciated.
Regards,
Matt Paisley
If it helps, the following is the code for the Class Library
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Office.Interop.Outlook; using Microsoft.Office.Interop.Word; namespace MWPOfficeUtils { public class WordDoc { public WordDoc() { } public void Merge(string pWordDoc, string pWordOutDoc, string[] pFields, string[] values) { Object oMissing = System.Reflection.Missing.Value; Object oTrue = true; Object oFalse = false; Microsoft.Office.Interop.Word._Application oWord = new Microsoft.Office.Interop.Word.Application(); Microsoft.Office.Interop.Word._Document oWordDoc = new Microsoft.Office.Interop.Word.Document(); oWord.Visible = false; Object oTemplatePath = pWordDoc; oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing); Microsoft.Office.Interop.Word.WdSaveFormat outHTML = WdSaveFormat.wdFormatHTML; //foreach (String pMergeField in pFields) for (int i = 0; i < pFields.Length; i++) { String pValue = values[i - 1]; String pMergeField = pFields[i - 1]; foreach (Microsoft.Office.Interop.Word.Field myMergeField in oWordDoc.Fields) { Microsoft.Office.Interop.Word.Range rngFieldCode = myMergeField.Code; String fieldText = rngFieldCode.Text; if (fieldText.StartsWith(" MERGEFIELD")) { Int32 endMerge = fieldText.IndexOf("\\"); Int32 fieldNameLength = fieldText.Length - endMerge; String fieldName = fieldText.Substring(11, endMerge - 11); fieldName = fieldName.Trim(); if (fieldName == pMergeField) { myMergeField.Select(); oWord.Selection.TypeText(pValue); } } } //End foreach (Microsoft.Office.Interop.Word.Field myMergeField in oWordDoc.Fields) } // End for(int i = 0; i < pFields.Length; i++) oWordDoc.SaveAs2(pWordOutDoc, outHTML); oWordDoc.Close(); oWord.Quit(); } // End Method: Merge public void MergePDF(string pWordDoc, string pWordOutDoc, string[] pFields, string[] values) { Object oMissing = System.Reflection.Missing.Value; Object oTrue = true; Object oFalse = false; Microsoft.Office.Interop.Word._Application oWord = new Microsoft.Office.Interop.Word.Application(); Microsoft.Office.Interop.Word._Document oWordDoc = new Microsoft.Office.Interop.Word.Document(); oWord.Visible = false; Object oTemplatePath = pWordDoc; oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing); Microsoft.Office.Interop.Word.WdSaveFormat outHTML = WdSaveFormat.wdFormatHTML; //foreach (String pMergeField in pFields) for (int i = 0; i < pFields.Length; i++) { //String pValue = values[i-1]; //String pMergeField = pFields[i-1]; String pValue = values[i - 1]; String pMergeField = pFields[i - 1]; foreach (Microsoft.Office.Interop.Word.Field myMergeField in oWordDoc.Fields) { Microsoft.Office.Interop.Word.Range rngFieldCode = myMergeField.Code; String fieldText = rngFieldCode.Text; if (fieldText.StartsWith(" MERGEFIELD")) { Int32 endMerge = fieldText.IndexOf("\\"); Int32 fieldNameLength = fieldText.Length - endMerge; String fieldName = fieldText.Substring(11, endMerge - 11); fieldName = fieldName.Trim(); if (fieldName == pMergeField) { myMergeField.Select(); oWord.Selection.TypeText(pValue); } } } //End foreach (Microsoft.Office.Interop.Word.Field myMergeField in oWordDoc.Fields) } // End for(int i = 0; i < pFields.Length; i++) oWordDoc.SaveAs2(pWordOutDoc, outHTML); oWordDoc.Close(); oWord.Quit(); } // End Method: Merge } // End class WordDoc }