2016-07-25 89 views
-1

我試圖把一個宏從vba形狀,但我收到這個錯誤「無效的財產使用」,爲什麼?
類CContainer代碼:Shape.OnAction上的屬性錯誤的無效使用

Option Explicit 
Sub CreateContainer() 
Dim s As Shape ' shape container 
Dim t As Shape 'text container 
Dim sr As Variant 'container for grouping 
Dim w As Worksheet 
Set w = ActiveWorkbook.Worksheets(1) 
Set s = w.Shapes.AddShape(msoShapeRectangle, 10, 10, 100, 100) 
With s 
s.Fill.ForeColor.RGB = RGB(255, 255, 255) 
s.Line.ForeColor.RGB = RGB(100, 100, 100) 
s.Line.DashStyle = msoLineDash 
s.Line.Style = msoLineSingle 
s.Line.Weight = 0.5 
s.Name = "ShapeExample" 
End With 
Set t = w.Shapes.AddTextbox(msoTextOrientationHorizontal, s.Left + 10, s.Top + 10, s.Width - 20, 20) 
With t 
t.Line.ForeColor.RGB = RGB(100, 100, 100) 
t.Line.DashStyle = msoLineDash 
t.TextFrame.Characters.Text = "Connector" 
t.TextFrame.Characters.Font.Size = 10 
t.TextFrame.HorizontalAlignment = xlHAlignCenter 
t.Name = "TextShapeExample" 
End With 
Set sr = w.Shapes.Range(Array("ShapeExample", "TextShapeExample")).Group 
sr.Name = "ContainerExample" 

t.OnAction "MsgCall" '<-- here the error occurs 
End Sub 

Sub MsgCall() 
MsgBox "Hello There" 
End Sub 

和這裏的距離,我想叫它模塊:

Option Explicit 

Sub example() 
Dim connector As CContainer 
Set connector = New CContainer 
connector.CreateContainer 

End Sub 
+0

你錯過了等號嗎? t.OnAction =「MsgCall」 –

+0

不,這會導致「應用程序定義/對象錯誤」,但感謝@科迪G。 – Jey

+0

Set sr = w.Shapes.Range(Array(「ShapeExample」,「TextShapeExample」))中的應用程序已定義/對象錯誤。 –

回答

0

我改變你的代碼有點這樣的:

With Worksheets(1).Shapes(1) 

.OnAction "MsgCall" '<-- no more error here 

End With 

現在,我收到了另一個錯誤:此代碼的Grouping is disabled for selected shapes

Set sr = w.Shapes.Range(Array("ShapeExample", "TextShapeExample")).Group 
+0

我仍然收到同樣的錯誤,即使改變我的代碼爲你向我提出的,'無效使用財產' – Jey

+1

我發現答案,但不是我想要的,問題是一個程序不能從同一個類模塊中調用(至少在我的例子中),我將子程序'MsgCall'移到了一個新的std模塊,然後我修改了我的代碼在線路錯誤改變這個'OnAction =「MsgCall」',然後它的工作原理,無論如何感謝您的幫助,因爲它指導我找到答案 – Jey

相關問題