VBA将文本附加到书签 [英] VBA append text to bookmark

查看:78
本文介绍了VBA将文本附加到书签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我在VBA中编码创建以从Userform中的用户输入撰写法律文档。现在我需要将其格式化为一个很好的Word 2010文档。我想使用书签。



然而这让我疯狂:



使用ActiveDocument

.Bookmarks.Add(test)

。书签(test)范围=A

。书签(测试 ).Range.InsertAfterB

。书签(测试)。Range.InsertAfterC

结束如果



生成的文本始终显示为CBA我需要它在书签Test中为ABC。恢复线不是一种选择。由于其复杂性,文档需要按逻辑顺序组成。出于同样的原因,我希望将书签的数量保持在最低限度,并且在我将它们放入文档之前,喜欢从文本/输入文件中预先组合书签。



任何帮助都将受到高度赞赏。在此先感谢。

Hello,

I'm coding in VBA to create to compose a legal document from user input in a Userform. Now I need to format this into a nice Word 2010 document. I'd like to use bookmarks.

However this is driving me nuts:

With ActiveDocument
.Bookmarks.Add ("test")
.Bookmarks("test") Range = "A"
.Bookmarks("test").Range.InsertAfter "B"
.Bookmarks("test").Range.InsertAfter "C"
End If

The generated text is always displayed as "CBA" I need it to be "ABC" in bookmark "Test". Reverting the lines is not an option. The document needs to be composed in a logical order, due to its complexity. For the same reason I like to keep the number of bookmarks in use to a minimum, and like to "pre-assemble" bookmarks from pieces of text / input, before I put them into the document.

Any help will be highly appreciated. Thanks in advance.

推荐答案

书签范围无法扩展。 Range.InsertAfter 的文档说明了这一点。因此,您必须创建一个在书签范围之后立即开始的新范围。创建新范围,使其成为要添加到范围的第一个文本的长度。在此测试用例中,在 Set rngNewRange 语句中添加了一个。



A bookmark Range cannot be extended. The documentation for Range.InsertAfter says that. Therefore, you have to create a new range that starts immediately after the bookmark Range. The new range is created so that it is the length of the first text that is to be added to the range. In this test case, a one is added in the Set rngNewRange statement.

Dim rngNewRange As Range ' Range that starts at point immediately after the bookmark
With ActiveDocument
    .Bookmarks.Add ("test") ' Create Bookmark
    '
    ' Set a new range that starts at the bookmark and is one character long
    Set rngNewRange = .Range(Start:=.Bookmarks("test").Range.End, End:=.Bookmarks("test").Range.End + 1)
End With
rngNewRange = "A" ' Add one character
rngNewRange.InsertAfter "B" ' Add another character
rngNewRange.InsertAfter "C" ' Add another character





秒示例



A second example

Sub x()
Dim rngNewRange As Range ' Range that starts at point immediately after the bookmark
With ActiveDocument
    .Bookmarks.Add ("test") ' Create Bookmark
    '
    ' Set a new range that starts at the bookmark and is six characters long
    Set rngNewRange = .Range(Start:=.Bookmarks("test").Range.End, End:=.Bookmarks("test").Range.End + 6)
End With
rngNewRange = "Hello " ' Add six characters
rngNewRange.InsertAfter "my name is " ' Add 11 characters
rngNewRange.InsertAfter "Mike!" ' Add 5 characters
End Sub





使用Word 2010测试


谢谢Mike!



您的解决方案正常。我仍然有一些问题,为什么VBA代码必须如此丑陋和不合逻辑,但它现在会做。



似乎(我还没有线索)为什么)+1是不必要的,范围自动适应正确的长度。



这是我使用的代码:



Thanks Mike!

Your solution works fine. I still have some questions about why VBA code has to be so ugly and unlogical but it'll do for now.

It seems (I haven't got a clue why) that the "+1" is unnecessary, the range automagically adapts to the right length.

This is the code I went with:

Dim r As Range
With ActiveDocument
    .Bookmarks.Add ("test")
    Set r = .Range(Start:=.Bookmarks("test").Range.End, End:=.Bookmarks("test").Range.End)
    r.InsertAfter "Aaa"
    r.InsertAfter "Beee"
    r.InsertAfter = "Cee"
End With





如果有人对Word文档的结构有一个很好的指针(其中)导航概念,即什么和为什么是范围),然后我很高兴收到一些指示。



If anybody has a good pointer about the structure of Word documents (its navigation concepts, i.e. what and why is a "Range"), then I'm happy to receive some pointers.


这篇关于VBA将文本附加到书签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