首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

张志晨VB实例教程之打开word步骤种种

2012-07-27 
张志晨VB实例教程之打开word方法种种张志晨VB实例教程之打开word方法种种 2011年08月31日  张志晨VB实例教

张志晨VB实例教程之打开word方法种种

张志晨VB实例教程之打开word方法种种
2011年08月31日
  张志晨VB实例教程之打开word方法种种
  方法一:
  Dim mWordapp As Word.Application   'word 应用程序
  Dim mobjDoc As Word.Document       'word 文档
  Dim fullFileName As String         '文件路径
  Private Sub Command1_Click()
  CommonDialog1.Filter = "word文件|*.docx"
  CommonDialog1.ShowOpen
  fullFileName = CommonDialog1.FileName
    Set mWordapp = CreateObject("Word.Application")
    Set mobjDoc = mWordapp.Documents.Add(fullFileName)
    mWordapp.Visible = True
  End Sub
  ................................................
  方法二:
  Dim fullFileName As String         '文件路径
  Private Sub Command1_Click()
  CommonDialog1.Filter = "word文件|*.docx"
  CommonDialog1.ShowOpen
  fullFileName = CommonDialog1.FileName       
   Shell "cmd /c " & fullFileName, vbHide'使用shell.exe与cdm.exe
  End Sub
  ..................................................
  方法三:
  Dim fullFileName As String         '文件路径
  Private Sub Command1_Click()
  CommonDialog1.Filter = "word文件|*.docx"
  CommonDialog1.ShowOpen
  fullFileName = CommonDialog1.FileName
  Shell "C:\Program Files\Microsoft Office\OFFICE11\WINWORD.EXE " & fullFileName  , vbNormalFocus
  End Sub
  ........................................
  方法四:
  Private Declare Function GetDesktopWindow Lib "user32" () As LongPrivate Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
  Private Const SW_SHOWNORMAL = 1
  '函数:OpenFile
  '功能:打开文件或者打开网站或者打开邮件
  '描述:asPath――文件位置或者网站地址或者对方邮件地址;Line――命令行
  '备注:如果是邮件地址,请在地址前加mailto: 例如:mailto:53076924@qq.com
  '返回值如果是2代表文件(夹)不存在,如果是33代表打开文件夹成功,42代表打开文件或网络地址成功,31代表没有文件与之关联
  '****************************************************************************
  Public Function OpenFile(asPath As String, Optional Line As String = vbNullString, Optional ShowMode As Long = 1) As Long
  Dim Scr_hDC As Long
  Scr_hDC = GetDesktopWindow()
  OpenFile = ShellExecute(Scr_hDC, "Open", asPath, Line, GetFileOfFolder(asPath), ShowMode)
  End Function
  Private Function GetFileOfFolder(FilePath As String) As String
  Dim i As Integer
  If FilePath = "" Then Exit Function
  For i = Len(FilePath) To 1 Step -1
  If Mid$(FilePath, i, 1) = "" Then
  GetFileOfFolder = Left$(FilePath, i - 1)
  Exit For
  End If
  Next
  End Function
  上面的是函数的定义,下面的是第四种方法的使用(可以打开任何类型的文件、文件夹):
  Private Sub Command1_Click()
  Dim fn As String
  CommonDialog1.Filter = "word文件(*.docx)|*.docx|所有文件(*.*)|*.*"
  CommonDialog1.ShowOpen
  fn = CommonDialog1.FileName
  OpenFile (fn)'只需要给函数一个file path就可以了!!!
  End Sub
  ...............张志晨奉献............

热点排行