Word 自动化:不可编辑

发布于 2024-11-18 00:29:28 字数 833 浏览 3 评论 0原文

我正在使用 Access 将数据发送到我在 Word 中创建的模板。成功发送数据后,我需要使打开的 Word 文档不可编辑。

另外,我注意到在完成文档后它会提示保存。是否可以删除此提示,但允许保存功能。

这是我用来执行 Word 自动化的代码:

' Create a Word document from template.
Dim WordApp As Word.Application
Dim strTemplateLocation As String
Dim myVariable As String
myVariable = “TEST!!”

' Specify location of template
strTemplateLocation = Left(CurrentDb.Name, InStrRev(CurrentDb.Name, "\")) & "test.dot"

Set WordApp = CreateObject("Word.Application")

WordApp.Visible = True
WordApp.WindowState = wdWindowStateMaximize
WordApp.Documents.Add Template:=strTemplateLocation, NewTemplate:=False


' Replace each bookmark with field contents.
WordApp.Selection.GoTo what:=wdGoToBookmark, Name:="myBookmark"
WordApp.Selection.TypeText myVariable

DoEvents
WordApp.Activate
Set WordApp = Nothing

I am using Access to send data to a template I created in Word. After it succesfully sends the data I need to make the open Word Document NON-editable.

Also, I notice that after I am done with the Document it prompts to save. Is it possible to remove this prompt, BUT allow the capability to save.

This is the code I am using to do the Word Automation:

' Create a Word document from template.
Dim WordApp As Word.Application
Dim strTemplateLocation As String
Dim myVariable As String
myVariable = “TEST!!”

' Specify location of template
strTemplateLocation = Left(CurrentDb.Name, InStrRev(CurrentDb.Name, "\")) & "test.dot"

Set WordApp = CreateObject("Word.Application")

WordApp.Visible = True
WordApp.WindowState = wdWindowStateMaximize
WordApp.Documents.Add Template:=strTemplateLocation, NewTemplate:=False


' Replace each bookmark with field contents.
WordApp.Selection.GoTo what:=wdGoToBookmark, Name:="myBookmark"
WordApp.Selection.TypeText myVariable

DoEvents
WordApp.Activate
Set WordApp = Nothing

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

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

发布评论

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

评论(2

姐不稀罕 2024-11-25 00:29:28

Document 对象有一个 Saved 属性,如果进行任何更改,该属性通常会更改为 False。如果您将此属性设置为 True,则关闭文档时不会提示您保存文档,但您仍然可以手动保存它(通过 Save另存为...)如果你愿意的话。

您可以使用 Document 对象的 Protect 方法来限制用户可以进行的更改。例如,您可以使用参数 wdAllowOnlyReading 来调用它,这意味着不能进行任何类型的更改。您可能还需要查看保护文档的密码,以防止用户再次简单地取消保护它

The Document object has a Saved property which normally changes to False if any changes are made. If you set this property to True then you won't be prompted to save the document when you close it but you can still save it manually (via Save or Save As...) if you want to.

You can use the Protect method of the Document object to restrict the changes which the user can make. For example, you can call it with the parameter wdAllowOnlyReading which will mean that no changes of any kind can be made. You may also need to look at password protecting the document to prevent the user from simply unprotecting it again

久隐师 2024-11-25 00:29:28

这应该可以满足您的需要。从 Excel 测试,而不是 Access,因此您需要修复模板路径

Sub Tester()
    ' Create a Word document from template.
    Dim WordApp As Word.Application
    Dim strTemplateLocation As String
    Dim myVariable As String

    myVariable = "TEST!!"
    ' Specify location of template
    strTemplateLocation = ThisWorkbook.Path & "\test.dotx"
    Set WordApp = CreateObject("Word.Application")
    With WordApp
        .Visible = True
        .WindowState = wdWindowStateMaximize
        .Documents.Add Template:=strTemplateLocation, NewTemplate:=False

        ' Replace each bookmark with field contents.
        .Selection.GoTo what:=wdGoToBookmark, Name:="myBookmark"
        .Selection.TypeText myVariable

        DoEvents

        With .ActiveDocument
            .Protect Type:=wdAllowOnlyReading, Password:="blah"
            .Saved = True
        End With

        .Activate
    End With

    Set WordApp = Nothing

End Sub

This should do what you need. Tested from Excel, not Access, so you'll need to fix the template path

Sub Tester()
    ' Create a Word document from template.
    Dim WordApp As Word.Application
    Dim strTemplateLocation As String
    Dim myVariable As String

    myVariable = "TEST!!"
    ' Specify location of template
    strTemplateLocation = ThisWorkbook.Path & "\test.dotx"
    Set WordApp = CreateObject("Word.Application")
    With WordApp
        .Visible = True
        .WindowState = wdWindowStateMaximize
        .Documents.Add Template:=strTemplateLocation, NewTemplate:=False

        ' Replace each bookmark with field contents.
        .Selection.GoTo what:=wdGoToBookmark, Name:="myBookmark"
        .Selection.TypeText myVariable

        DoEvents

        With .ActiveDocument
            .Protect Type:=wdAllowOnlyReading, Password:="blah"
            .Saved = True
        End With

        .Activate
    End With

    Set WordApp = Nothing

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