Decimal parsing differences on separate environments - vb.net

Evening,
I'm bashing my head against a wall with the following problem:
I'm loading numbers from cells from a Number column with size=16
and decimal places = 2 inside adBase III .dbf file.
These numbers, when viewed with a DbfViewer appear as: 12345.12, where there is no thousands separator and the decimal
separator is ..
I parse the number from the cell in the database using decimal.parse(val).
I do stuff with that number.
I am using the ClosedXML library to paste the number into an .xlsx Excel file cell with the following formula: "=R[-1]C * 100/" & val where val is the value I obtained from the dBaseIII database file. This is done with the following statements:
Dim formula as String = "=R[-1]C * 100/" & project.TotalIncome(i)
cell.FormulaR1C1 = formula.
I am using two programming environments:
A Windows 8.1 machine with Visual Studio 2013 Community and Office 2010.
A Windows 8.1 machine with Visual Studio 2013 Ultimate and Office 2013.
I have made sure that both environments have the same Language, Date, Time and Number format, both for Windows and Office.
When I build and execute the program from the Option 1 Environment, everything pastes fine inside the Excel file. I navigate to the cell containing the formula, and whether or not the value obtained had decimal places, the formula is there.
However, If I build and execute the program from the Option 2 Environment, I get a:
Removed Records: Formula from /xl/worksheets/sheet.xml part
Removed Records: Formula from /xl/calcChain.xml part (calculation properties)
I tried adding a breakpoint in Environment 2, opening the Locals window and editing those values which had decimal places and everything worked as intended, whereas when I use Environment 1 I have no trouble whatsoever when the value has decimal places.
I have tried the following (in Environment 2):
Dim nfi As NumberFormatInfo = New CultureInfo("es-ES", False).NumberFormat
nfi.NumberDecimalSeparator = ","
value = Decimal.Parse(row("VALUECOL"), nfi)
also:
value = Decimal.Parse(row("VALUECOL"), New CultureInfo("es-ES"))
To no avail.
I have opened the XML file containing the Excel Sheet info in Environment 2 and found this:
<x:c r="L101" s="41">
<x:f>L100 * 100/57125,71</x:f>
</x:c>
Whereas the definitions for the same XML file created by the Environment 1 has the following cell value:
<x:c r="L101" s="41">
<x:f>L100 * 100/57125.71</x:f>
</x:c>
So, is it a Visual Studio Locale thing (which both have the same, as far as I can see), or am I missing something else?
EDIT: Printing out the current Locale with:
Console.WriteLine(CultureInfo.CurrentCulture.Name)
yields the same es-ES on both Environment 1 and Environment 2.
EDIT 2:
Taken from: Microsoft Office XML formats. Defective by design.
To save them time, Microsoft chose to store XML using the US English
locale regardless of all settings above. [...]
Also, for Excel formulas, it means the formula names are US English
formula names, [...] it implies you are willing to work with US English
function names (plus US English separators, ...).
So basically it all boils down (I believe) to a pre localisation of the decimal value into the Excel XML taking into account something, somewhere.
In Environment 2, any other (non-formula) value I write to the Excel file appears in the XML as an en-US localised value (i.e. 12345.12). Most of them brought in by a dataTable import. However, since writing a formula requires the input of a string, and Visual Studio applies locale settings to said string, it ends up as 12345,12 in the Excel XML, which results in the previously mentioned errors.
So, what on earth is Visual Studio taking from Environment 1 that is different from Environment 2? All possible UI localisation options are exactly the same in both machines...

I had a similar issue before, and found that there was a different dll file in my project references. The dll's were named the same, I only noticed because of a file size difference. Once I manually linked to the same one on both Dev machines, I got the expected results.
Like I said, my issue was different... But it did also involve excel files, and I did have Excel 2010 on one Dev machine and 2013 on the other.

I don't even know if this qualifies as an answer since I still have no clue about where's the localisation variable that Environment 1 has different from Environment 2.
However, It seems Visual Studio -when using different localisations- deals internally with de-localised decimal variables, but with localised string variables. Even when checking the locals panel during debugging, the value of a decimal number stored in a dictionary entry will appear as its localised version on the keyValuePair entry, and as a de-localised en-US value when expanded:
Hence, when outputting a dataTable as a whole to the Excel file, it's written onto the XML as en-US values. On the other hand, when outputting a formula (a.k.a. a string) it pours over the localised version of the associated decimal value.
Conclusion: When dealing with Office files in localised systems, just write the data as de-localised (i.e. en-US) and let the software localise it for you.
Ended up doing the following dirty patch:
Dim formula As String = "=R[-1]C * 100/" & project.TotalIncome(i).ToString().Replace(",", ".")

