检查剪贴板是否为图像

时间:2017-03-17 08:53:20

标签: excel vba word-vba clipboard copy-paste

我正在尝试将.PDF复制到Word和Excel中(此处仅为Word显示代码,对于Excel几乎相同)。为此,我使用IrfranView.PDF转换为图片,但它只在某些情况下有效,不知道为什么?我想我需要等一段时间才能粘贴它。

有没有办法可以检查剪贴板是否包含图像并保持循环直到它或者计时器超过1.5s?

'Add pdf of drawing to word file
If zFile <> "" Then
    Dim oData As New MSForms.DataObject
    oData.SetText Text:="Empty" 'Clear
    oData.PutInClipboard 'take in the clipboard to empty it
    Shell "C:\Program Files (x86)\IrfanView\i_view32.exe " & zFile _
        & "/clipcopy /convert=" & Environ("AppData") _
        & "\IrfanView\ConverTemp.jpg /jpgq=100"
    Sleep (1000)
    copyImg = Not oData.GetFormat(1)
    If copyImg Then
        Documents(docLogSkjema).Activate
        Selection.EndKey Unit:=wdStory
        Selection.InsertBreak Type:=wdSectionBreakNextPage
        With Selection.PageSetup
            .Orientation = wdOrientLandscape
            .PageWidth = CentimetersToPoints(42)
            .PageHeight = CentimetersToPoints(29.7)
        End With
        With Selection.Sections(1).Headers(wdHeaderFooterPrimary)
            .LinkToPrevious = False
            .Range.Delete
            .LinkToPrevious = False
            .Range.Delete
        End With
        Selection.Paste
    End If
End If

2 个答案:

答案 0 :(得分:2)

以下是检查剪贴板上是否有图片的代码,在单独的模块中使用此代码:

#If Win64 Then
  Private Declare PtrSafe Function IsClipboardFormatAvailable Lib "user32" _
      (ByVal wFormat As Long) As Long
#Else
  Private Declare Function IsClipboardFormatAvailable Lib "user32" _
      (ByVal wFormat As Long) As Long
#End If

Function Is_Pic_in_Clipboard() As Boolean
  If IsClipboardFormatAvailable(2)<>0 Or IsClipboardFormatAvailable(14)<>0 Then _
      Is_Pic_in_Clipboard = True '2=BMP, 14=JPEG
End Function

然后,要确定是否有图片,请使用If Is_Pic_in_Clipboard Then ...

更多信息:

答案 1 :(得分:1)

我认为您无法检查它是否包含任何图像,但您可以检查它是否不再包含文字。这样的事情怎么样:

oData.SetText "Empty"                         ' create dummy string as object
oData.PutInClipboard                          ' load dummy string to clipboard
Do Until x = 15 Or oData.GetFormat(1) = False ' loop until counter hits 15 or dummy text missing
    x = x + 1                                 ' increment counter
    Sleep (100)                               ' wait
    oData.GetFromClipboard                    ' reload from clipboard
Loop                                          ' end of loop

根据需要将Until x=15更改为更合适的内容。然后做最后的检查以决定是否粘贴。