I have a List of Merge Field names passed into my application, along with a Word Document. The Merge Fields include periods and underscores, in the form "Here.Is.An.Example.Merge_Field". These separators are required - the characters may be able to be changed, but they cannot be removed altogether.
Several of these Merge Fields are contained within a datasource that is created for the document. The datasource has no data save for the merge field names - the merge itself takes place elsewhere in the process.
I attach the document to the datasource as below:
WordApplication.MailMerge.OpenDataSource(DataFilePath);
This loads the Merge Fields into the menu as desired, but all the periods have gone. I now have "HereIsAnExampleMerge_Field", which causes issues at other points in my application.
How can I prevent Word from removing these characters?
I don't think you can, because AFAIK the merge field names are modified to be valid bookmark names, which have certain restrictions
(See, e.g. What are the limitations for bookmark names in Microsoft Word? for a discussion, except in this case I think Word will insert an "M" before an initial "_". Plus, names over 40 characters long are mangled to produce unique names.)
So what to do really depends on what issues you are facing.
You can retrieve the fieldnames that Word actually uses (i.e. its mangled names) from either ActiveDocument.MailMerge.DataSource.DataFields or .FieldNames. AFAIK these are in the same sequence as the fields in the data source, even though Word sometimes rearranges fields for display in the Edit Recipients dialog (e.g., it sorts field names that it considers to be address field names in a certain sequence). So that should allow you to match its field names with the originals.
Alternatively, if your code needs to insert { MERGEFIELD } fields in a Mail Merge Main Document and knows the sequence of the fields in the data source, you can use field numbers (1 for the first field retrieved by Word etc.), e.g. { MERGEFIELD 1 }. But beware, as I have never seen that facility documented anywhere by Microsoft.
Related
I am working with a dot NET MVC application and with an Apache Solr. I have two fields indexed into Solr, one is Name & second is Category. I have indexed some diacritics words in Name as well as in Category field with this encoding method.
HttpUtility.UrlEncode()
The reason I have index it with encoding is that I want to display Category with Facets.
So all this values are indexed in encoded form into Solr. Now, during Search process I am encoding searchterm and then searching it into Solr and it gives me result.
But the problem is that if I search the same word without diacritic, it does not give me any result as the word is stored into encoded form.
Is there any solution to solve this?
Create a new field category_norm and add a normalizing analyzer chain to it (I think the example schema has one for text), then use a copyField to automatically copy the content from your category into the new field.
Or you could turn it around and introduce category_facet for faceting with the raw value and let the field name have the search-version, again using copyField to keep them synchronized.
When we use C# with the Universe database the multi values are coming from the Universe Database as comma separated values to the programming site. Normally in Pick Basic language programming they come up as a ^252 or ^253 separated values. Therefore we can split the multi value easily with value separators because people don’t use the ^252 or ^253 in normal data entries.
But in C# when we select multi values from the Universe database they comes up with comma separated. If the multi value data actually contains a comma then we can’t use the comma value(,) as a value separator. Because this will split the multi value data in the wrong position.
For example if the multi value data is :
01 Although , we will do , Tom goes there , I will come down
The multi value for the above record are separated by a comma in the .net programming. But the first value(in bold) actually contains a comma after the “Although”.
We are facing problem to use the C# Split function to separate the data and get the individual values. Could you tell us how can we can overcome this in C# orVB.net programming with Universe database and get the individual values/sub values? .
Thank you.
In general field delimiters are required precisely for the problem you are describing. If you use " you will then also need to decide what to do when your data inside a field also holds a " in it.
When you have found a good field delimiter (one with small memory foot print and that your data is not likely to contain). You can create a regular expression to grab the data from each field.
Like others have said, some code snippets or samples will mean that answers are more accurate and helpful.
Is there anyway you can bring the data back into a specific Type, for example MS DataTable or your own structure? List, where Row is a Type you create to store all possible fields in your specific data model?
I am having an issue with importing a CSV file. The problem arises when an address field has multiple comma seperated values e.g. home no, street no, town etc.
I tried to use http://forums.asp.net/t/1705264.aspx/1 this article but, the problem did not solved because of a single field containing multiple comma separated values.
Any idea or solution? because I didnt found any help
Thanks
Don't split the string yourself. Parsing CSV files is not trivial, and using str.Split(',') will give you a lot of headaches. Try using a more robust library like CsvHelper
- https://github.com/JoshClose/CsvHelper
If that doesn't work then the format of the file is probably incorrect. Check to make sure the text fields are properly quoted.
Do you control the format of the CSV file? You could see about changing it to qualify the values by surrounding them with double quotes (""). Another option is to switch to a different delimiter like tabs.
Barring that, if the address is always in the same format, you could read in the file, and then inspect each record and manually concatenate columns 2, 3, and 4.
Are the fields surrounded by quotation marks? If so, split on "," rather than just ,.
Is the address field at the beginning or end of a record? If so, you can ignore the first x commas (if at the beginning) or split only the correct number of fields (if at the end).
Ideally, if you have control of the source file's creation, you would change the delimiter of either the address sub-fields, or the record fields.
Having used SQL Server Bulk insert of CSV file with inconsistent quotes (CsvToOtherDelimiter option) as my basis, I discovered a few weirdnesses with the RemoveCSVQuotes part [it chopped the last char from quoted strings that contained a comma!]. So.. rewrote that bit (maybe a mistake?)
One wrinkle is that the client has asked 'what about data like this?'
""17.5179C,""
I assume if I wanted to keep using the CsvToOtherDelimiter solution, I'd have to amend the RegExp...but it's WAY beyond me... what's the best approach?
To clarify: we are using C# to pre-process the file into a pipe-delimited format prior to running a bulk insert using a format file. Speed is pretty vital.
The accepted answer from your link starts with:
You are going to need to preprocess the file, period.
Why not transform your csv to xml? Then you would be able to verify your data against an xsd before storing into a database.
To convert a CSV string into a list of elements, you could write a program that keeps track of state (in quotes or out of quotes) as it processes the string one character at a time, and emits the elements it finds. The rules for quoting in CSV are weird, so you'll want to make sure you have plenty of test data.
The state machine could go like this:
scan until quote (go to 2) or comma (go to 3)
if the next character is a quote, add only one of the two quotes to the field and return to 1. Otherwise, go to 4 (or report an error if the quote isn't the first character in the field).
emit the field, go to 1
scan until quote (go to 5)
if the next character is a quote, add only one of the two quotes to the field and return to 4. Otherwise, emit the field, scan for a comma, and go to 1.
This should correctly scan stuff like:
hello, world, 123, 456
"hello world", 123, 456
"He said ""Hello, world!""", "and I said hi"
""17.5179C,"" (correctly reports an error, since there should be a
separator between the first quoted string "" and the second field
17.5179C).
Another way would be to find some existing library that does it well. Surely, CSV is common enough that such a thing must exist?
edit:
You mention that speed is vital, so I wanted to point out that (so long as the quoted strings aren't allowed to include line returns...) each line may be processed independently in parallel.
I ended up using the csv parser that I don't know we had already (comes as part of our code generation tool) - and noting that ""17.5179C,"" is not valid and will cause errors.
What is the best way to remove all merge fields from a word 2010 document using openxml sdk 2.0, and replace them with a simple text? I have some difficulties to remove them cleanly. Have tried to remove all Run objects that includes a FieldCode with a "MERGEFIELD" defined, and appended a new Run with my text. But I am missing something crucial since the field seems to stay defined for this element.
Ok, I solved this somehow. I pick out every paragraph that contains a FieldCode that contains the text "MERGEFIELD" and run through every Run, pushing all other types onto a List. When I discover a FieldChar End type, I roll back and replace the four Runs making up the merge field with a single Run containing a Text node. The only problem I have left now is nested fields. E.g. Merge field inside an if test.