Solved

OpenXML Wordprocessing change/replace field contents in a docx document

Posted on 2014-03-31
5
888 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

This article will show, step by step, how to integrate R code into a R Sweave document
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

746 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

12 Experts available now in Live!

Get 1:1 Help Now