Related

Change Heading Number copied from another document

How can I change the Heading Number with VBA code?
For example, "1.1 Computer system"
I'd like to change "1.1" to "1.2".
I can read it with:
Selection.Paragraphs(1).Range.ListFormat.ListString
I can't find a way to change it.
Basic Function Test
1.1. LED Function Test Purpose: To make sure all the LED Functions are working as the Product Specification Resource Requirements:
The context is shown above. Sometimes, I copy from another document. The pasted heading number is not correct.
I tried to record the macro but the recorded macro is empty.
To force Heading 2 to start the numbering from 1.2, all you need is:
ActiveDocument.Styles(wdStyleHeading2).ListTemplate.ListLevels(2).StartAt = 2
Your update shows you're trying to do something quite different, however.
The only reliable way to retain the original numbering when copying/pasting between documents is to either:
convert the source numbering to static text before copying; or
paste the copied content as unformatted text.

How to create a program to convert unit measurements

Using Microsoft access, visual basic.
I'm having a big problem doing this task.
What I have done: Created a table on access where I have put measurements in (from meters):
mile = 10000meters, nautic mile = 1862meters, English mile=1652, kilometers = 1000 meters and all the way down to Millimeters.
What I have created for input:
1 box takes an Integer to be converted and a 1 box specified with an initial unit.
What I have created for Output:
1 box shows the Integer of result with 1 box specified the chosen unit of the output.
Can anyone please, please help me with the codes?
Honestly I'd never really noticed the CONVERT function until today but here's a quick demo of how I'd slap together a "conversion tool" in Excel.
If you want to do the same thing in Access, the premise is the same, but it will be a bit more work since you'll have to design the form from scratch instead of using a worksheet, which is kind of meant for this kind of job.
Using Excel functions in Access
Before you are able to use Excel's CONVERT function in Access, you'll need to reference the Microsoft Excel Object Library.
In Access, open any VBA Module.
GoTools > References
Check the box next to Microsoft Excel 16.0 Object Library. (The version number will vary if you have an older version of Office.)
Then you can call most Excel functions from Access VBA or queries with WorksheetFunction (the same way you would use them in Excel VBA).
For example:
MsgBox WorksheetFunction.Convert(3.7, "m", "ft")
...displays a message box with the number of feet in 3.7 metres.
The calculations will be the easy part; a couple lines of VBA in the On Change or On Exit events will trigger the calculation.
The most time-consuming part will likely be perfecting the placement and formatting of the controls on the form, which is by no means difficult (and there are several tutorials online that can provide the basics if necessary.)
Lastly, keep in mind that there are no doubt a plethora of existing conversion tools available for free download with a little Googling... (I'm confident that you're not the first person who wanted to use MS Office to convert measurements.) 😉
More Information:
Microsoft Docs : WorksheetFunction.Convert Method
Microsoft Docs : List of Worksheet Functions Available to Visual Basic
Office Support : Create a form in Access
QuackIt: Microsoft Access Tutorial
Blueclaw : Access Event Procedures
You can download the demo xlsx used above from JumpShare here.
For both comboboxes, bind them to column 2, faktorTilMeter, and set the ColumnWidths to, say: 2,542cm;0cm.
Then, assign this expression as ControlSource for your output textbox:
=TextboxInput/ComboboxFrom*ComboboxTo

Excel 2016 VBA - Slow performance

Our company recently upgraded to Office 2016 and a lot of people in the office are experiencing very slow calculation speeds (like 1%/s if their lucky).
I have tried restarting, repairing Office, removing the add-ins, etc. I narrowed it down to a file having VBA in it. For example, I created two files: both have two columns of 10k observations of hard-coded numbers and one calculated column. The only difference is that file #1 uses a predefined formula (below) while file #2 uses a UDF (below) and is saved as .xlsm instead of .xlsx.
File #1 (.xlsx)
C1 = 100 * A1 + B1
File #2 (.xlsm)
C1 = UDF(A1, B1)
Where UDF is defined as:
Function UDF(x, y)
UDF = 100 * x + y
End Function
Are there any steps I can try to fix this issue? Not everyone is experiencing it either. I've sent the VBA file around and for those that it calculates fast (i.e. instantaneously) I had them screenshot their VBA references but they are the same as what I have.
If starting Excel in Safe Mode cures the problem, it has to be down to an add-in or startup file - or some combination of them. Disable them all and see if that fixes it, then you can re-enable one by one to try and narrow down the culprit.

