vba 在excel中visual basic创建的word文档中设置语言、文字方向和对齐方式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6330330/
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 13:27:41  来源:igfitidea点击:

Setting language, text direction and alignment in word document created by visual basic in excel

vbaexcel-2003bidirectionalword-2003

提问by Ernst

I've made an excel visual basic script that takes data from an excel sheet, and produces a paragraph in a word sheet for each row. The default language for the document is Hebrew, with text aligned right and direction right to left. For one (the last) line in the paragraph, I want to set the language to English, the direction left to right, and the alignment right. Then, for the first line in the next paragraph change back to Hebrew, direction right to left and alignment right. When recording a macro in word when being on the last line of a paragraph, pressing home, shift end, clicking the icons on the toolbar to change language, direction and alignment as wanted, I get:

我制作了一个 excel 可视化基本脚本,它从 excel 工作表中获取数据,并在单词表中为每一行生成一个段落。文档的默认语言是希伯来语,文本右对齐,方向从右到左。对于段落中的一行(最后一行),我想将语言设置为英语,方向从左到右,对齐方式为右。然后,将下一段中的第一行改回希伯来语,从右到左,右对齐。当在段落的最后一行在 word 中录制宏时,按 home,shift end,单击工具栏上的图标以根据需要更改语言、方向和对齐方式,我得到:

Sub test()
    Selection.HomeKey Unit:=wdLine
    Selection.EndKey Unit:=wdLine, Extend:=wdExtend
    Application.Keyboard (2057)
    Selection.LtrPara
    Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
End Sub

One thing that surprised me is that changing the language is recorded as if I was using the keyboard, instead of being a property of the object, just like direction and alignment. How do I translate this to excel visual basic? The line in question is currently added by using:

令我感到惊讶的一件事是,更改语言的记录就像我在使用键盘一样,而不是对象的属性,就像方向和对齐一样。我如何将其转换为excelvisual basic?有问题的行目前是通过使用添加的:

With f
    .Content.InsertAfter Format(a, "standard") & " x " & Format(b, "#,##0.000") & " x " & Format(c, "#,##0.000") & " / " & Format(d, "#,##0.000") & " = " & Format(e, "standard")
    .Content.InsertParagraphAfter
End With

Thanks,

谢谢,

Ernst

恩斯特

回答by Ernst

Okay, I've got a workaround, this is only a partial answer. This is not a solution in Excel visual basic, but one in Word:

好的,我有一个解决方法,这只是部分答案。这不是 Excel Visual Basic 中的解决方案,而是 Word 中的解决方案:

Search for all occurences of ^13[!^13]@x*^13and then do the language, alignment and direction change. When recording it as a macro in word, I get the following:

搜索所有出现的^13[!^13]@x*^13,然后进行语言、对齐和方向更改。在 Word 中将其记录为宏时,我得到以下信息:

Sub Macro1()

Selection.Find.ClearFormatting
With Selection.Find
    .Text = "^13[!^13]@x*^13"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchKashida = False
    .MatchDiacritics = False
    .MatchAlefHamza = False
    .MatchControl = False
    .MatchAllWordForms = False
    .MatchSoundsLike = False
    .MatchWildcards = True
End With
Selection.LtrPara
Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
Selection.Find.ClearFormatting
With Selection.Find
    .Text = "^13[!^13]@x*^13"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchKashida = False
    .MatchDiacritics = False
    .MatchAlefHamza = False
    .MatchControl = False
    .MatchAllWordForms = False
    .MatchSoundsLike = False
    .MatchWildcards = True
End With

End Sub

结束子

But when running this on a freshly generated Word document, this does not do what it's supposed to do. Any ideas for a workaround on that? Btw, can I add and run a word macro to a document generated by excel vb using excel vb?

但是当在新生成的 Word 文档上运行它时,这不会做它应该做的事情。关于解决方法的任何想法?顺便说一句,我可以在使用 excel vb 的 excel vb 生成的文档中添加和运行 word 宏吗?