在受保护/共享工作簿中插入图像的宏 - 保存后出现问题

时间:2018-05-19 22:19:20

标签: excel vba excel-vba

运行代码后出现问题如下。  工作簿受到保护和共享,请考虑Excel不允许您以共享模式插入图像文件。

目标是在将图像调整大小并放入先前选定的单元格后,将图像放在工作簿上。  问题是,事实上,一切都运行到这一点,但在保存工作簿,关闭程序并重新打开它之后,没有图像存在,也没有调整单元格的大小。  此外,工作簿不再处于共享模式(这是许多用户使用此文件的基础)。

有人能帮助我吗?非常感谢你们所有人!

代码:

Sub Button22_Click()

 If ActiveWorkbook.MultiUserEditing Then
        Application.DisplayAlerts = False
        ActiveWorkbook.ExclusiveAccess
        ActiveSheet.Unprotect ""
        Application.DisplayAlerts = True
        MsgBox "Now exclusive"
    End If

ActiveCell.Select
Selection.Rows.RowHeight = 90
Selection.Columns.ColumnWidth = 25

Dim fName As String
Dim pic As Picture
Dim r As Range

fName = Application.GetOpenFilename( _
        FileFilter:="Images (*.jpg;*.gif;*.png),*.jpg;*.gif;*.png", _
        Title:="Please select an image...")
If fName = "False" Then Exit Sub

Set r = ActiveCell
Set pic = ActiveSheet.Pictures.Insert(fName)

With pic
    .ShapeRange.LockAspectRatio = msoFalse
    .Left = r.Left
    .Top = r.Top
    .Width = r.Width
    .Height = r.Height
    .Select
End With

If TypeName(Selection) = "Picture" Then
    Application.SendKeys "%a~"
    Application.CommandBars.ExecuteMso "PicturesCompress"
End If

Dim Password As String
    Password = ""
    ActiveSheet.Protect _
        Password:=(Password), _
        AllowFormattingColumns:=True, _
        AllowFormattingRows:=True, _
        AllowInsertingColumns:=True, _
        AllowInsertingRows:=True, _
        AllowFormattingCells:=True


 If Not ActiveWorkbook.MultiUserEditing Then
        Application.DisplayAlerts = False
        ActiveWorkbook.SaveAs ActiveWorkbook.Name, accessmode:=xlShared
        Application.DisplayAlerts = True
        MsgBox "Now Shared"
    End If

End Sub

0 个答案:

没有答案