当前位置:编程学习 > VB >>

vb6.0 先保存后直接打开的代码

'保存文件 Function SaveFile() As String '保存文件 Dim obj As Object Set obj = CreateObject("MSComDlg.CommonDialog") With obj .Filter = "word 文档|*.doc" .ShowSave If Len(.FileName) = 0 Then Exit Function SaveFile = .FileName End With End Function '通过以上代码可以实现保存文件,并可以选择路径 下面我需要的就是通过command1来实现打开刚才保存的文件,是直接打开文件,不是通过选择打开
答案:这样真的能保存么?好像不行。
打开就简单了:
在窗体代码最前面定义
Private 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
Dim strFile
在调用你的保存函数时用
strFile = SaveFile
(题外话:自己写word文档的保存?好强大哦!VB只能用open strfile for output as #1:print#1,xxx:close#1等类似方法写入文件。你可以引用Word库试一下。我没研究过)
然后打开:因为你的文件并没有真的保存,所以找不到文件,不会有任何提示。你可以用一个已知的文件名替代 strFile 试一下
Private Sub Command1_Click()
    ShellExecute 0, "open", strFile, vbNullChar, vbNullChar, 0
End Sub 

'保存WORD,引用word库先
Function SaveFile() As String '保存文件
Dim obj As Object
Set obj = CreateObject("MSComDlg.CommonDialog")
With obj
.Filter = "word 文档|*.doc"
.ShowSave
If Len(.FileName) = 0 Then Exit Function
SaveFile = .FileName
End With

Dim wdApp As Word.Application
Dim wdBook As Word.Document
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then Set wdApp = CreateObject("Word.Application")
wdApp.Visible = True '设置Excel 可见
On Error GoTo 0
Set wdBook = wdApp.Documents.Add  
'……你要保存的内容
wdBook.SaveAs SaveFile 
wdApp.Quit
Set wdBook = Nothing
Set wdApp = Nothing
End Function

上一个:谁有曲柄摇杆机构的VB程序代码?急急急!!!!
下一个:VB中,用Skin控件加载Skn皮肤,当运行的时候,还是原来的windows样式。怎么办

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,