excel - 检查剪贴板是否为图像

标签 excel vba ms-word clipboard copy-paste

我正在尝试将 .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

最佳答案

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

#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 ...

<小时/>

更多信息:

关于excel - 检查剪贴板是否为图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42853007/

相关文章:

excel - 多个条件的 SUM(if) 函数 + 动态的日期/时间

Excel vba 重命名复选框

ms-access - Access 2013 VBA - 为控件设置新的点击事件

matplotlib - 为 ipython 图启用右键单击复制?

perl - 如何使用 Perl 编写 Microsoft Word/Office Open XML 文档?

excel - 为什么我的 Visual Basic excel 代码在我的单元格中返回 #value

java - 公式单元格上的 JExcelApi 货币格式 - 文件错误

mysql - Excel VBA mysql 查询字符串变量

vba - 在文本字符串中插入空格 准备文件目录

python-3.x - 如何在 python-docx 中提取使用跟踪更改插入的文本