麻辣家族討論版版's Archiver

HUNGCHILIN 發表於 2012-2-24 23:38

關於VB6(or VSTO)製作Excel DLL相關技巧

[i=s] 本帖最後由 HUNGCHILIN 於 2012-2-25 00:02 編輯 [/i]

最近突然覺得無聊了起來,提不起勁。可能是到了又要尋找方向突破的時候
目前應該有兩個方向去試鍊與應用
1.2010版功能應用
2.VB(or VSTO)製作Excel DLL相關技巧

[b][size=5]本文在於試鍊與記錄VB(or VSTO)製作Excel DLL相關技巧[/size][/b]
以下先介紹VB6製作ActiveX DLL或增益功能...等舊方法,再介紹VSTO方法

HUNGCHILIN 發表於 2012-2-24 23:54

[i=s] 本帖最後由 HUNGCHILIN 於 2012-2-25 02:32 編輯 [/i]

[b][size=6]關於 安裝 dll[/size][/b]

[size=4]
[b]使用vbs檔[color=Red]安裝[/color]dll(假設安裝 ULCase.dll)[/b]
  1.在txt檔內輸入
     RegSvr32.exe ULCase.dll
  2.改檔名為.BAT,即完成安裝檔

[b]使用vbs檔[color=Red]卸載[/color]dll(假設卸載 ULCase.dll)[/b]
  1.在txt檔內輸入
     RegSvr32.exe ULCase.dll / U
  2.改檔名為.BAT,即完成安裝檔[/size]
[attach]9714[/attach]

HUNGCHILIN 發表於 2012-2-25 01:46

[i=s] 本帖最後由 HUNGCHILIN 於 2012-2-28 23:24 編輯 [/i]

[b][size=6]關於 如何製作第一個DLL檔[/size][/b]

[size=4]
[b]a.我們使用英文大小寫轉換.XLA 增益集 來作改編[b][/size][attach]9715[/attach]
excel原始程式碼[code]Option Explicit
Sub 首字大寫轉換()
On Error GoTo 1
Dim RANGECOUNT
Dim RANGECOUNTA
Application.ScreenUpdating = False
For Each RANGECOUNT In Selection
RANGECOUNTA = RANGECOUNTA + 1
If Range(Selection.Address).Cells(RANGECOUNTA) = "" Then GoTo 2
Range(Selection.Address).Cells(RANGECOUNTA) = WorksheetFunction.Proper(Range(Selection.Address).Cells(RANGECOUNTA))
2 Next
1 End Sub
Sub 大寫轉換()
On Error GoTo 1
Dim RANGECOUNT
Dim RANGECOUNTA
Application.ScreenUpdating = False
For Each RANGECOUNT In Selection
RANGECOUNTA = RANGECOUNTA + 1
If Range(Selection.Address).Cells(RANGECOUNTA) = "" Then GoTo 2
Range(Selection.Address).Cells(RANGECOUNTA) = UCase(Range(Selection.Address).Cells(RANGECOUNTA))
2 Next
1 End Sub
Sub 小寫轉換()
On Error GoTo 1
Dim RANGECOUNT
Dim RANGECOUNTA
Application.ScreenUpdating = False
For Each RANGECOUNT In Selection
RANGECOUNTA = RANGECOUNTA + 1
If Range(Selection.Address).Cells(RANGECOUNTA) = "" Then GoTo 2
Range(Selection.Address).Cells(RANGECOUNTA) = LCase(Range(Selection.Address).Cells(RANGECOUNTA))
2 Next
1 End Sub[/code]----------------------------------------------------------------------------------------------------------------------------
[size=4][b]b.使用VB6製作dll[/b]
  1.先COPY 英文大小寫轉換.XLA 增益集 內模組內的程式
  2.開啟vb6
[attach]9716[/attach]

  3.刪除預設的表單&新增一個class
     更改class名稱為ULCASECLASS (記住等一下要用)
     並把英文大小寫轉換.XLA 增益集 內模組內的程式貼進class
[attach]9718[/attach]
[attach]9717[/attach]

