Primitive MailMerge Using Only Limited Field Names

Obviously, the right way for our application to create a Word document based on a template is to encourage administrative users (who will follow the templates) to implement the correct merge or bookmark fields in the template document.

However, in the past we found that a regular admin user who usually does not use MailMerge, or any other "advanced" features in Word, is significantly delayed by using merge fields. We tried to do this for them, creating documentation, lots of screenshots, etc. But all this is "inconvenient" for them.

They have dozens of templates (all just different really simple letters), and they will want to modify them often enough.

What they really would like to do is simply mark the fields with a simple delimiter, such as a curly brace, which actually places the homemade merge field in our application (although Word doesn't pay attention to its significance), as in:

Dear {CustomerSurname}

Then we can simply select a field with several lines of code, as in:

w = New Word.Application
d = w.Documents.Open(...)
Dim mergedContent As String = d.Content.Text
mergedContent = mergedContent.Replace("{CustomerSurname}", Customer.Surname)
mergedContent = mergedContent.Replace("{CustomerPostcode}", Customer.Postcode)
d.Content.Text = mergedContent

It seems rude but beautifully simple (for the end user).

Has anyone else taken this route? Is there something wrong with this? We would advise them not to use the symbol "{" and "}" elsewhere in the plain text of the document, but this is not a significant limitation.

Speed? Wrapping a merge field in two lines? Other problems?

+3
2

, . , . VBA, Word .

range = document.Range()

With range.Find
 .Text = "{" & name & "}"
 .Replacement.Text = NewValueHere
 .Forward = True
 .Wrap = Word.WdFindWrap.wdFindContinue
 .Format = False
 .MatchCase = True
 .MatchWholeWord = False
 .MatchWildcards = False
 .MatchSoundsLike = False
 .MatchAllWordForms = False
End With
range.Find.Execute(Replace:=Word.WdReplace.wdReplaceAll)

, Office. Office 2007, Word. , \document.xml, String.Replace("[OldValue]", "New value"), ( docx). , , , Word. Word, 2 6 4 .

+3

, ? , , , /{/} {{}} ..

, , , {CustomerSurname}, {Customerurname} . { }.

+1

Source: https://habr.com/ru/post/1719879/


All Articles