當前位置:編程學習大全網 - 編程語言 - EXCEL中VB宏可以封裝成EXE文件麽

EXCEL中VB宏可以封裝成EXE文件麽

'壹、用VB制作EXE文件頭部分 1、打開VB,“文件”-“新建工程”-“標準EXE”; _

2、此時會出現名為Form1的默認窗體編輯窗口,Form1將作為軟件啟動封面窗體, _

打開該Form1的屬性窗口,對如下屬性進行設置:BorderStyle=0,StartUpPosition=2, _

Icon與Picture屬性設置成妳需要的圖標(這也將成為妳EXE的圖標)和設計好準備使用的圖片(即軟件封面), _

窗體的大小設置成您需要的合適值即可。 3、往窗體中添加壹個時鐘控件timer1,並將其InterVal屬性設為1000。 _

4、雙擊窗體打開代碼編輯窗口,錄入以下代碼:

Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long

Private Declare Function GetTempFileName Lib "kernel32" Alias "GetTempFileNameA" (ByVal lpszPath As String, ByVal lpPrefixString As String, ByVal wUnique As Long, ByVal lpTempFileName As String) As Long

Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long

Private Const MAX_PATH = 260

Private Const EXE_SIZE = 81920 '本EXE實際字節大小

Private Type FileSection

Bytes() As Byte

End Type

Private Type SectionedFile

Files() As FileSection

End Type

Dim StopTime As Integer

Private Sub Form_Activate()

If Command() = "" Then Main1

End Sub

Private Sub Form_Load()

On Error Resume Next

If Command() = "" Then

Form1.Visible = True

SetWindowPos Form1.hwnd, -1, 0, 0, 0, 0, &H2 Or &H1 '將封面置為最頂層窗體 Else Form1.Visible = False Form1.Timer1.

Enabled = True

End If

End Sub

Sub Main1()

Dim StartXLSByte, I, J As Long

Dim AppPath, XlsTmpPath As String

Dim myfile As SectionedFile

Dim xlApp As Excel.Application '定義EXCEL類

Dim xlBook As Excel.Workbook '定義工件簿類

Dim xlsheet As Excel.Worksheet '定義工作表類

AppPath = App.Path

XlsTmpPath = GetTempFile() '取得XLS臨時文件名(帶路徑)

If VBA.Right(App.Path, 1) = "\" Then AppPath = VBA.Left(App.Path, VBA.Len(App.Path) - 1)

Open AppPath & "\" & App.EXEName & ".exe" For Binary As #1

ReDim myfile.Files(1)

ReDim myfile.Files(1).Bytes(1 To LOF(1) - EXE_SIZE)

Open XlsTmpPath For Binary As #2

Get #1, EXE_SIZE + 1, myfile.Files(1).Bytes '此處數字要根據EXE實際頭文件大小更改設定

Put #2, 1, myfile.Files(1).Bytes

Close #1

Close #2

Set xlApp = CreateObject("Excel.Application") '創建EXCEL應用類

Set xlBook = xlApp.Workbooks.Open(Filename:=XlsTmpPath, Password:="ldhyob")

'打開EXCEL工作簿,已知該工作簿已加打開口令為ldhyob,若您的工作簿沒有口令,則將此參數去掉 '以下星號括起部分代碼是往xls裏寫數據(也可不往工作簿裏寫數據的 _

方法,而生成txt的方法),作用是保存本exe的絕對路徑與臨時xls絕對路徑,以便於EXE重寫更新與臨時文件刪除 '*****************************************

Set xlsheet = xlBook.Worksheets("temp") '設置活動工作表

xlsheet.Cells(1, 1) = AppPath & "\" & App.EXEName & ".exe" '將該EXE完全路徑存在工作表單元格內

xlsheet.Cells(2, 1) = XlsTmpPath '將該EXE本次運行產生XLS臨時文件路徑存在工作表單元格內 '****************************************

xlApp.Visible = True '設置EXCEL可見

Set xlApp = Nothing '釋放xlApp對象

StopTime = 0

Me.Timer1.Enabled = True '啟動時鐘

End Sub

Private Function GetTempFile() As String '用來產生系統臨時文件名

Dim lngRet As Long

Dim strBuffer As String, strTempPath As String

strBuffer = String$(MAX_PATH, 0)

lngRet = GetTempPath(Len(strBuffer), strBuffer)

If lngRet = 0 Then

Exit Function

strTempPath = Left$(strBuffer, lngRet)

strBuffer = String$(MAX_PATH, 0)

lngRet = GetTempFileName(strTempPath, "tmp", 0&, strBuffer)

If lngRet = 0 Then

Exit Function

lngRet = InStr(1, strBuffer, Chr(0))

If lngRet > 0 Then GetTempFile = Left$(strBuffer, lngRet - 1)

Else: GetTempFile = strBuffer

End If

End Function

Private Sub Timer1_Timer()

On Error Resume Next

If Command() <> "" Then

If VBA.Dir(Command()) <> "" Then

Kill Command() '刪除本次運行遺留的臨時XLS文件

Else: End

End If

Else

StopTime = StopTime + 1 '計時累加

If StopTime = 1 Then

Unload Me: End '2秒後自動關閉退出

End If

End Sub

'在ThisWorkBook代碼區頭部加入以下代碼:

Private Const EXE_SIZE = 81920 '此處數字為前面第7步得到的EXE文件字節數

Private Type FileSection

Bytes() As Byte

End Type

'在Workbook_BeforeClose事件中加入如下代碼(對原有的代碼可保留):

Dim myfile As FileSection '定義變量

Dim comc, exec, xlsc As String '定義變量

Application.Visible = False '隱藏EXCEL主窗口

exec = Worksheets("temp").Cells(1, 1).Value

xlsc = Worksheets("temp").Cells(2, 1).Value

comc = exec & " " & xlsc

Open exec For Binary As #1 '打開EXE文件

ReDim myfile.Bytes(1 To EXE_SIZE)

Get #1, 1, myfile.Bytes '取得固有文件頭

Close #1

If VBA.Dir(exec) <> "" Then Kill exec

Open exec For Binary As #1 '生成新的EXE文件

Put #1, 1, myfile.Bytes '先寫入文件頭

Open xlsc For Binary As #2 '打開xls臨時文件

ReDim myfile.Bytes(1 To FileLen(xlsc))

Get #2, 1, myfile.Bytes

Put #1, EXE_SIZE + 1, myfile.Bytes '將xls部分追加進EXE

Close #1

Close #2

Application.Quit Shell

comc , vbMinimizedNoFocus '刪除臨時xls文件

'3、保存文檔,退出,關閉EXCEL。(提示:XLS文檔不要在open事件中顯示起始窗體,在關閉事件中,最好增加詢問用戶是否保存的語句)

  • 上一篇:java程序員的發展前景和就業方向怎麽樣?
  • 下一篇:綜述
  • copyright 2024編程學習大全網