更改class內的程式碼為[code]Option Explicit
Sub ULCase()
On Error GoTo 1
Dim EXL As Object
Set EXL = GetObject(, "Excel.Application")
With EXL
Dim RANGECOUNT
Dim RANGECOUNTA
.ScreenUpdating = False
For Each RANGECOUNT In .Selection
RANGECOUNTA = RANGECOUNTA + 1
If .Range(.Selection.Address).Cells(RANGECOUNTA) = "" Then GoTo 2
.Range(.Selection.Address).Cells(RANGECOUNTA) = .WorksheetFunction.Proper(.Range(.Selection.Address).Cells(RANGECOUNTA))
2 Next
End With
Set EXL = Nothing
1 End Sub
Sub UCaseA()
On Error GoTo 1
Dim EXL As Object
Set EXL = GetObject(, "Excel.Application")
With EXL
Dim RANGECOUNT
Dim RANGECOUNTA
.ScreenUpdating = False
For Each RANGECOUNT In .Selection
RANGECOUNTA = RANGECOUNTA + 1
If .Range(.Selection.Address).Cells(RANGECOUNTA) = "" Then GoTo 2
.Range(.Selection.Address).Cells(RANGECOUNTA) = UCase(.Range(.Selection.Address).Cells(RANGECOUNTA))
2 Next
End With
Set EXL = Nothing
1 End Sub
Sub LCaseA()
On Error GoTo 1
Dim EXL As Object
Set EXL = GetObject(, "Excel.Application")
With EXL
Dim RANGECOUNT
Dim RANGECOUNTA
.ScreenUpdating = False
For Each RANGECOUNT In .Selection
RANGECOUNTA = RANGECOUNTA + 1
If .Range(.Selection.Address).Cells(RANGECOUNTA) = "" Then GoTo 2
.Range(.Selection.Address).Cells(RANGECOUNTA) = LCase(.Range(.Selection.Address).Cells(RANGECOUNTA))
2 Next
End With
Set EXL = Nothing
1 End Sub[/code]4.點設計師中的Connect如圖,進入後更改應用程式為excel與應用程式版本假設為9.0,設定載入行為為None
[attach]9719[/attach]

5.點設計師中的Connect按右鍵選檢視程式碼刪除裡面所有程式
[attach]9720[/attach]

6.到專案設定屬性如下圖(請記得屬性描述)
[attach]9721[/attach]

7.儲存專案如附檔,裡面有一些暫存檔可以不用理會
[attach]9722[/attach]

8.製成DLL檔 如圖 (如果程式有問題VB會偵錯)
[attach]9723[/attach]
成品:[attach]9724[/attach]

9.安裝DLL檔,請下載第二帖 到DLL同資料夾 並按"安裝BAT檔"

10.Excel vba如何使用DLL內的程式
需要在Excel模組內輸入下列程式[code]Public Sub aa()
Dim XLSULCASE As New ULCASECLASS
XLSULCASE.ULCase
End Sub

Public Sub bb()
Dim XLSULCASE As New ULCASECLASS
XLSULCASE.UCaseA
End Sub

Public Sub cc()
Dim XLSULCASE As New ULCASECLASS
XLSULCASE.LCaseA
End Sub[/code]附檔:[attach]9800[/attach]
使用上述10個小步驟即可完成製作與使用DLL程式
[/size]

sklo 發表於 2012-4-9 06:41

你好,先謝謝大大分享

我是VB6新手,嘗試照著大大的步驟做封裝,但去到以下步驟就不懂了︰

以下是我開啟VB6後的情況,找不到「設計師」(如大大的圖),請問怎樣做才看到「設計師」一項呢?謝謝賜教!

[attach]10356[/attach]

大大的圖片︰
[img]http://forum.twbts.com/attachments/month_1202/12022502114d2f4046e2eccc57.jpg[/img]

HUNGCHILIN 發表於 2012-4-9 22:16

看到b 2步驟
有選 activex dll

sklo 發表於 2012-4-10 00:01

