Free Support Forum - aspose.com

Inserting a field without knowing its type

Hi,

I wanted to know if there is a way of inserting a field without knowing it’s type/field code etc., and only appending it later with a proper field code.
In Word you are allowed to insert an empty Field (ctrl +F9) without any Field Code.

I tried to use documentBuilder.InsertField(FieldType.FieldNone, false), but i am getting ‘System.InvalidOperationException : Field type ‘FieldNone’ is invalid or not supported.’

could you please advice?

Thanks
Aga

@acturisaspose,

Thanks for your inquiry. Please use DocumentBuilder.InsertField method (String) to insert a Word field into a document and updates the field result. Please check the following code examples.

If you still face problem, please ZIP and attach your expected output Word document here for our reference. We will then provide you more information about your query along with code.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertField(@"=10+10");

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertField(@"MERGEFIELD MyFieldName \* MERGEFORMAT");

ReferenceDoclet_withSingleCell.zip (22 Bytes)

Hello Tahir,

i think you misunderstood my question - i don’t ask how to insert field different way (i know all the overloads described in your documentation). instead i wan’t to know how to insert empty field (see template attached).

(although i think it might be a defect in aspose as when i tried to load this template using aspose library, i got an exception too)

Thanks
Aga

@acturisaspose,

Thanks for your inquiry. Unfortunately, the ZIP file does not contain any file. Please ZIP and attach it again.

We have tested the scenario using following code example. It throws System.InvalidOperationException. We have logged this problem in our issue tracking system as WORDSNET-17328. You will be notified via this forum thread once this issue is resolved. We apologize for your inconvenience.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertField(FieldType.FieldNone, true);

EmptyFieldExample.zip (8.8 KB)

Hi Tahir,

sorry for the empty zip, corrected attached.
The case you have tested is exactly what i meant. Additionally, if you try to load attached template as
var doc = new Document([template]);
it will fail with exception too.

Many thanks
Aga

@acturisaspose,

Thanks for your inquiry. We have imported the shared document into Aspose.Words DOM using the latest version of Aspose.Words for .NET 18.8 and have not found the shared issue.

Could you please share some detail about your use case why you want to insert FieldType.FieldNone into the document? We will then provide you more information about your query.

Hello Tahir,

We construct our documents dynamically and in most of the cases at the moment when we are inserting field, we don’t know what type of field it will be (or what text it will have). We need to be able to insert empty field the way word allows to do it and move to it at later time to update the type/text.

Thanks
Aga

@acturisaspose,

Thanks for sharing the detail. Could you please share the steps that you are using to create the empty field using MS Word? Please also share your final expected output document. This will helps us to understand your requirement. Thanks for your cooperation.

FieldsExample.zip (9.1 KB)
To insert a field in word you can hit “ctrl + F9” key combination. this will insert " { }"
You can then (but don’t have to) type in field content (eg. field code). if you don’t do anything the field will remain there as an empty field and, when you hide logic (alt + F9) it will get hidden and display nothing.

attached is a document with various fields - they are just normal fields but to explain further what we are doing - often we will get information that in certain place in the document there will be a Field. Only later we get information that it will actually be an IF (we just get a text input for field code which we don’t analyze, and instead would just insert it as a field content), and then again only later we get info that there should be few mergefields inside the IF field

I hope this information helps a bit, please let me know if you had any more questions

Thanks
Aga

@acturisaspose,

Thanks for sharing the detail. In your case, we suggest you please insert the bookmark instead of None field type. Once you know the information of filed, you can move the cursor to the bookmark and insert the field of specific type. Moreover, you can not change the field type using Aspose.Words. E.g. If you insert a field of type MergeField, you cannot change it to Page field. You need to remove the old field and insert the new one.

Hi Tahir,

thanks a lot for your reply. Unfortunately the bookmark solution is not working in our case.

I was checking in the office documentation and it seems that they do allow changing Field Type:

https://docs.microsoft.com/en-us/office/vba/api/word.field.code
(see second example)

I know you can update text inside the field in Aspose (eg. moving cursor to inside the Field and updating the text) but this won’t change fields Type (as you said). However if you save this document,open it in word, hit update field (right click on a field > Update Field), the field is being correctly updated to the desired type (ie. using the field code we have changed to)

I appreciate that this is now not being supported by Aspose, but is there any chance to add this functionality? It seems that this is something that is possible in Word itself so it might make sense to be able to do this in Aspose as well.

Thanks
Aga

@acturisaspose

Thanks for sharing the detail. We have updated the issue detail. We will inform you via this forum thread once there is an update available on this issue.

The issues you have found earlier (filed as WORDSNET-17328) have been fixed in this Aspose.Words for .NET 18.10 update and this Aspose.Words for Java 18.10 update.