Solved

OpenXML Wordprocessing change/replace field contents in a docx document

Posted on 2014-03-31
5
932 Views
Last Modified: 2014-11-19
I'm searching a way to change the content of simple text fields in a Word docx document by VB.NET.

I have found some OpenXML based scripts for inserting pictures, or tables but nothing to change (replace) the content of simple text fields inside a well formated docx template.
0
Comment
Question by:hpnix4ever
5 Comments
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 40166911
Just asking: are you looking for something like http://www.aspose.com/.net/word-component.aspx ? But then Free ?
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 40168475
You could write your own.  A docx file is nothing more than a zip file containing XML documents.  Most strings are stored in one file (can't remember the name offhand) and referenced from other files by occurrence number within the file (very poor XML design on Microsoft's part).
0
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 500 total points
ID: 40176250
you could do something like this
using (WordprocessingDocument doc = WordprocessingDocument.Open(filename, true))
        {
            var allParas = doc.MainDocumentPart.Document.Descendants<DocumentFormat.OpenXml.Wordprocessing.Text>();

            foreach (Text item in allParas)
            {
                if (item.Text == "rrq")
                {
                    item.Text = "bbq";
                }
            }
            doc.MainDocumentPart.Document.Save();
        }

Open in new window

0
 

Author Closing Comment

by:hpnix4ever
ID: 40452401
I have a own solution with OpenXML and the mailmerge function.

Thanks for help!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now