我用VB在指定文件夹里读取一个word模版,里面是一个表,把数据库的内容写入word中,写可以成功了。
现在问题是保存,我想设定直接保存在指定的文件夹中。先贴出我现在完成的代码:    Dim Appword As Word.Application
    Dim Newword As Word.Document
    Set Appword = New Word.Application
    Set Newword = Appword.Documents.Add(App.Path + "\stencil" + "\stencil.doc")
    Appword.Visible = True
    Appword.WindowState = wdWindowStateMinimize
    Appword.Documents.Add    With Newword
            .Tables(1).Cell(1, 1).Range.Text = txtName.Text
    End With
        Appword.ChangeFileOpenDirectory (App.Path + "\Result")        Appword.ActiveDocument.SaveAs FileName:=(txtName.Text & ".doc"), FileFormat:=wdFormatDocument,  LockComments:=False, Password:="", AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:=False        Appword.Documents.Close        Appword.Quit
        MsgBox "导出Word文件成功!"
运行结果是弹出一个保存界面,读取文档的第一行字为文件名,然后按保存才能保存成功。问题1:但是由Appword.ActiveDocument.SaveAs FileName:=(txtName.Text & ".doc"), 代码控制保存的文档内却没有内容。问题2:如果已经开启了一个word文档的话,会弹出无法保存的错误。
亟待解决,谢谢高手指教!

解决方案 »

  1.   

    可以先设置一个默认路径(比如根目录)将要写入的Word 文档写入!
    然后,再将文件移动到指定的文件夹中即可!
      

  2.   

     这样试试有内容吗?
       
     Appword.ActiveDocument.SaveAs FileName:="c:\111.doc"
      

  3.   

    Dim Appword As Word.Application 
        Dim Newword As Word.Document 
        Set Appword = New Word.Application 
        Set Newword = Appword.Documents.Add(App.Path + "\stencil" + "\stencil.doc") 
        Appword.Visible = True 
        Appword.WindowState = wdWindowStateMinimize 
        'Appword.Documents.Add '去掉这句
        With Newword 
                .Tables(1).Cell(1, 1).Range.Text = txtName.Text 
        End With 
            Appword.ChangeFileOpenDirectory (App.Path + "\Result")         Appword.ActiveDocument.SaveAs FileName:=(txtName.Text & ".doc"), FileFormat:=wdFormatDocument,  LockComments:=False, Password:="", AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:=False         Appword.Documents.Close         Appword.Quit 
            MsgBox "导出Word文件成功!" 
      

  4.   

    chinaboyzyq
    正解,昨天晚上我测试后已经行了。非常感谢。