将图表创建为GIF并加载到UserForm

时间:2017-05-29 01:44:40

标签: excel excel-vba excel-2010 vba

我正在使用John Walkenbach代码将图表导出为GIF,然后将其加载到用户表单中。我遇到的问题是这里有public void markStructuralIntrons(LGP manager) { int instruction_count=instructions.size(); for (int i = instruction_count - 1; i >= 0; i--) { instructions.get(i).setStructuralIntron(true); } Set<Integer> Reff = new HashSet<>(); int io_register_count = manager.getRegisterCount(); for (int i = 0; i < io_register_count; ++i) { Reff.add(i); } Instruction current_instruction = null; Instruction prev_instruction = null; // prev_instruction is the last visited instruction from bottom up of the program for (int i = instruction_count - 1; i >= 0; i--) { prev_instruction = current_instruction; current_instruction = instructions.get(i); // prev_instruction is not an structural intron and the current_instruction // is a conditional construct then, the current_instruction is not structural intron either // this directly follows from Step 3 of Algorithm 3.1 if (current_instruction.getOperator().isConditionalConstruct() && prev_instruction != null) { if (!prev_instruction.isStructuralIntron()) { current_instruction.setStructuralIntron(false); } } else { if (Reff.contains(current_instruction.getTargetOperand().getIndex())) { current_instruction.setStructuralIntron(false); Reff.remove(current_instruction.getTargetOperand().getIndex()); if (!current_instruction.getOperand1().isConstant()) { Reff.add(current_instruction.getOperand1().getIndex()); } if (!current_instruction.getOperand2().isConstant()) { Reff.add(current_instruction.getOperand2().getIndex()); } } } } } 。它没有将GIF加载到UserForm上。它导出Image很好我可以在与我的excel文件相同的目录中看到它。

Image1.Picture = LoadPicture(Fname)

1 个答案:

答案 0 :(得分:1)

在使用之前,您需要为Fname指定一个值。有几种方法可以做到这一点:

1)只需在CommandButton1_Click

中指定值即可
Private Sub CommandButton1_Click()
    GetChart
    Fname = ThisWorkbook.Path & "/temp.gif"
    Image1.Picture = LoadPicture(Fname)
    MsgBox "Yep"
End Sub

Public Sub GetChart()
    Set CurrentChart = Sheets("StatsDB").ChartObjects(1).Chart
    Fname = ThisWorkbook.Path & "/temp.gif"
    CurrentChart.Export Filename:=Fname, FilterName:="GIF"
End Sub

2)将您在GetChart中使用的值作为“返回”值传递回来:

Private Sub CommandButton1_Click()
    Fname = GetChart()
    Image1.Picture = LoadPicture(Fname)
    MsgBox "Yep"
End Sub

Public Function GetChart() As String
    Set CurrentChart = Sheets("StatsDB").ChartObjects(1).Chart
    Fname = ThisWorkbook.Path & "/temp.gif"
    CurrentChart.Export Filename:=Fname, FilterName:="GIF"
    GetChart = Fname
End Sub

3)在范围内设置Fname模块级别:

Dim Fname As String

Private Sub CommandButton1_Click()
    GetChart
    Image1.Picture = LoadPicture(Fname)
    MsgBox "Yep"
End Sub

Public Sub GetChart()
    Set CurrentChart = Sheets("StatsDB").ChartObjects(1).Chart
    Fname = ThisWorkbook.Path & "/temp.gif"
    CurrentChart.Export Filename:=Fname, FilterName:="GIF"
End Sub

4)将两个子程序合并为一个:

Private Sub CommandButton1_Click()
    Set CurrentChart = Sheets("StatsDB").ChartObjects(1).Chart
    Fname = ThisWorkbook.Path & "/temp.gif"
    CurrentChart.Export Filename:=Fname, FilterName:="GIF"
    Image1.Picture = LoadPicture(Fname)
    MsgBox "Yep"
End Sub

还有很多方法可以做到这一点,但希望以上其中一个吸引你。