I was trying to change formfields to mergeFields. The problem was, how to set cursor near the formfield i am currently trying to change.
In help i found: Note that some bookmarks in the document are assigned to form fields. Moving to
such a bookmark and inserting text there inserts the text into the form field
code.
According to text found in help it can not be done but i still managed to do it with the following code:
dF = all form fields
builder.MoveToBookmark(dF[0].Name, false, false);
builder.InsertField(“MERGEFIELD " + dF[0].Name + @” * MERGEFORMAT", “a”);
doc.Range.FormFields.Remove(dF[0]);
is the code correct?
I would rather suggest using the following code:
string filename1 = Application.StartupPath + @"\TestReplaceFormFieldsWithMergeFields.doc";
string filename2 = Application.StartupPath + @"\TestReplaceFormFieldsWithMergeFields Out.doc";
Document doc = new Document(filename1);
DocumentBuilder builder = new DocumentBuilder(doc);
// Collect all FormField names.
ArrayList formFieldNames = new ArrayList();
foreach (FormField formField in doc.Range.FormFields)
{
formFieldNames.Add(formField.Name);
}
// Iterate through all bookmarks
foreach (Bookmark bookmark in doc.Range.Bookmarks)
{
foreach (string formFieldName in formFieldNames)
{
// If bookmark corresponds to a FormField
if (bookmark.Name == formFieldName)
{
// Remove FormField and insert merge field with the same name instead.
doc.Range.FormFields.Remove(formFieldName);
builder.MoveToBookmark(bookmark.Name);
builder.InsertField("MERGEFIELD " + formFieldName + @" \* MERGEFORMAT", string.Format("«{0}»", formFieldName));
break;
}
}
}
// Remove bookmarks corresponding to former formfields (not (eally necessary, just for aesthetic purposes).
foreach (string formFieldName in formFieldNames)
{
doc.Range.Bookmarks[formFieldName].Remove();
}
// Save resulting document.
doc.Save(filename2);
// Open saved document in MS Word.
System.Diagnostics.Process.Start(filename2);
Best regards,
The code works fine, theresult is the same, when comparing with my old code (ok, i believe this code is better) but one problem still remains. Some formfields still don’t want to change. I noticed that if i open my template in word an then edit/goto/bookmarks, there are some bookmarks missing. There is a formfield but no bookmark with this name and this formfields are exactly those, which do not want to change to mergefield. If I double-click on each of those formfields and clik ok, then bookmark begins to exists.
any solution (i have too many documents to fix them manualy)?
Please provide the sample document. I will check what could be done.
I reduced size of the document. In this document the problem is with the PP_KRAJ and PP_POSTA. If you open the document, you will notice that there is no bookmark for them. If you try Edit/Goto/bookmark they are not listed. If you double-click on each of them and then clik ok, bookmark starts to exist.
The code for that one gets much trickier. Although there is little that Aspose.Words cannot handle.
string filename1 = Application.StartupPath + @"NP3.dot";
string filename2 = Application.StartupPath + @"NP3 Out.dot";
Document doc = new Document(filename1);
DocumentBuilder builder = new DocumentBuilder(doc);
// Collect all formfields.
ArrayList formFields = new ArrayList();
foreach (FormField formField in doc.Range.FormFields)
{
formFields.Add(formField);
}
// Iterate through formfield collection.
foreach (FormField formField in formFields)
{
Node node = formField;
while (node != null)
{
// seek field end for this formfield
if (node.NodeType == NodeType.FieldEnd)
{
// DocumentBuilder.InsertField method inserts field BEFORE the DocumentBuilder.CurrentNode.
// So to insert merge field field AFTER formfield we need inserting and moving to a dummy node (empty run).
// create dummy node
Run emptyRun = new Run(doc, "");
// insert it after formfield end
node.ParentNode.InsertAfter(emptyRun, node);
// position DocumentBuilder before dummy node.
builder.MoveTo(emptyRun);
// insert merge field
builder.InsertField("MERGEFIELD " + formField.Name + @" \* MERGEFORMAT", string.Format("«{0}»", formField.Name));
// remove dummy node as we don't need it anymore
emptyRun.Remove();
// remove formfield
doc.Range.FormFields.Remove(formField);
// remove bookmark for this formfield (if it exists)
if (doc.Range.Bookmarks[formField.Name] != null)
doc.Range.Bookmarks[formField.Name].Remove();
// exit while
break;
}
node = node.NextSibling;
}
}
// Save resulting document.
doc.Save(filename2);
// Open saved document in MS Word.
System.Diagnostics.Process.Start(filename2);
THANK YOU!!!