在VBA中隐藏图片压缩对话框(Excel 2010)

时间:2016-09-27 15:23:14

标签: excel vba dialog compression hide

我使用宏来自动压缩Excel 2010中的图片,宏打开一个对话框并发送密码,最终用户可以看到它(半秒钟),我想隐藏它。请帮忙!

这是我的宏:

Sub compression()
Application.SendKeys "%w~"
Application.CommandBars.ExecuteMso "PicturesCompress"
End Sub

我已经尝试过:

Application.DisplayAlerts = False
Application.EnableEvents = False
Application.ScreenUpdating = False

但他们似乎无法工作。

@TomPreston 这是我的整个代码,我希望用户双击单元格类型以在评论中插入图片,但必须压缩图片以保证文件合适!

我也遇到sendkeys和num lock的问题,如果有人可以帮我这个(见下文):

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

    If Not Intersect(Target, [v:v]) Is Nothing Then

    Application.ScreenUpdating = False

    With Application.FileDialog(msoFileDialogFilePicker)
             .AllowMultiSelect = False
             .InitialFileName = CurDir
             .Filters.Clear
             .Filters.Add Description:="Images", Extensions:="*.png;*.jpg;*.jpeg;*.gif", Position:=1
             .Title = "Choose image"

             If .Show = -1 Then TheFile = .SelectedItems(1) Else TheFile = 0
        End With

    If TheFile = 0 Then
    MsgBox ("No image selected")
    Exit Sub
    End If

    ActiveCell.ClearComments
    Selection.AddComment
    PreviousCell = ActiveCell.Address
    ActiveCell.Comment.Shape.Fill.UserPicture TheFile

    NumLockState = GetKeyState(VK_NUMLOCK)

    Application.SendKeys "%a~"
    Application.CommandBars.ExecuteMso "PicturesCompress"

    If NumLockState <> GetKeyState(VK_NUMLOCK) Then
    Application.SendKeys ("%{Numlock}"), True
    End If

    ActiveCell.Comment.Visible = True

    CommentAdded = True

    Application.ScreenUpdating = True

    End If

    End Sub

用户可以更改图片的大小,选择更改后,评论将被隐藏。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If CommentAdded = True Then

Range(PreviousCell).Comment.Visible = False

PreviousCell = ""

CommentAdded = False

End If

End Sub

这些是变量:

Public CommentAdded As Boolean
Public PreviousCell As String
Public Const VK_NUMLOCK = &H90
Public Declare Function GetKeyState Lib "user32" _
(ByVal nVirtKey As Long) As Long

1 个答案:

答案 0 :(得分:0)

如果您只想隐藏闪烁效果,请使用:

Sub compression()
Application.ScreenUpdating = False
Application.SendKeys "%w~" Application.CommandBars.ExecuteMso "PicturesCompress"
Application.ScreenUpdating = True
End Sub