打开密码保护的文件

时间:2015-08-08 05:49:36

标签: excel-vba vba excel

我想检查文件是否受密码保护。以下代码工作正常。但我的工作簿受密码保护,请告知密码在下面的代码中的位置

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value is System.Drawing.Icon)
        {
            var icon = value as System.Drawing.Icon;
            ImageSource imageSource = Imaging.CreateBitmapSourceFromHIcon(
                icon.Handle,
                System.Windows.Int32Rect.Empty,
                System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
            System.Windows.Controls.Image img = new System.Windows.Controls.Image();
            img.Source = imageSource;
            return img;
        }

        return Binding.DoNothing;
    }

2 个答案:

答案 0 :(得分:0)

我认为您要打开文件作为帖子提及的主题。 按照VBA帮助

打开文件
expression.Open(FileName, UpdateLinks, ReadOnly, Format, Password, WriteResPassword, IgnoreReadOnlyRecommended, Origin, Delimiter, Editable, Notify, Converter, AddToMru, Local, CorruptLoad) 

因此,用法:

Workbooks.Open Filename:= "C:\Documents and Settings\My Documents\Book2.xls", Password:="YourPasswordHere"

答案 1 :(得分:0)

确定工作集或工作簿是否受保护

Option Explicit

Public Function isSheetProtected(Optional ByRef ws As Worksheet = Nothing) As Boolean

    If ws Is Nothing Then Set ws = Application.ActiveSheet

    isWorksheetProtected = ws.ProtectContents Or _
                           ws.ProtectDrawingObjects Or _
                           ws.ProtectScenarios

End Function
Public Function isFileProtected(Optional ByRef wb As Workbook = Nothing) As Boolean

    If wb Is Nothing Then Set wb = Application.ActiveWorkbook

    isWorkbookProtected = ws.ProtectWindows Or ws.ProtectStructure

End Function

取消保护工作簿:

'------------------------------------------------------------------------------------------

Public Sub unprotectFile(Optional ByRef wb As Worksheet = Nothing)

    If wb Is Nothing Then Set wb = Application.ActiveWorkbook

    If isFileProtected(wb) Then wb.Unprotect Password:="YourPassword"

End Sub

'------------------------------------------------------------------------------------------