Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Tue Oct 14, 2014 12:20 pm

This may be a simple question but it's the first time I've ever done any programming with Word files.

I want to be able to load some Word documents that have "fields" in them that I can programmatically change their contents. I've read a little about FormFields and MailMerge but when I look at some of the documents I am attempting to manipulate I'm not sure what that the fields in the document actually match these description.

As an example - the document attached is a simple document from the Word gallery. When I load it up I see some fields that can be set:


    Street Address
    City, ST ZIP Code
    Title
    Company Name
    Street Address
    City, ST ZIP Code

I can't seem to find a way to load a document up and easily list these fields and then set them to a value before saving the document.

This seems like a simple task - but I'm guessing that my lack of Word knowledge (what type of fields are these) and/or Spire.Doc is holding me back here.

Any hints for a newbie like me?

I can't seem to add the test file I am using (.docx and .txt) can't be uploaded - how do I share the file that is giving me trouble?

jnalewabau
 
Posts: 8
Joined: Mon Oct 06, 2014 6:24 am

Wed Oct 15, 2014 1:33 am

Dear jnalewabau,

Thanks for your inquiry.

Please zip the document and the code snippet you're using at first, then upload it.

Best regards,
Burning
E-iceblue Support Team
Best Regards,
Burning
E-iceblue Support Team
User avatar

burning.liu
 
Posts: 406
Joined: Mon Aug 04, 2014 6:47 am

Tue Dec 02, 2014 9:32 am

Sorry for such a delay in responding - I got caught up with other projects. I have attached a very simple file that has some fields in it.

I'd like to write some code that could documents with fields in it, change the field values in the document to some values I can gather from a database and then save the Word document. This seems like a simple thing to do but I haven't found the right code to do something like this.

I was trying the following code to at least find the fields in the document but it never returns any fields.

Code: Select all
            foreach (FormField field in doc.Sections[0].Body.FormFields)
            {
                Console.WriteLine("Field = {0}", field.Name);
            }


I think this is a simple task - I just need to to know how to use Spire.NET API to find and then change field values. I'm sorry if there is a simple question.

Thanks

jnalewabau
 
Posts: 8
Joined: Mon Oct 06, 2014 6:24 am

Wed Dec 03, 2014 1:53 am

Dear jnalewabau,

After reading the document you sent, I make sure that you want to do something about mail merge.
Here is a very simple code snippet you can refer to:
Code: Select all
Document doc = new Document("Test.doc", FileFormat.Doc);
//Find fiedls
string[] fields = doc.MailMerge.GetMergeFieldNames();
foreach (string field in fields)
{
    Console.WriteLine(field);
}
//Do mail merge
string[] values = { "jnalewabau", "1234567890", DateTime.Now.ToString(), "Person Important", "Random Street", "09-87654321" };
doc.MailMerge.Execute(fields,values);
doc.SaveToFile("Result.doc",FileFormat.Doc);

Also, there are some complicated demos in our website, you can visit them via links below:
http://www.e-iceblue.com/Tutorials/Spir ... B.NET.html
http://www.e-iceblue.com/Tutorials/Spir ... B.NET.html
http://www.e-iceblue.com/Tutorials/Spir ... B.NET.html

Best Regards,
Burning
E-iceblue Support Team
Best Regards,
Burning
E-iceblue Support Team
User avatar

burning.liu
 
Posts: 406
Joined: Mon Aug 04, 2014 6:47 am

Wed Dec 03, 2014 10:40 am

Burning,

Thanks so much - I don;t know much about Word documents and fields and this was exactly what I needed to get going.

Much appreciated,
Joe

jnalewabau
 
Posts: 8
Joined: Mon Oct 06, 2014 6:24 am

Thu Dec 04, 2014 1:00 am

Dear jnalewabau,

Please feel free to contact us if you have any problems.

Best Regards,
Burning
E-iceblue Support Team
Best Regards,
Burning
E-iceblue Support Team
User avatar

burning.liu
 
Posts: 406
Joined: Mon Aug 04, 2014 6:47 am

Return to Spire.Doc