永发信息网

VB 程序 :如何让已经打开的Word文件前端显示?

答案:2  悬赏:0  手机版
解决时间 2021-02-22 21:15
  • 提问者网友:ミ烙印ゝ
  • 2021-02-22 18:27
我做了一个程序,运行VB时 会打开一个指定的 word文档;如果在运行VB时,这个word文档已经打开,但没有前端显示;如何在运行vb时,使该word文档 前端显示?
最佳答案
  • 五星知识达人网友:等灯
  • 2021-02-22 18:38
通过在打开之前进行窗口查找,如果查找成功,则调用SetWindowPos 置顶窗体,否则打开
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 FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Const HWND_TOPMOST = -1
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOSIZE = &H1
Private Const SWP_SHOWWINDOW = &H40

Private Sub Form_Load()
Dim n As Long, m As Long
Dim sName As String
sName = "1.doc" & " - Microsoft Word" '这里的1.doc 改成你的文件名
n = FindWindow(vbNullString, sName)
If n <> 0 Then
m = SetWindowPos(n, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_SHOWWINDOW)
Else
'打开Word代码
End If
End Sub
全部回答
  • 1楼网友:孤独入客枕
  • 2021-02-22 18:59
首先引用 microsoft word library 代码: ‘==================== dim xword as word.application private sub cmdreplace_click() dim xword as word.application dim xrange as range dim xselection as find dim xtabella as table dim xcella as cell set xword = new application xword.visible = false xword.documents.add app.path & "\新概念单词表3.rtf" set xrange = xword.activedocument.range xrange.find.execute "%%xxx%%", , , , , , , , , text1.text, true: set xrange = xword.activedocument.range xrange.find.execute "%%aaa%%", , , , , , , , , text2.text, true: xword.visible = true xword.windowstate = wdwindowstatenormal xword.application.activate end sub ’================ ‘"%%xxx%%" 、"%%aaa%%" 就是需要替换的内容
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