2014-03-06 41 views
1

我正在运行访问VBA代码来打开Excel,对一些图进行小修改并将它们保存为bmp。 98%的时间运行良好。然而,每一个现在,然后我得到的线“Selection.Left = 320”以下错误:运行时错误91 - 访问excel交互

运行时错误“91”:
对象变量或与块变量未设置

二小问题,我我想解决的是,每次我运行这个宏后,每次打开其他excel文档时,这个宏使用的excel文档也会自动打开,不知道为什么。

Private Sub Form_Open(Cancel As Integer) 
If [Forms]![Detail]![Qualification Documents].[Value] <> "" Then 
     Dim octopus As String 
     octopus = ([Forms]![Detail]![Qualification Documents].[Value]) 
     Set objExcelApp = New Excel.APPLICATION 
     Dim ws As Worksheet 

Set wb = objExcelApp.Workbooks.Open(FileName:="Path\" & octopus & " ", ReadOnly:=True) 
     'Set ws = wb.Sheets("SheetX") 
      wb.APPLICATION.DisplayAlerts = False 
      wb.Sheets("SheetX").Select 
    x = 1 
    While x < 4 
    wb.ActiveSheet.ChartObjects(x).Activate 
    With wb.ActiveChart.Parent 
    .Height = 500 ' resize 
    .Width = 1200 ' resize 
    .Top = 100 ' reposition 
    .Left = 100 ' reposition 
    End With 

       On Error GoTo here: 
here: 
wb.ActiveChart.Legend.Select 
Selection.Left = 320 
Selection.Top = 380 
Selection.Height = 35 
Selection.Width = 600 
wb.ActiveChart.Export "X:\Assembly\CAPEX 2013\drilling database\graph" & x & ".bmp" 
x = x + 1 
Wend 
wb.Close 
Set objExcelApp = Nothing 
End If 
End Sub 

回答

0

似乎Selection是从时间到时间,即在运行宏时,选择以某种方式无效,例如无效通过用户点击进入Excel窗口或类似的东西。尝试使用图形或直接对象的Left财产,不使用Selection.

x = 1 
While x < 4 
    With wb.Sheets("SheetX").ChartObjects(x) 
     .Parent.Height = 500 ' resize 
     .Parent.Width = 1200 ' resize 
     .Parent.Top = 100 ' reposition 
     .Parent.Left = 100 ' reposition 

     .Legend.Left = 320 
     .Legend.Top = 380 
     .Legend.Height = 35 
     .Legend.Width = 600 
    End With 
Wend 
+0

感谢您的回答。 Howerver,我已经解决了这个问题,所以我无法测试你对特定问题的建议。 – LuTze