VBA Word - 更改小数点分隔符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16191557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 20:48:43  来源:igfitidea点击:

VBA Word - changing decimal separator

vbams-wordword-vba

提问by Kris_R

Some people around me using national localization in windows/office. Unfortunately this leads to situation where my macros fail to do simple math as they using comma for decimal position and the pov-ray files, that I editing, use comma only for list separator and point for decimal one. I know that in Excel one can override system decimal separator using

我周围的一些人在 windows/office 中使用国家本地化。不幸的是,这导致我的宏无法做简单的数学运算,因为它们使用逗号作为小数点位置,而我编辑的 pov-ray 文件仅使用逗号作为列表分隔符,点作为小数点。我知道在 Excel 中可以使用覆盖系统小数点分隔符

With Application.
    .DecimalSeparator = "."
    .ThousandsSeparator = ","
    .UseSystemSeparators = False
End With

There is however no such property of Application like DecimalSeparator in MS Word. Is there in MS Word a simple way to overwrite system separator for the time a macros is running? Or do I need to go hard way and replace them during reading of povray files?

然而,在 MS Word 中没有像 DecimalSeparator 这样的应用程序属性。MS Word 中是否有一种简单的方法可以在宏运行时覆盖系统分隔符?或者我是否需要在阅读 povray 文件期间努力更换它们?

采纳答案by Kris_R

Being not very patient I did it the "harder way". I declared public variable

由于不是很有耐心,我以“更难的方式”做到了。我声明了公共变量

Public strDecimal As String

and then on the beginning of main sub I set its value with:

然后在 main sub 的开头,我将其值设置为:

strDecimal = Application.International(wdDecimalSeparator)

Then in source I replaced all read-ins like

然后在源代码中,我替换了所有读入内容,例如

CDbl(strShort(5))

with

CDbl(Replace(strShort(5), ".", strDecimal))

and all write-outs like

和所有写出像

Selection.TypeText Text:=CStr(Int(1000 * bondRadius) / 1000)

with

Selection.TypeText Text:=CStr(Replace(Int(1000 * bondRadius) / 1000, strDecimal, "."))

It's not as elegant as overwriting systems setting but it took me not too much time and it works for me. If anyone can see reason, why it should on occasion fail, please leave some comments.

它不像覆盖系统设置那么优雅,但它花费了我不太多的时间并且对我有用。如果有人可以看到原因,为什么它偶尔会失败,请留下一些评论。

I'll play a bit with info from barrowc's comment (thanx) to see if it's read only or read-write value.

我将使用 barrowc 的评论 (thanx) 中的信息来查看它是只读还是读写值。