DOC+VBA
폴더내 모든 워드문서의 읽기전용 속성 해제
쵸코난
2024. 2. 1. 16:42
여기서 읽기전용이라 함은 워드문서를 열고 파일 > 정보에서 항상 읽기 전용으로 열기를 설정한 상태를 말합니다.
이 경우 항상 파일을 열 때 아래처럼 읽기 전용을 해제해야 편집이 가능합니다.
이러한 읽기전용으로 표시된 파일이 여러 개인 경우 일괄로 해제하는 매크로입니다.
1. 첨부 파일 다운로드 후 파일속성> 차단해제 > 확인, 파일 열 때 매크로 허용.
2. Alt-F8로 ReadOnlyOff 매크로실행
3. 읽기 전용 워드 문서가 들어 있는 폴더 안에 들어가서 ‘확인’ 버튼 클릭
4. 완료
첨부파일을 차단해제 체크하고 확인한 후에 매크로 허용해서 열고
Alt-F8을 눌러서 ReadOnlyOff 매크로를 실행하고
워드 문서들이 들어 있는 폴더 안에 들어가서 '확인'버튼을 누르면
읽기전용 권장 속성을 해제하고 다른 이름으로 저장한 다음 다시 원래 이름으로 복구합니다.
Ctrl-G 디버그창에 아래와 같은 결과 메시지가 뜹니다.
Target Folder : C:\Users\사용자이름\Downloads\새 폴더
문서1 - 복사본 - 복사본.docx True => False 문서1 - 복사본.docx False => False 문서1.docx True => False |
코드:
Option Explicit
Sub ReadOnlyOff()
Dim sFile As String, sPath As String, Spr As String
Dim Doc As Document, tDoc As Document, i As Integer
Dim td As Object
Spr = Application.PathSeparator
Set Doc = ActiveDocument
'폴더 선택
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = Doc.Path & Spr
.ButtonName = "워드파일이 있는 폴더로 들어간 후 클릭!"
If .Show = -1 Then
sPath = .SelectedItems(1)
End If
End With
If sPath = "" Then Exit Sub
Debug.Print "Target Folder : "; sPath
sFile = Dir(sPath & Spr & "*.doc?")
While Len(sFile) > 0
If Doc.Name <> sFile Then
Set tDoc = Documents.Open(FileName:=sPath & Spr & sFile, ReadOnly:=False, Visible:=True)
'Application.WordBasic.fileopen sPath & Spr & sFile
'Set tDoc = Documents(1)
Debug.Print sFile, tDoc.ReadOnlyRecommended;
If tDoc.ReadOnlyRecommended Then
tDoc.ReadOnlyRecommended = False
tDoc.SaveAs2 FileName:=sPath & Spr & "T_" & sFile
Debug.Print " => "; tDoc.ReadOnlyRecommended
tDoc.Close
'Kill sPath & Spr & sFile
Name sPath & Spr & sFile As sPath & Spr & "X_" & sFile
Name sPath & Spr & "T_" & sFile As sPath & Spr & sFile
i = i + 1
End If
End If
sFile = Dir
Wend
If i Then MsgBox i & "개의 문서 처리 완료"
End Sub
읽기전용인 경우 VBA에서 이 속성을 해제하고 저장할 수 없어서
일단 다른 이름으로 저장한 후에 다시 원래 이름으로 복구하는 방법을 사용합니다.
현재는 기존 파일은 X_기존파일명으로 백업하는데 삭제하려면 주석(Kill 부분 )을 수정하세요.
첨부파일 참고하세요.
관련: 지식인