检查剪贴板是否为图片。

4
我试图将一个 .PDF 文件复制到 Word 和 Excel 中(这里只是为了展示 Word 的代码,与 Excel 几乎相同)。为此,我使用 IrfranView 将 .PDF 转换为图像,但它有时候只能起作用,不知道为什么?我认为我需要等待一段时间再粘贴它。
有没有办法可以检查剪贴板是否包含图像,并循环保持直到存在或计时器超过 1.5 秒?
'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个回答

4

这是检查剪贴板中是否存在图片的代码,请在单独的模块中使用:

#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
我认为你无法检查它是否包含任何图像,但你可以检查它是否不再包含文本。可以尝试使用以下代码:
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更改为更合适的内容。然后进行最终检查,决定是否粘贴。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接