2014-03-06 43 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