不同的格式合并为一行 Interop.word
我一直在尝试弄清楚如何使用 c# 中的 interop.word 将 2 种不同的格式插入到同一个段落中,如下所示:
hello Planet earth 这是我想要做的
I've been trying to figure out how to insert 2 different formats into the same paragraph using interop.word in c# like this:
hello planet earth here's what I want to do
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
假设您将文档定义为 oDoc,以下代码应该会获得所需的结果:
Assuming you have your document defined as oDoc, the following code should get you the desired result:
我必须稍微修改丹尼斯的答案才能使其对我有用。
我正在做的事情是完全自动化的,所以我只需要使用变量。
让我想发表这篇文章的主要区别是应该在更改字体后插入段落。我最初的想法是在设置 SpaceAfter 属性后插入它,但随后 objStart 和 objEnd 值抛出“OutOfRange”异常。这有点违反直觉,所以我想确保每个人都知道。
I had to modify Dennis' answer a little to get it to work for me.
What I'm doing it totally automated, so I have to only work with variables.
The main difference that made me want to make this post was that the Paragraph should be inserted AFTER the font is changed. My initial thought was to insert it after setting the
SpaceAfter
property, but then theobjStart
andobjEnd
values were tossing "OutOfRange" Exceptions. It was a little counter-intuitive, so I wanted to make sure everyone knew.在格式化段落中的特定选择时,以下代码似乎最适合我。使用Word 内置的“查找”功能进行选择,然后仅设置所选文本的格式。仅当要选择的文本是所选内容中的唯一字符串时,此方法才有效。但对于我遇到的大多数情况来说,这似乎是有效的。
希望这对某人有帮助!
The following code seemed to work the best for me when formatting a particular selection within a paragraph. Using Word's built in "find" function to make a selection, then formatting only the selected text. This approach would only work well if the text to select is a unique string within the selection. But for most situations I have run across, this seems to work.
Hope this helps someone!
我知道这篇文章很旧,但它几乎出现在我所有的搜索中。下面的答案是为了防止像我这样的人想要对句子中的多个单词执行此操作。在本例中,我循环遍历包含字符串的变量字符串数组,并将该文本更改为粗体 -modifing @joshman1019
因此,该变量表示的每个字符串都将是粗体。因此,如果
a = "hello world"
,则 Hello World 在 Word 文档中会变为粗体。希望它可以节省一些时间。I know this post is old, but it came out in almost all my searches. The answer below is in case someone, like me, wants to do this for more than one word in a sentence. In this case, I loop through a string array of variables that contain strings and change that text to bold--modifing @joshman1019
So, each string represented by the variable will be bold. So if
a = "hello world"
, then Hello World is made bold in the Word doc. Hope it saves someone some time.最终考虑使用
Range.Collapse
并将Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd
作为参数。这将允许下一个文本的格式与前一个文本不同(并且下一个文本格式不会影响前一个文本的格式)。
Consider usage of
Range.Collapse
eventually withMicrosoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd
as parameter.That would allow next text to have formatting different than previous text (and next text formatting will not affect formatting of previous one).
我知道这是一个旧线程,但我想无论如何我都会在这里发布给那些通过谷歌遇到它的人(就像我所做的那样)。我用 krillgar 的方法找到了大部分解决方案,但我遇到了麻烦,因为我的一些文本包含换行符。因此,这种修改对我来说效果最好:
关键的区别是我在函数中更早地计算开始和结束。我不太清楚,但我认为如果您的新文本中有换行符,则稍后开始/结束的计算会弄乱一些东西。
显然,我的解决方案适用于格式如下的文本:
标签:数据
,其中标签要加粗。
I know this is an old thread, but I thought I'd post here anyway for those that come across it via Google (like I did). I got most of the way to a solution with krillgar's approach, but I had trouble because some of my text contains newlines. Accordingly, this modification worked best for me:
The key difference is that I calculate start and end earlier in the function. I can't quite put my finger on it, but I think if your new text has newlines in it, the later calculation of start/end messes something up.
And obviously my solution is intended for text with the format:
Label: Data
where Label is to be bolded.