1

作为一个更大项目的一部分,我需要在一个宏中显示一系列图像。当我运行它时,它会在最后一个 msgbox 之后显示最终图像,其他图像在下面:

Sub Macro4()
Dim x As Integer
Dim Pic As Object
Dim picname As String
For x = 1 To 7
picname = ThisWorkbook.Path & "/" & "pic" & x & ".png"
ActiveSheet.Pictures.Insert(picname).Select
MsgBox (x)
Next x
End Sub

Msgbox 命令可以减慢处理速度,以便我可以看到,或者在这种情况下看不到,图片会发生变化。

这些图像被称为 pic1.png、pic2.png 等

如何在宏期间显示单独的图像?

重新编辑:

所以这里是图片功能和播放随机音乐混凝土的较大功能。

Private Declare Function PlaySound Lib "winmm.dll" _
Alias "PlaySoundA" (ByVal lpszName As String, _
ByVal hModule As Long, ByVal dwFlags As Long) As Long

showpic给出与正在播放的声音的音高相关的图像。

Function showpic(value)
Dim v As Integer
v = value
picname = ThisWorkbook.Path & "/" & "pic" & v & ".png"
ActiveSheet.Shapes.AddPicture (picname), True, True, a1, a1, 170, 170
DoEvents
End Function

play运行一系列特定的声音文件,这些文件由随机过程生成,选择乐器和音高并创建必要的文件名。“piece”运行“notes”秒,并由一个单独的宏触发,该宏更改给定“单元格”中的值以匹配“条件”。

Function play(Cell, condition, notes)
Dim WAVFile As String
Dim x As Integer
Dim y As Integer
Dim z As Integer
Dim c As String
Dim d As String
Dim currentcell As String
Dim pitchcell As String
Const SND_SYNC = &H0
Const SND_ASYNC = &H1
Const SND_FILENAME = &H20000
On Error GoTo ErrHandler
For x = 1 To notes
If Evaluate(Cell.value & condition) Then
y = x + 65
z = x + 39
c = Chr(y)
d = Chr(z)
If y > 90 Then c = "A" & d
currentcell = c & 6
pitchcell = c & 3
showpic (Sheets("Sheet1").Range(pitchcell).value)
WAVFile = ThisWorkbook.Path & "/" & Sheets("Sheet1").Range(currentcell).Text & ".wav"
Call PlaySound(WAVFile, 0&, SND_SYNC)
End If
Next x
ErrHandler:
play = False
Exit Function
End Function

我有三个问题:

  1. 它不显示第一个图像文件
  2. 它在开始时执行“计算”或 F9,因此重新随机播放乐曲并且不播放显示的序列;我认为这是由第一个 DoEvents 引起的。
  3. 它现在播放两次!但是,第二次通过它确实显示了所有图像文件。
4

1 回答 1

1

编辑:
由于我最初的答案对您不起作用,那么以完全不同的方式实现您的结果怎么样。此方法使用Application.OnTime重新运行您的 sub 并插入下一张图片。

Sub NextPicture()
    Static x As Integer
    Dim pic As Object
    Dim picname As String
    'Reset x to 0 because I assumed you want to rotate through the pictures.
    'If you want it to stop replace with If x = 7 then exit sub
    If x = 7 Then x = 0
    x = x + 1
    picname = ThisWorkbook.Path & "/" & "pic" & x & ".png"
    ActiveSheet.Pictures.Insert(picname).Select
    'Using 00:00:05 = 5 seconds, change the amount to speed up or slow down the picture changes
    Application.OnTime Now + TimeValue("00:00:05"), "NextPicture"
End Sub

原始答案
我无法让您的代码正常工作,但如果我使用Shapes.AddPicture,我可以,这实际上更强大,因为您获得/必须指定位置和大小。

句法:

expression.AddPicture(Filename, LinkToFile, SaveWithDocument, Left, Top, Width, Height)

例子:

ActiveSheet.Shapes.AddPicture(picname, True, True, 100, 100, 70, 70)
于 2012-10-10T23:14:41.750 回答