[quote]看到b 2步驟
有選 activex dll
[size=2][color=#999999]HUNGCHILIN 發表於 2012-4-9 22:16[/color] [url=http://forum.twbts.com/redirect.php?goto=findpost&pid=35861&ptid=5913][img]http://forum.twbts.com/images/common/back.gif[/img][/url][/size][/quote]


    已選了 Activex Dll,選了後只出現我上一貼附圖效果。

另外,我的電腦是用WINDOW 7,網上睇資料,好像說VB6用在WINDOW7上會出問題,是否真的這樣?

謝謝

Scott090 發表於 2013-3-22 23:56

[b]參與 [url=http://forum.twbts.com/redirect.php?goto=findpost&pid=35866&ptid=5913]6#[/url] [i]sklo[/i] [/b]


    在專案名稱 Project1 按右鍵,選擇加入 ADDIN CLASS 即可以有 設計師 AddInDessigner1;把它的名稱改為 Connect;只是表格中的 "應用程式(A)" 只能有 Default 值 "Visual Basic"; "應用程式版本" 也是 Default 值 "Visual Basic 6.0" ;所以玩不下去。
有請大師指點迷津。

Scott090 發表於 2013-4-5 10:49

使用 VB6.0 或 2008製作 .dll 可以被 x86 EXCEL2010使用; 但是  x64 EXCEL 2010 卻不能用 ,錯誤429!! 不知為什麼。 裡面沒有明顯的 Declare 及 Long 的東東, 2008應是 x64版本。
爬文中發現有人主張用 2008做 Automation 來驅動 EXCEL;這個本末易位不是想要的東東

2lg 發表於 2013-9-13 11:50

真的不枉我来注册这个坛子。

Scott090 發表於 2013-11-25 17:08

[b]回復 [url=http://forum.twbts.com/redirect.php?goto=findpost&pid=35861&ptid=5913]5#[/url] [i]HUNGCHILIN[/i] [/b]

有請 大師
    在不同的電腦使用VB6.0 做 Dll,發現 AddInDesigner 的視窗內: 應用程式(A),應用程式版本(V)  可以做選擇
而之前在另一電腦卻不能 !!! 是註冊表的問題還是 ???

謝謝先

opelwang 發表於 2014-3-6 12:24

跟暍恛,有一方法,用VSTO制作DLL。

Scott090 發表於 2014-3-18 10:15

[i=s] 本帖最後由 Scott090 於 2014-3-18 10:19 編輯 [/i]

[b]回復 [url=http://forum.twbts.com/redirect.php?goto=findpost&pid=61478&ptid=5913]10#[/url] [i]Scott090[/i] [/b]


    正在研讀    使用 Excel 的逐步解說

http://msdn.microsoft.com/zh-tw/library/d7f63219

Scott090 發表於 2014-3-18 10:25

[b]回復 [url=http://forum.twbts.com/redirect.php?goto=findpost&pid=65427&ptid=5913]12#[/url] [i]Scott090[/i] [/b]


    相關MSDN網站
Excel 文件層級自訂的程式設計入門
http://msdn.microsoft.com/zh-tw/library/f27xe9xb.aspx

Scott090 發表於 2014-3-18 10:38

[b]回復 [url=http://forum.twbts.com/redirect.php?goto=findpost&pid=65428&ptid=5913]13#[/url] [i]Scott090[/i] [/b]


    COM addIn 應屬於   應用程式層級增益集

逐步解說:建立 Excel 的第一個應用程式層級增益集
http://msdn.microsoft.com/zh-tw/library/cc668205

Scott090 發表於 2014-3-18 11:26

[b]回復 [url=http://forum.twbts.com/redirect.php?goto=findpost&pid=65431&ptid=5913]14#[/url] [i]Scott090[/i] [/b]


    逐步解說:使用 Visual Studio 專案 Automation 建立新的 Office 專案
http://msdn.microsoft.com/zh-tw/library/x50x16we

frantz 發表於 2016-4-3 16:04

看劍到這篇正在思考能否用Visual Studio 制作EXCEL中RTD函數需要用的COM呢…

shaokui123 發表於 2021-3-25 21:42

vba代码贴附进vb还需要修改一下,不爽

頁: [1]

麻辣家族討論版版為 麻辣學園 網站成員  由 昱得資訊工作室 © Since 1993 所提供