Open XML SDK 2.5 document validation: The 'smtClean' attribute is not declared

Our current work project involves opening a Microsoft PowerPoint file (.pptx format), changing some text and chart values, and then presenting the edited version to the end user.
This works rather well so far, but I'm puzzled by what happens when I try to validate the document afterwards. Using the DocumentFormat.OpenXml.Validation.OpenXmlValidator class, I run the Validate function with the PresentationDocument passed in as the only parameter.
Dim document As PresentationDocument = PresentationDocument.Open(templateFilePath, True)
Dim validator As OpenXmlValidator = New OpenXmlValidator()
Dim errors = validator.Validate(document)
For Each errInfo As ValidationErrorInfo In errors
Debug.Print("Error: """ & errInfo .Description & """")
Debug.Print("XPath: " & errInfo .Path.XPath)
Next
Validate() returns an array filled with instances of ValidationErrorInfo. Just about all of these give the same error description when debugging:
The 'smtClean' attribute is not declared.
The XPath for each error looks like this (numbers vary; there appears to be one error per piece of text):
/p:sldLayout[1]/p:cSld[1]/p:spTree[1]/p:sp[4]/p:txBody[1]/a:p[1]/a:fld[1]/a:rPr[1]
Every TableCell has a Paragraph, with child element Run, and this Run has child elements RunProperties and Text. I modify the Text in my scripts, but I do not touch anything else.
Searching for 'smtClean' gave me an MSDN entry for RunProperties which shows 'smtClean' as one of the possible values to be set, but if I create a new instance of DocumentFormat.OpenXml.Presentation.Drawing.RunProperties the 'smtClean' attribute is not available.
Looking around, I found threads where people mentioned merged documents to be one possible cause, but these errors occur even in an unmodified presentation with only a single slide and table in it. Using the Open XML SDK 2.5 Productivity Tool to Validate the base document, I get the same result.
The errors also occur no matter which format I ask the Validator to test for - both the 2007, 2010 and 2013 version of the PowerPoint format return the same amount of errors.
Finally: The file itself works just fine in PowerPoint, even after being modified. I am curious about why the validator returns so many errors, however.
Thanks in advance for any help.
we process Office Documents and remove this Attribute in all Types (Word, Powerpoint, Excel) without Side-Effects. Eric White has identified this as Bug: smtpClean attribute not supported
It is fixed in the current OpenXml SDK on Branch Office2016: https://github.com/OfficeDev/Open-XML-SDK/tree/Office2016
Regards...
Smart tags were deprecated in Office 2010, and the SDK v2.5 validator doesn't support smart tag elements and therefore marks them as invalid.
Please see this MSDN article for more information.
The current developer of the productivity tool says in this thread that the smtClean validation error was a bug in some situations and has been fixed in v3 of the tool.
v3 (the Office 2016 productivity tool) can be found here, however I'm not sure how compatible it is with older versions of Office.

vb.NET SaveAs not saving all Excel data

I have a very strange issue that I cannot seem to find an answer to online.
I have a VB.NET application that creates an Excel of data (roughly 42,542 rows in total) and the saves the file to a folder location & opens it on screen for the user.
The onscreen version & folder version is only showing 16,372 rows of data like it is being cut off.
When I go through debug I can see all the rows are being added & if I save manually in debug all the rows save. Some data seems to get lost on the system save.
I am taking data from 4 record sets & writing each set one after the other with specific headers for each block on the Excel sheet.
My save line is:
xlWBook.SaveAs(Filename:=sFileName, FileFormat:=Excel.XlFileFormat.xlExcel7)
Would anyone please have any ideas as to what this might be?
Older version of Excel only support 16,384 rows per worksheet. You are saving as Excel7 (which is Excel 95) and has this limitation:
See here for a summary of sizes per version:
https://superuser.com/questions/366468/what-is-the-maximum-allowed-rows-in-a-microsoft-excel-xls-or-xlsx
Change your code to another format, See here for all the allowed formats: XlFileFormat Enumeration
However the file format is actually an optional argument in the SaveAs method, so you could leave it off altogether: "For an existing file, the default format is the last file format specified; for a new file, the default is the format of the version of Excel being used."
Source: WorkBook.SaveAs Method