供你参考
程序只有一个form,其上有一个text。我想实现
的功能如下
首先调用miscrosoft word或其他文字处理软件,
新建一个文档,然后运行程序,在文本框中输入文本。
如果我这时候点击word的话(激活),怎么将文本框
中输入的文字粘贴到word中。直接粘贴,不用ctrl-v.
在"唯一"的form窗体中再添加一个timer控件(interval=200,如果运行不正常,可适当加大此值), (当然还
有一个text),接着代码如下∶
Private Declare Function GetForegroundWindow& Lib "user32" ()
Private Declare Function GetWindowText& Lib "user32" Alias "GetWindowTextA" _
(ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long)
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte,
_
ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Declare Function MapVirtualKey& Lib "user32" Alias "MapVirtualKeyA" _
(ByVal wCode As Long, ByVal wMapType As Long)Private Const VK_CONTROL& = &H11 'ctrl键的虚拟键值
Private Const KEYEVENTF_KEYUP% = &H2
Private Const VK_V& = &H56 'V键的ascii码值(虚拟键值)Private Sub Text1_Change()
Timer1.Enabled = True
Clipboard.Clear
Clipboard.SetText Form1.Text1.Text
End SubPrivate Sub Timer1_Timer()
Dim dl1&, dl2&
Dim Canp As String * 255
dl1& = GetForegroundWindow&() '获取当前活动窗口的句柄
dl2& = GetWindowText&(dl1&, Canp$, 255) '获取窗口的标题
If Left(Canp$, 14) = "Microsoft Word" Then
MyCapture 1
Timer1.Enabled = False
End If
End Sub
Public Sub MyCapture(ByVal mode%) '生成一次模拟击键∶Ctrl+V
Dim altscan%
Dim dl&
Dim snapparam%
altscan% = MapVirtualKey(VK_CONTROL, 0)
keybd_event VK_CONTROL, altscan, 0, 0
keybd_event VK_V, 0, 0, 0
keybd_event VK_CONTROL, altscan, KEYEVENTF_KEYUP, 0
End Sub
注∶本例程只适用于microsoft word.要适用于其他文字处理程序,请
修改程序中的 If Left(Canp$, 14) = "Microsoft Word" Then 条件检测。
程序只有一个form,其上有一个text。我想实现
的功能如下
首先调用miscrosoft word或其他文字处理软件,
新建一个文档,然后运行程序,在文本框中输入文本。
如果我这时候点击word的话(激活),怎么将文本框
中输入的文字粘贴到word中。直接粘贴,不用ctrl-v.
在"唯一"的form窗体中再添加一个timer控件(interval=200,如果运行不正常,可适当加大此值), (当然还
有一个text),接着代码如下∶
Private Declare Function GetForegroundWindow& Lib "user32" ()
Private Declare Function GetWindowText& Lib "user32" Alias "GetWindowTextA" _
(ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long)
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte,
_
ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Declare Function MapVirtualKey& Lib "user32" Alias "MapVirtualKeyA" _
(ByVal wCode As Long, ByVal wMapType As Long)Private Const VK_CONTROL& = &H11 'ctrl键的虚拟键值
Private Const KEYEVENTF_KEYUP% = &H2
Private Const VK_V& = &H56 'V键的ascii码值(虚拟键值)Private Sub Text1_Change()
Timer1.Enabled = True
Clipboard.Clear
Clipboard.SetText Form1.Text1.Text
End SubPrivate Sub Timer1_Timer()
Dim dl1&, dl2&
Dim Canp As String * 255
dl1& = GetForegroundWindow&() '获取当前活动窗口的句柄
dl2& = GetWindowText&(dl1&, Canp$, 255) '获取窗口的标题
If Left(Canp$, 14) = "Microsoft Word" Then
MyCapture 1
Timer1.Enabled = False
End If
End Sub
Public Sub MyCapture(ByVal mode%) '生成一次模拟击键∶Ctrl+V
Dim altscan%
Dim dl&
Dim snapparam%
altscan% = MapVirtualKey(VK_CONTROL, 0)
keybd_event VK_CONTROL, altscan, 0, 0
keybd_event VK_V, 0, 0, 0
keybd_event VK_CONTROL, altscan, KEYEVENTF_KEYUP, 0
End Sub
注∶本例程只适用于microsoft word.要适用于其他文字处理程序,请
修改程序中的 If Left(Canp$, 14) = "Microsoft Word" Then 条件检测。
dim wordlong as long
dim tmplng as long
dim wordvar
dim tmpvar
wordlong=rs!word.autualsize
do while tmplong<wordlong
tmpvar=rs!word.GetChunk(1000)
wordvar=wordvar&tmpvar
tmplng=tmplng+1000
loop
Dim WordA As Word.Application
Dim doc As Word.Document
Set WordA = New Word.Application
Set doc = WordA.Documents.Add
doc.Content = wordvar
WordA.Visible = True
如果WORD文档的全部文件字节数据在rs!word中,则先把字节数据存放到一个临时文件,扩展名为.doc,实现是很简单的,我就不说了。
然后执行下面代码:
Set WordA = New Word.Application
WordA.Documents.Open "mytmpfile.doc"'完整路径
WordA.Visible = True
Dim mydoc As Word.Document
dim aselection as Word.Selection
dim arange As Word.Range
dim fpath as string
fpath = "c:\test.doc"
Set appwd = CreateObject("word.application") '创建 word application 对象
With appwd
.Visible = False
Set mydoc = .Documents.Add(, , wdNewBlankDocument, True) '新建word文档
Call mydoc.Activate
Set aselection = .Selection
Set arange = .ActiveDocument.Range(0, 1)
arange.Select
aselection.Style = "正文"
aselection.Font.name = "宋体"
aselection.Font.Size = 11
aselection.Font.Bold = false
dim title as string
while not rs.eof
title = rs.fields("title").value 'rs->recordset
aselection.InsertAfter (" " +rtrim(title))
aselection.TypeParagraph
wend
mydoc.SaveAs Trim(fpath)
end with
mydoc.close
set appwd = nothing
mydoc.close
set appwd = nothing
Private Sub Command1_Click()
' Declare our variables
Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim oRange As Word.Range
Dim oConn As ADODB.Connection
Dim oRS As ADODB.Recordset
Dim sTemp As String ' Create an instance of Word
Set oWord = CreateObject("Word.Application")
' Show Word to the user
oWord.Visible = True
' Add a new, blank document
Set oDoc = oWord.Documents.Add
' Get the current document's range object
Set oRange = oDoc.Range ' Create a new ADO connection
Set oConn = CreateObject("ADODB.Connection")
' Open our connect
oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
sFileName & ";Persist Security Info=False"
' Execute a SQL statement to retrieve the information
Set oRS = oConn.Execute( _
"SELECT CustomerID, CompanyName, ContactName FROM Customers")
' Use GetString to return the recordset as a string
sTemp = oRS.GetString(adClipString, -1, vbTab) ' Insert a heading on the string
sTemp = "Customer ID" & vbTab & "Company Name" & _
vbTab & "Contact Name" & vbCrLf & sTemp
' Insert the data into the Word document
oRange.Text = sTemp
' Convert the text to a table and format the table
oRange.ConvertToTable vbTab, , , , wdTableFormatColorful2
End Sub 详细信息请参考
Q261999 HOWTO: Transfer an ADO Recordset to a Word Table with Automation http://support.microsoft.com/support/kb/articles/q261/9/99.asp
- 微软全球技术中心 VB技术支持本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款
(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查
(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。