我的代码如下:为什么,我汇入EXcel生成的柏拉图,只有在vb运行(F5)被关闭时候才能被打开,当我运行vb程序(F5),生成柏拉图,只要程序在运行(F5在执行),打开已经保存的Exce文件是不完整的,只有眉头和尾,没有中间图像部分,烦请各位大哥大姐解答,分不够可在加,我怀疑是不是excel进程关闭的不对,另外请指出关闭excel进程的顺序,谢谢
从繁体转过来,稍微有些乱码,请见凉,但影响阅读
Sub charu()
'
' charu エ栋
' fyhu  2003-10-16 魁籹エ栋
''
    ActiveCell.FormulaR1C1 = "a"
    With ActiveCell.Characters(Start:=1, Length:=1).Font
        .Name = "Times New Roman"
        .FontStyle = "夹非"
        .Size = 12
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ColorIndex = xlAutomatic
    End With
    Range("C3").Select
    ActiveCell.FormulaR1C1 = "b"
    With ActiveCell.Characters(Start:=1, Length:=1).Font
        .Name = "Times New Roman"
        .FontStyle = "夹非"
        .Size = 12
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ColorIndex = xlAutomatic
    End With
    Range("D3").Select
    ActiveCell.FormulaR1C1 = "c"
    With ActiveCell.Characters(Start:=1, Length:=1).Font
        .Name = "Times New Roman"
        .FontStyle = "夹非"
        .Size = 12
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ColorIndex = xlAutomatic
    End With
    Range("E3").Select
    ActiveCell.FormulaR1C1 = "d"
    With ActiveCell.Characters(Start:=1, Length:=1).Font
        .Name = "Times New Roman"
        .FontStyle = "夹非"
        .Size = 12
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ColorIndex = xlAutomatic
    End With
    Range("B4").Select
    ActiveCell.FormulaR1C1 = "10"
    Range("C4").Select
    ActiveCell.FormulaR1C1 = "20"
    Range("D4").Select
    ActiveCell.FormulaR1C1 = "34"
    Range("E4").Select
    ActiveCell.FormulaR1C1 = "26"
    Range("B3").Select
    Charts.Add
    ActiveChart.ChartType = xlColumnClustered
    ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("B3:E4")
    ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet1"
    ActiveWindow.Visible = False
    Windows("Report.xls").Activate
    Range("H6").Select
    ActiveSheet.ChartObjects("瓜 1").Activate
    ActiveChart.ChartArea.Select
    ActiveWindow.Visible = False
    Windows("Report.xls").Activate
    Range("K13").Select
    ActiveSheet.ChartObjects("瓜 1").Activate
    ActiveChart.ChartArea.Select
    ActiveSheet.Shapes("瓜 1").ScaleWidth 1.36, msoFalse, msoScaleFromTopLeft
    ActiveSheet.Shapes("瓜 1").ScaleWidth 1.26, msoFalse, msoScaleFromBottomRight
End SubPrivate Sub Command1_Click()
    Dim rs As ADODB.Recordset
    Dim sql As String
    sql = "select * from draw"
    Set rs = conn.myconn.Execute(sql)
    If rs.RecordCount > 0 Then
      ' MsgBox "OK"
      Set TDBGrid1.DataSource = rs
      Set DataGrid1.DataSource = rs
       
    End If
    
End SubPrivate Sub Command2_Click()   Dim xlApp As New Excel.Application
   Dim xlBook As New Excel.Workbook
   Dim xlSheet As New Excel.Worksheet
   Dim savepath As String
   CommonDialog1.CancelError = True
   On Error GoTo errhander
   CommonDialog1.Flags = cdlOFNHideReadOnly
   CommonDialog1.FileName = "Report"
   CommonDialog1.DefaultExt = ".xls"
   CommonDialog1.Filter = "Excel(*.xls)|*.xls|Text(*.txt)|*.txt"
   CommonDialog1.FilterIndex = 1
   CommonDialog1.Flags = &H2
   CommonDialog1.ShowSave
   
   If Err.Number = cdlCancel Then
      Exit Sub
   End If
   
   savepath = CommonDialog1.FileName
   
   Set xlApp = CreateObject("excel.application")
   xlApp.Visible = False
   Set xlBook = xlApp.Workbooks.Add
   On Error Resume Next
   Set xlSheet = xlBook.Worksheets(1)
   
   xlSheet.Cells(1, 1) = "aa"
   
   Call charu
   
   
   xlBook.SaveAs FileName:=savepath, FileFormat:=xlNormal, _
   Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, _
   CreateBackup:=False
   
  xlBook.Saved = True
  MsgBox "The Save is OK ", vbOKOnly, "Information"
  xlApp.Quit
  Set xlSheet = Nothing
  Set xlBook = Nothing
  Set xlApp = Nothing
  
errhander:
  Exit Sub
  
  Exit Sub
   
 
   
   
   
   
End Sub

解决方案 »

  1.   

    其實很簡單了Sub charu()
    是我在excel裏面錄製的宏,大家只要看這一段好了,謝謝大家
    Private Sub Command2_Click()   Dim xlApp As New Excel.Application
       Dim xlBook As New Excel.Workbook
       Dim xlSheet As New Excel.Worksheet
       Dim savepath As String
       CommonDialog1.CancelError = True
       On Error GoTo errhander
       CommonDialog1.Flags = cdlOFNHideReadOnly
       CommonDialog1.FileName = "Report"
       CommonDialog1.DefaultExt = ".xls"
       CommonDialog1.Filter = "Excel(*.xls)|*.xls|Text(*.txt)|*.txt"
       CommonDialog1.FilterIndex = 1
       CommonDialog1.Flags = &H2
       CommonDialog1.ShowSave
       
       If Err.Number = cdlCancel Then
          Exit Sub
       End If
       
       savepath = CommonDialog1.FileName
       
       Set xlApp = CreateObject("excel.application")
       xlApp.Visible = False
       Set xlBook = xlApp.Workbooks.Add
       On Error Resume Next
       Set xlSheet = xlBook.Worksheets(1)
       
       xlSheet.Cells(1, 1) = "aa"
       
       Call charu
       
       
       xlBook.SaveAs FileName:=savepath, FileFormat:=xlNormal, _
       Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, _
       CreateBackup:=False
       
      xlBook.Saved = True
      MsgBox "The Save is OK ", vbOKOnly, "Information"
      xlApp.Quit
      Set xlSheet = Nothing
      Set xlBook = Nothing
      Set xlApp = Nothing
      
    errhander:
      Exit Sub
      
      Exit Sub
       
     
       
       
       
       
    End Sub
      

  2.   

    我想是不是在调用宏Call charu
    之后还要释放什么阿
    小第刚从delphi转过来,希望大家帮忙阿
      

  3.   

    参考:
    http://www.yesky.com/20030217/1652372.shtml