VBA ActiveDocument问题/替代方案? [英] VBA ActiveDocument Concerns / Alternatives?

查看:483
本文介绍了VBA ActiveDocument问题/替代方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在访问中使用VBA来打开受保护的单词模板,填写数据,然后对其进行重新保护....这样,如果数据库系统出现故障,则仍然可以使用单词模板

I'm using VBA in access to open up a protected word template, fill in the data, and then re-protect it.... this way, if the database system goes down, the word template can still be used manually in its protected state.

我刚刚开始使用VBA,并且在这一行中:

I have just started using VBA and in this line:

If ActiveDocument.ProtectionType <> wdNoProtection Then
    ActiveDocument.Unprotect Password:=""
End If

I我担心在访问中运行代码时,如果用户打开另一个Word文档并使其成为焦点,那么它将无意间受到保护,而不是另一个。如何保持对正在编写的文档的积极关注...或者我需要使用WordApp.protect(或类似的类似方法)以某种方式引用我的文档

I'm concerned that whilst running the code in access, that if the user opens up another word document and makes it the focus, that it will occidentally get protected instead of the other. How do I keep active focus on the document I'm writing to... or do I need to reference my document somehow using WordApp.protect (or something similar that works)

Private Sub Command0_Click()
Dim WordApp As Word.Application
Dim strDatabasePath As String
Dim strTemplatePath As String
Dim strTemplate As String
Dim strJobTitle As String
Dim strFile As String

strFile1 = "testcoc.dotx"
strFile2 = "testcoc-private.dotx"
strDatabasePath = CurrentProject.Path & "\"
strTemplatePath = "\templates\"
strTemplate = strDatabasePath & strTemplatePath & strFile2


On Error Resume Next
Set WordApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
    Set WordApp = CreateObject("Word.Application")
End If
On Error GoTo ErrHandler


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


'strJobTitle = DLookup("JobTitle", "Job", "JobNum = " & [JobType])
strJobTitle = DLookup("JobTitle", "Job", "JobNum = 'J0456'")

With WordApp.Selection

'Unprotect the file
If ActiveDocument.ProtectionType <> wdNoProtection Then
    ActiveDocument.Unprotect Password:=""
End If

    .Goto what:=wdGoToBookmark, Name:="bm_0_4"
    .TypeText strJobTitle


End With

'Reprotect the document.
'If ActiveDocument.ProtectionType = wdNoProtection Then
    'ActiveDocument.Protect _
    'Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""
'End If


DoEvents
WordApp.Activate
Set WordApp = Nothing
Exit Sub


ErrHandler:
Set WordApp = Nothing
End Sub

谢谢

推荐答案

我还没有尝试过,但是 WordApp.Documents.Add Template:= strTemplate,NewTemplate:= True 确实返回了新文档。所以我会做类似的事情

I haven't tried this but WordApp.Documents.Add Template:=strTemplate, NewTemplate:=True does return the new document. So I would do something like

Dim doc as Word.Document
Set doc = WordApp.Documents.Add(Template:=strTemplate, NewTemplate:=True)

并引用 doc 而不是 ActiveDocument 。看来这样做应该可以帮助您避免担心的特定情况。

and reference doc throughout my code instead of ActiveDocument. It seems like doing that should get help you avoid the particular situation you're concerned about.

这篇关于VBA ActiveDocument问题/替代方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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