如何编辑只读Word文档(VBA) [英] How to Edit a Read-Only Word Document (VBA)

查看:611
本文介绍了如何编辑只读Word文档(VBA)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会定期从各个客户端获取Word文档,有时它们会以只读"模式将它们发送给我.手动进入查看>编辑文档"并不是什么大问题,但我似乎找不到在VBA代码中执行此操作的方法.

I am periodically getting Word documents from various clients and sometimes they send them to me in 'Read-Only' mode. While it isn't a big deal to go to 'View > Edit Document' manually, I cannot seem to find how to do this within my VBA code.

打开文档或将其切换为可编辑就足够满足我的需要.

Either opening a document as editable or toggling it as editable once it is open would be sufficient for my needs.

请注意,由于它似乎设置为建议只读"(基于我对Document.Open的MS手册页的阅读),因此无法使用"readOnly = false"打开文档.

Note that I cannot open the document with 'readOnly = false' as it looks like it is set to 'readOnly recommended' (based on my reading of the MS man page on Document.Open).

在上下文中: 我也遇到了关闭读取模式"的问题,该模式默认情况下会打开文档.我已发布此问题并回答

IN CONTEXT: I was also hitting a problem with turning off 'read-mode' which the documents were opening as by default. I have posted this question and answer here.

推荐答案

下面的代码将更改已关闭文件的ReadOnly属性,具体取决于提供给过程的参数,将其ReadOnly属性设置为True或False. >

The code below will change the ReadOnly attribute of a closed file, setting its ReadOnly attribute to either True or False depending on the argument supplied to the procedure.

Private Sub SetReadOnlyProperty(Fn As String, _
                                ByVal ReadOnly As Boolean)
    ' 21 Nov 2017

    Dim Fso As Object
    Dim Doc As Object

    Set Fso = CreateObject("Scripting.FileSystemObject")
    Set Doc = Fso.GetFile(Fn)

    If (Doc.Attributes And vbReadOnly) <> Abs(Int(ReadOnly)) Then
        Doc.Attributes = Doc.Attributes Xor vbReadOnly
    End If
End Sub

此过程需要访问MS脚本运行时DLL.通过在VBE窗口的工具">参考"中选中Miscrosoft Scripting Runtime框,启用此访问权限.以下是如何调用该函数的示例.请注意,如果提供的文件不存在,将导致错误.

This procedure requires access to the MS Scripting Runtime DLL. Enable this access by checking the box against Miscrosoft Scripting Runtime from Tools >References in the VBE window. Below is an example of how to call the function. Note that an error will result if the supplied file doesn't exist.

Private Sub TestReadOnly()
    SetReadOnlyProperty "H:\Test Folder\Test File.docx", False
End Sub

这篇关于如何编辑只读Word文档(VBA)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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