让我开始编程和调试 Microsoft Office 自动化

发布于 2024-08-10 01:13:26 字数 1402 浏览 2 评论 0原文

我正在使用 Microsoft Office 2003 并创建一堆模板文档来标准化某些任务。我在 Superuser.com 上问了这个问题没有得到回应,所以我认为这太程序化了,希望我在这里能有更好的运气。

我需要自动化使用一堆 Office(主要是 Word)模板的工作流程。我想要的是在共享驱动器上的“My Foo Bar Stuff”中拥有“My Template Foo.dot”和“My Template Bar.dot”等,并让用户双击模板来创建新的 Foo 或酒吧。

我真正想要的是用户双击 Foo 模板,并提示输入与他们的任务相关的几个项目(例如项目编号),并让模板中的脚本更改“保存”将的名称默认为“Foo for Project 1234.doc”。

我在 Google 网上论坛< /a> 并得到了一个有效的答案......一段时间。然后,当我通过双击模板创建新文档时,我的 AutoNew 宏停止运行。我不知道为什么或如何调试它。

在类模块/此应用程序中,我有:

Sub AutoNew()
    Dim Project As String
    Project = InputBox("Enter the Project Number")
    ActiveDocument.SaveAs "Project " & Project & " Notes.doc"
End Sub

在 Microsoft Word 对象/ThisDocument 中,我有:

Private Sub Document_New()

End Sub

我真的不知道为什么或从哪里来。

在“工具/宏安全性”中...我将安全级别设置为“低”。

我是一名拥有 25 年以上经验的软件工程师,但完全是办公自动化菜鸟。欢迎提供具体的解决方案和“这就是如何自动化 Word”常见问题解答。谢谢。


更新:如果我创建一个新模板(新建...,空白文档,另存为“My New Template.dot”),并插入 AutoNew() 宏,有用。那么是什么阻碍了它在我现有的模板上工作呢?

更新 2: 从旧模板中删除模块和功能并将其添加回来也可以。

I'm using Microsoft Office 2003 and creating a bunch of template documents to standardize some tasks. I asked this on Superuser.com and got no response so I'm thinking it's too program-y and hoping I'll have better luck here.

I need to automate a work flow that uses a bunch of Office (mostly Word) templates. What I want is to have "My Template Foo.dot" and "My Template Bar.dot", etc. in the "My Foo Bar Stuff" on a shared drive and have users double click on a template to create a new Foo or Bar.

What's I'd really like is for the user to double-click on the Foo template and be prompted for a couple of items related to their task (e.g., a project number) and have a script in the template change the name that Save will default to something like "Foo for Project 1234.doc".

I asked on Google Groups and got an answer that worked....for a while. Then my AutoNew macro stopped kicking in when I created a new document by double-clicking on the template. I have no idea why or how to debug it.

In Class Modules/This Application, I have:

Sub AutoNew()
    Dim Project As String
    Project = InputBox("Enter the Project Number")
    ActiveDocument.SaveAs "Project " & Project & " Notes.doc"
End Sub

In Microsoft Word Objects/ThisDocument, I have:

Private Sub Document_New()

End Sub

I really have no idea why or where that came from.

In Tools/Macro Security... I have Security Level set to "Low".

I'm a software engineering with 25+ years of experience but a complete Office automation noob. Specific solutions and pointers to "this is how to automate Word" FAQs are welcome. Thanks.


Update: If I create a new template (New..., Blank Document, Save As "My New Template.dot"), and insert the AutoNew() macro, it works. So what's inhibiting it from working on my existing template?

Update 2: Removing the module and function from my old template and adding it back works, too.

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

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

发布评论

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

评论(2

木格 2024-08-17 01:13:26

您可以将模板附加到已保存的文档中,以便访问相关模板中包含的宏。

您可以使用 Document 对象(即 ActiveDocument)的 AttachedTemplate 属性来完成此操作。
请注意,我自己没有尝试过。

Sub AutoNew()
   Dim Project As String
   Project = InputBox("Enter the Project Number")
   ActiveDocument.SaveAs "Project " & Project & " Notes.doc"

   ActiveDocument.AttachedTemplate = "\\path\to\templates\My Template Foo.dot"

End Sub

请参阅 MSDN - Word 2003 VBA 语言参考 - AttachedTemplate 属性< /a>

希望有帮助。

You can attach a template to a saved document in ordre to access the macros contained in the template in question.

You can do this with the AttachedTemplate property of a Document object (i.e. ActiveDocument).
Please note that I did not try this myself.

Sub AutoNew()
   Dim Project As String
   Project = InputBox("Enter the Project Number")
   ActiveDocument.SaveAs "Project " & Project & " Notes.doc"

   ActiveDocument.AttachedTemplate = "\\path\to\templates\My Template Foo.dot"

End Sub

See MSDN - Word 2003 VBA Language Reference - AttachedTemplate Property

Hope that helps.

沉默的熊 2024-08-17 01:13:26

检查该宏是否仍包含在您的模板中。这听起来很愚蠢,但它也发生在我身上,在 Word 2003 中,在以下情况下:

  1. 创建一个包含宏的模板,
    一切都很好
  2. 创建一个新的文档文件基于
    宏,宏开始
  3. 注意我可以改进模板
    到处做一点,我在
    2) 中创建的文件和
  4. .DOT 中的 SaveAs .DOT 宏消失了!

为什么?因为存储在 .DOT 中的代码不会转到 doc 文件。

Check if the macro is still contained in your template. This sounds stupid but it happended to me, too, in Word 2003 under the following circumstances:

  1. Create a template containing macro,
    everything fine
  2. Create a new document file based on
    the macro, macro kicks in
  3. Notice I could improve the template
    here and there a bit, I do it in the
    file created in 2) and SaveAs .DOT
  4. Macro in .DOT GONE!

Why? Because the code stored in the .DOT doesn't go over to the doc file.

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