Link to home
Start Free TrialLog in
Avatar of Ess Kay
Ess KayFlag for United States of America

asked on

MS Word Replace plaintext with MergeField using vb.net

I have a bunch of letters in RTF format

I made code to convert them to DOTX

The next step is to convert the old plaintext fields into mailmerge fields

This is what i need help with



Here is my current code:
Dim rtfLoc = "c:/test/inputdoc.rtf"
Dim dotXLoc "c:/test/result.dotx"

Dim Fileformat As Microsoft.Office.Interop.Word.WdSaveFormat = Word.WdSaveFormat.wdFormatXMLTemplate  'SAVE AS DOT
Dim wordApp As Object = New Microsoft.Office.Interop.Word.Application()
Dim currentDoc As Microsoft.Office.Interop.Word.Document = wordApp.Documents.Open(rtfLoc)
currentDoc.SaveAs(dotXLoc, Fileformat)
currentDoc.Close()
wordApp.Quit()

Open in new window



Lets say i have an array

Dim replacements(,) As String =
	    New String(,) {{"[firstname]", "$Field.FName"},
			   {"[lastname]", "$Field.LName"},
			   {"[addr]", "$Field.Addr.St"},
			   {"[city]", "$Field.City"}}

Open in new window


Here is the code to run through the array for each replace
' Get bounds of the array.
Dim bound0 As Integer = replacements.GetUpperBound(0)
	
' Loop over all elements.
For i As Integer = 0 To bound0
	' Get element.
	Dim FieldFind As String = replacements(i, 0)
	Dim FieldReplace As String = replacements(i, 1)

        'CODE HERE TO REPLACE TEXT WITH MERGEFIELD
Next

Open in new window

the first column is the look for text,
the second column is the replace text, which has to be a Word Merge Field

So plaintext [lastname] will be replaced with MergeField   $Field.LName


How to do this?





Can I do this as i run the migrate from RTF code above, or do i need to save as DOTX first, then open and do..etc.
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

An open Word document has the same object structure whatever the file type that it was loaded from, so no intermediate conversion is necessary.
Avatar of Ess Kay

ASKER

please read the question again.  

i have a full Plaintext document

i need to replace some of the text with Microsoft MergeField fields
Sorry, I took this as your question:

"Can I do this as i run the migrate from RTF code above, or do i need to save as DOTX first, then open and do..etc."

I'll see if I can address any of the detail in your exposition.
SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ess Kay

ASKER

i don't know how to convert that to .net
Here is the core of my code (with the word Set removed) plugged into your code. I have removed the two string declarations as each value is only used once in the loop and setting them would introduce an extra couple of steps.
 
'
 Loop over all elements.
For i As Integer = 0 To bound0
       rng = currentdoc.Range
        With rng.Find
            .Text = replacements(i, 0)
            Do While .Execute(Replace:=wdReplaceOne) 'replace with ""
                currentdoc.Fields.Add rng, wdFieldMergeField, replacements(i, 1)
            Loop
        End With
    Next i

Open in new window

I am downloading Visual Studio 2015 community edition to test it, but it is taking a very long time.
Avatar of Ess Kay

ASKER

thanks i used your code to do what i need.  will paste code and accept when i get back.  thank you
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ess Kay

ASKER

Thanks
Avatar of Albi-Lot
Albi-Lot

My preference with mail merge scenarios is the one based on template document(s) which are filled with data at runtime. I think you could benefit from this kind of approach too. There are several 3rd party solutions available, I am using Docentric Toolkit.

Data is provided by .NET application as collection classes and can come from anywhere (xml, database, plain text, …). It only takes two lines of code to merge data with the template. Output can be DOCX and PDF.

The benefits I see are:
(1) Templates can be made with MS Word and can contain any formatting
(2) Even end users can change or create templates as long as the data structures are the same
(3) It runs well on servers with multiple users and generates word or pdf documents without MS Office or PDF driver installed.