Stop copy / paste Office from ignoring region settings
I am working with Office Interop (cannot use OOXML) and want to copy a table from an Excel file to an RTF file.
So, first copy the table to Excel
excelSheet = excelBook.Worksheets[1];
excelBook.CheckCompatibility = false;
excelRange = excelSheet.Range["B12:F21"];
excelRange.Copy();
Then in Word (with open RTF) I paste it
wordApplication.Selection.Find.Execute(placeholder);
WordRange range = wordApplication.Selection.Range;
if (range.Text.Contains(placeholder))
range.Paste();
The placeholder contains text that I use as code to find out where to insert it
Now in this excel table I have cells formed as currency and therefore they contain data in the form 3,56 โฌ
, but after pasting what I have in Word (RTF file) is 3.56 $
- note the change from ,
to .
and from โฌ
to$
However, if I do it all by hand (open an Excel file in Excel, select all cells from the table, click ctrl+C
, open RTF in Word, place the cursor and click ctrl+V
- I get the correct value (Euro).
Any ideas how I make this work programmatically, how it is done manually?
source to share
So I found a little workaround - thanks to @JensKloster for the idea
After inserting the table (as in the question), I load the rtf as text into a variable and apply the following:
cnt = Regex.Replace(cnt, @"(?<main>\d+)\.(?<decimals>\d{2,3})", "${main},${decimals}").Replace("$", "โฌ");
See this seam to get the job done (at least for now I have not found any problems with the current rtf templates, although problems may appear in the future
If someone smarter gives me a better option, that would be the answer
source to share