以下是我的問題:
第一次執行程式的時候完全沒問題, 我檢查了EXCEL檔案, 都有我想要做到的上面3項
可是當我再一次執行功能選單, 打開Form2按下CommandButton的時候, 卻在
"Selection.ShapeRange.Fill.Patterned msoPatternLightDownwardDiagonal"
出現了錯誤訊息
"91 尚未設定物件變數或With區塊變數"
請問各位高手
這是什麼原因
我的猜想是Selection找不到物件, 可是為什麼第一次執行可以而第二次卻不行,
請問這該如何解決
謝謝
以下是我的程式碼
Private Sub Command1_Click()
Dim strFileSource As String
Dim strFileTarget As String
'設定引用項目 Microsoft Scripting Runtime
'設定引用項目 Microsoft Excel 10.0 Object Library
'設定引用項目 Microsoft Office 10.0 Object Library
Dim fs As New FileSystemObject
If Text1.Text = "" Then
MsgBox "不可空白", vbExclamation
Else
test = Text1.Text
'複製檔案
strFileSource = "C:\Book2.xls"
strFileTarget = "C:\" & test & ".xls"
fs.CopyFile strFileSource, strFileTarget
'開啟並讀取excel檔案
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open(strFileTarget)
Set xlSheet = xlBook.Worksheets(1)
'1 輸入數字文字
xlSheet.Cells(1, 1) = Text1.Text
xlSheet.Cells(1, 3) = 1
'2 畫多邊形
With xlSheet.Shapes.BuildFreeform(msoEditingAuto, 108#, 132#)
.AddNodes msoSegmentLine, msoEditingAuto, 324.75, 132#
.AddNodes msoSegmentLine, msoEditingAuto, 324.75, 198#
.AddNodes msoSegmentLine, msoEditingAuto, 108#, 181.5
.AddNodes msoSegmentLine, msoEditingAuto, 108#, 132#
.ConvertToShape.Select
End With
'畫斜線
Selection.ShapeRange.Fill.Patterned msoPatternLightDownwardDiagonal
'3 加入文字方塊並輸入123
xlSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, 129#, 159#, 37.5,
17.25).Select
Selection.Characters.Text = "123"
Selection.ShapeRange.Fill.Visible = msoTrue
End If
xlBook.Close (True)
xlApp.Quit
Set xlApp = Nothing
Unload Form2
End Sub
用了 CreateObject 就不需要引用 Excel 物件。
你必須明確說明你在哪裡使用,例如 VB6/VBA/VBScript/VBNET ,看起來像是 VB6 。
With 裡面多了 . 變成 ..
改用
Set xlShape = .ConvertToShape
xlShape.Fill.Patterned msoPatternLightDownwardDiagonal
下面也是類似的改法,最後記得把參照的物件設為 Nothing ,以便釋放 Automation :
Set xlShape = Nothing
==> 本文由 "fiesty <fie...@discussions.microsoft.com>"
> 於 news:C908DCF8-A519-4491-B98C-98B0DA21ACF7%40microsoft.com 發表
> ..AddNodes msoSegmentLine, msoEditingAuto, 324.75, 132#
> ..AddNodes msoSegmentLine, msoEditingAuto, 324.75, 198#
> ..AddNodes msoSegmentLine, msoEditingAuto, 108#, 181.5
> ..AddNodes msoSegmentLine, msoEditingAuto, 108#, 132#
> ..ConvertToShape.Select
> End With
> '畫斜線
> Selection.ShapeRange.Fill.Patterned msoPatternLightDownwardDiagonal
> '3 加入文字方塊並輸入123
> xlSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, 129#, 159#, 37.5,
> 17.25).Select
> Selection.Characters.Text = "123"
> Selection.ShapeRange.Fill.Visible = msoTrue
> End If
> xlBook.Close (True)
> xlApp.Quit
> Set xlApp = Nothing
> Unload Form2
> End Sub
--
風禹科技驗證有限公司 ASP.NET Web News Reader 0.2.6 UTF-8 Beta
網站地圖 http://tlcheng.twbbs.org/wwwmap.htm
流域防洪/區域水資源/徐昇網/玫瑰圖/語音通訊 文章與程式
Basic/Fortran/Windows API/.Net/輔助說明檔 原始碼、文章與討論
微軟程式設計、系統管理使用新技術論壇討論區,網友回覆後即時簡訊、電子郵件通知:
MSDN: http://forums.microsoft.com/msdn-cht/default.aspx?siteid=14
TechNet: http://forums.microsoft.com/technet-cht/default.aspx?siteid=23
--
ASPNET News Reader http://tlcheng.twbbs.org/News/Reader.aspx
RSS 2.0 http://tlcheng.twbbs.org/News/rss2.aspx?Action=List&Newsgroup=microsoft.public.tw.dotnet.languages.vb
Dim xlshape As Shape
...
With xlSheet.Shapes.BuildFreeform(msoEditingAuto, 108#, 132#)
...
.ConvertToShape.Select
Set xlshape = .ConvertToShape
End With
xlShape.Fill.Patterned msoPatternLightDownwardDiagonal
結果在
xlShape.Fill.Patterned msoPatternLightDownwardDiagonal
出現錯誤訊息 "找不到方法或資料成員"
我也試過把
xlShape.Fill.Patterned msoPatternLightDownwardDiagonal
寫到With 裡面
With xlSheet.Shapes.BuildFreeform(msoEditingAuto, 108#, 132#)
...
.ConvertToShape.Select
Set xlshape = .ConvertToShape
xlShape.Fill.Patterned msoPatternLightDownwardDiagonal
End With
還是出現同樣的錯誤訊息
請問應該如何修正呢
謝謝
Set xlshape = .ConvertToShape
End With
objShape.Fill.Patterned msoPatternLightDownwardDiagonal
先前直接在網頁上回覆,沒下去測試程式碼,沒注意到 xlShape 已被定義在 Excel 程式庫內,所以是關鍵字不能使用。
Const Excel.XlChartItem.xlShape = 14
註:已在 VBA 測試過改用 objShape 可以跑了。
==> 本文由 "fiesty <fie...@discussions.microsoft.com>"
> 於 news:EBF75B7F-3007-4031-99E2-2BFB4F946E34%40microsoft.com 發表
> 謝謝您的回覆
> 您所說的原因我大概知道了
> 不過您建議的方法 我試過了 修改如下
> Dim xlshape As Shape
> ....
> With xlSheet.Shapes.BuildFreeform(msoEditingAuto, 108#, 132#)
> ....
==> 本文由 "璉璉 <de...@tainan.com.tw>"
> 於 news:0875AFC3D44F43F2BAE70614985DD56B%40c2e6400 發表
謝謝