在 MS Word 2007 中使用 VBA 定义页面元素?

发布于 2024-08-19 06:30:07 字数 506 浏览 1 评论 0原文

我希望能够创建一个页面元素,我可以在其中输入文本,并且它将自行形成首选布局。例如:

{MACRO DocumentIntro("Introduction to Business Studies", "FP015", "Teachers' Guide")}

将其作为字段,输出应该是一行,前两个字符串具有一定的大小和字体,居中,另一行,然后第三个字符串具有字体、大小和居中。

我知道这有点像 TeX,可能超出了 VBA 的范围,但如果有人知道它是如何实现的,请告诉我!

编辑

好的,如果我将所需的信息作为文档属性的一部分放入关键字中,并使用某种唯一的分隔符,那么就会获取该信息,并且该信息对于每个文档都是唯一的。下一步是在要显示内容的地方放置一个书签。然后创建一个 AutoOpen 宏,该宏转到该书签,从关键字中提取相关内容,并将文本适当地形成到书签的 .Selection 中。

这可行吗?

I'd like to be able to create a page element which I can feed text and it will form itself into the preferred layout. For instance:

{MACRO DocumentIntro("Introduction to Business Studies", "FP015", "Teachers' Guide")}

with that as a field, the output should be a line, the first two strings a certain size and font, centred, another line and then the third string fonted, sized and centred.

I know that's sort of TeX-like and perhaps beyond the scope of VBA, but if anyone's got any idea how it might be possible, please tell!

EDIT:

Ok, if I put the required information into Keyword, as part of the document properties, with some kind of unique separator, then that gets that info in, and the info will be unique to each document. Next one puts a bookmark where the stuff is going to be displayed. Then one creates an AutoOpen macro that goes to that bookmark, pulls the relevants out of the keywords, and forms the text appropriately into the bookmark's .Selection.

Is that feasible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小镇女孩 2024-08-26 06:30:07

对于编码解决方案,您肯定走在正确的道路上。但是,有一种无需代码的简单方法 - Word 2007 中的内容控件就是针对这种情况而构建的,并且通过字段/属性,您可以绑定到内容控件 (CC)。这些 CC 可以保存样式(如居中、粗体等)。无需 VBA。

最简单的方法是选择 3 个您始终希望使用的内置文档属性。例如,“标题”可能是您的第一个字符串,“主题”是您的第二个字符串,“关键字”是您的第三个字符串。然后,只需转到插入功能区、快速部件文档属性,然后按照您喜欢的方式插入、放置和设置它们的格式。然后转到 Word 的开始按钮(球形物体),然后在准备下选择属性。在这里,您可以在标题框中键入“商业研究简介”,然后以某种方式取消选择它(例如单击另一个框)。 标题的内容控件将自动填充您的文本。

如果您想将其用于多个文件,只需将此文件创建为 .dotx(在 CC 插入/放置/格式化之后和更新文档属性文本之前)。然后每次您所要做的就是为每个新文件设置这三个属性。

You're certainly on the right track here for a coding solution. However, there is a simpler way with no code - this is the type of scenario that Content Controls in Word 2007 were built for and with Fields/Properties, you can bind to content controls (CC). These CC can hold styles (like centered, bold, etc.). No VBA required.

The very easiest thing to do is to pick 3 built-in document properties that you will always want these to be. For example, "Title" could be your first string, "Subject" your second string and "Keywords" your third. Then, just go to the Insert ribbon, Quick Parts, Document Properties and insert, place and format those how you like. Then go to Word's start button (the orb thingy) and then under Prepare choose Properties. Here you can type, for example "Introduction to Business Studies", into the Title box and then just deselect it somehow (like click in another box). The Content Control for Title will be filled in automatically with your text.

If you want to use this for multiple files, just create this file as a .dotx (after CC insertion/placement/formatting and before updating the Document Properties' text). Then every time all you'll have to do is set these three properties with each new file.

墨小墨 2024-08-26 06:30:07

嗯,是的,事实证明这是可行的。

Sub autoopen()
    Dim sKeywords As String
    sKeywords = ActiveDocument.BuiltInDocumentProperties(4)
    ActiveDocument.Bookmarks("foo").Select
    Selection.Text = sKeywords
End Sub

好吧,我还有一些事情要做,但至少它的内容已经存在了。

Well, yes, it did turn out to be feasible.

Sub autoopen()
    Dim sKeywords As String
    sKeywords = ActiveDocument.BuiltInDocumentProperties(4)
    ActiveDocument.Bookmarks("foo").Select
    Selection.Text = sKeywords
End Sub

Okay, I have some filling out to do, but at least the guts of it are there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文