Tengo una aplicacion que agrega texto en word en determinados lugares
determinados por bookmarks, el problema que tengo es que al agregar texto me
corre el texto que sigue la cantidad de caracteres que tiene el texto
insertado lo que provoca que el documento pierda todo el formato que tiene.
Agrego el texto con el objeto range.
Alguien tiene una idea de como resolver esto.
Aca les paso el codigo.
public void SetTextValues()
{
object fileName = "D:\\documento.doc";
object readOnly = false;
object isVisible = false;
object missing = System.Reflection.Missing.Value;
WordApp = new Word.ApplicationClass();
doc = WordApp.Documents.Open(ref fileName,
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);
List<TextBox> textBoxes = new List<TextBox>();
WordApp.Options.Overtype = true;
foreach (Control control in this.groupBox1.Controls)
{
if (control.GetType() == typeof(TextBox))
{
textBoxes.Add((TextBox)control);
}
}
foreach (Control control in this.groupBox2.Controls)
{
if (control.GetType() == typeof(TextBox))
{
textBoxes.Add((TextBox)control);
}
}
for (int i = 1; i <= doc.Bookmarks.Count; i++)
{
object objI = i;
string bookName = doc.Bookmarks.Item(ref objI).Name;
Word.Range rng = doc.Bookmarks.Item(ref objI).Range;
i++;
object objIEnd = i;
Word.Range rngEnd = doc.Bookmarks.Item(ref objIEnd).Range;
rng.Text = "";
object objIEnd2 = i + 1;
Word.Range rngEnd2 = doc.Bookmarks.Item(ref objIEnd).Range;
foreach (TextBox tb in textBoxes)
{
if (tb.Name == bookName)
{
object start = Word.WdCollapseDirection.wdCollapseStart;
rng.End = rngEnd.Start - tb.Text.Trim().Length;
rng.Collapse(ref start);
rng.Select();
rng.Text = tb.Text.Trim();
break;
}
}
object oRng = rng;
doc.Bookmarks.Add(bookName, ref oRng);
}
Leer las respuestas