龙空技术网

vba中实现多线程最好用的模板

竹林一樵 930

前言:

现在朋友们对“vb60 多线程”大致比较讲究,我们都需要学习一些“vb60 多线程”的相关知识。那么小编在网络上收集了一些对于“vb60 多线程””的相关内容,希望姐妹们能喜欢,看官们一起来了解一下吧!

不可能人人都是程序员和专业技术人才,所以对普通文员来说,只需要把有用的代码搬过来做成模板,在实际工作中大胆套用就可以了,这样可以节省很多时间精力。以操作word文档为例,代码如下:

Private Const INFINITE = &HFFFFFFFF

Private Declare Function WaitForSingleObject Lib "kernel32" _

(ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long

Private Declare Function CreateThread Lib "kernel32" _

(ByVal LpThreadAttributes As Long, _

ByVal DwStackSize As Long, _

ByVal LpStartAddress As Long, _

ByVal LpParameter As Long, _

ByVal dwCreationFlags As Long, _

ByRef LpThreadld As Long) As Long

Private Declare Function CloseHandle Lib "kernel32" _

(ByVal HANDLE As Long) As Long

Sub Main()

Dim x As Long

x = CreateThread(nil, 0, AddressOf secondary, nil, 0, nil) '主线程

Application.AutomationSecurity = msoAutomationSecurityForceDisable '启用宏

a = MsgBox("必须先启用宏,否则无法使用word文档", vbOKOnly, "快点")

If a = vbOK Then

End If

Dim yyy As Date, www As Date

Dim so, t As Long

Do

yyy = Time

www = #9:25:00 PM#

so = Abs(DateDiff("s", yyy, www))

If so <= 0 Then

MsgBox "时间到!"

Exit Sub

End If

If so > 0 Then

hh = Round(so / 3600, 0)

mm = Int((so Mod 3600) / 60)

ss = Int((so Mod 3600) Mod 60)

ActiveWindow.Caption = "离解除密码的时间还有:" _

& hh & "小时" & mm & "分" & ss & "秒" '显示倒计时

End If

t = Int(Timer)

Do While Int(Timer) = t

DoEvents

Loop

Loop

Call WaitForSingleObject(x, INFINITE)

CloseHandle x

End Sub

Sub secondary() '输入密码,正确才能使用

Dim wordapp As Word.Application

Dim closebl As Boolean, Document As Word.Document

Dim strPassword As String

On Error Resume Next

Set wordapp = GetObject(, "word.application")

Dim i As Integer

If Err.Number <> 0 Then

Set wordapp = CreateObject("word.application")

closebl = True

End If

Set Document = wordapp.ActiveDocument

Do

strPassword = InputBox("请输入密码")

If strPassword = "123456" Then

MsgBox "密码正确,将进入word文档"

Exit Do

Exit Sub

ThisDocument.Activate

Else

MsgBox "密码有误,重新输入,不能超过三次", vbCritical + vbInformation, "错误"

End If

i = i + 1

Loop While i <= 3

If i > 3 Then

MsgBox "已超过三次了,滚!", vbInformation, "将要退出WORD文档"

ThisDocument.Close

ActiveDocument.Visible = False

End If

DoEvents

End Sub

说明:

在word文档中,同时实施这两个任务:1,强制启用宏,然后显示倒计时,一直显示下去。2,出现对话框,要求输入密码,否则word文档锁定不可用 。密码正确就进入word文档使用,如果不正确就提示继续输入,超过三次则退出word 文档。

把黑体加粗的部分复制,然后换两个过程就可以用了。

个人观点,水平有限,欢迎指正,赐教!

运行效果:

视频加载中...

标签: #vb60 多线程 #vb timer 多线程