Word Automation:不可编辑

时间:2011-06-29 17:48:21

标签: ms-access vba automation ms-word ms-office

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

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

这是我用来进行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

2 个答案:

答案 0 :(得分:1)

Document对象具有Saved属性,如果进行了任何更改,通常会更改为False。如果您将此属性设置为True,那么当您关闭文档时,系统不会提示您保存文档,但如果您需要,仍然可以手动保存文档(通过SaveSave As...)到。

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

答案 1 :(得分:0)

这应该做你需要的。从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
相关问题