excel vba-打开先前保存的文档

时间:2018-11-04 21:56:25

标签: excel vba reference ms-word

我真的迷上了这个,不知道从哪里开始。还是VBA的新手。

我得到了一个代码,该代码使用书签将用户窗体中的文本框值写入到Word文档中。然后,单击提交后,此文档将保存到C:\ documents。

基本上,我要达到的目的是使用用户窗体上的另一个命令按钮,根据我先前输入的值之一打开保存的文件,并在需要时修改文档。

这有可能吗?

谢谢。

当我单击“提交”按钮时,这就是我可以从用户窗体写入Word doc的功能。但是我添加了另一个命令按钮以根据用户窗体示例中的文本框值来调用该文档,用户窗体textbox1的值为3。因此,当我单击“编辑命令”按钮时,它将调用其内容中具有该值的文档,即bookmark1 = 3

        '----------The Script below writes values to the document ----------------------- 
     -----------------

    Dim wApp As Object
    Dim wDoc As Object

    'We need to continue through errors since if Word isn't
    'open the GetObject line will give an error

    On Error Resume Next
    Set wApp = GetObject(, "Word.Application")


'We've tried to get Word but if it's nothing then it isn't open
If wApp Is Nothing Then
    Set wApp = CreateObject("Word.Application")
End If

'It's good practice to reset error warnings
On Error GoTo 0

'Open your document and ensure its visible and activate after openning

Set wDoc = 
wApp.Documents.Open(Filename:="C:\Documents\template.docx ", 
ReadOnly:=False)
    With wDoc
    .Bookmarks("bookmark1").Range.Text = Me.TextBox1.Value
    .Bookmarks("bookmark2").Range.Text = Me.TextBox3.Value
    .Bookmarks("bookmark3").Range.Text = Me.TextBox4.Value
    .Bookmarks("bookmark4").Range.Text = Me.TextBox5.Value
    .Bookmarks("bookmark5").Range.Text = Me.TextBox6.Value
    .Bookmarks("bookmark6").Range.Text = Me.TextBox7.Value
    .Bookmarks("bookmark7").Range.Text = Me.TextBox8.Value
    End With

wApp.Visible = True

'set default file name and file path

ProposedFileName = Format(Now(), "DDMMMYYYY") &  
TextBox1.Value & "-" & TextBox2.Value & ".doc"
ProposedFilePath = "C:\Users\Documents\"

    With wApp.FileDialog(msoFileDialogSaveAs)
    wDoc.SaveAs2 ProposedFilePath & ProposedFileName, _
    FilterIndex = 1, _
    FileFormat:=wdFormatDocument

    End With   
End Sub

在另一个用户窗体上,有一个编辑按钮,单击该按钮可从上述脚本中调用保存的文档。也许像下面这样?

Sub EditButton_Cick()
Set wDoc = wApp.Documents.Open(Filename:="C:\Documents\SavedDoc.docx ", 
ReadOnly:=False) 'here is where I want to open the previous saved document located in C:\Documents from the script in my initial question however it should open the` doc referenced to textbox1.value
    With wDoc
    .Bookmarks("bookmark10").Range.Text = Me.TextBox10.Value 'In the userform, I add in another value later to the saved document

   objDoc.Save

End Sub

1 个答案:

答案 0 :(得分:0)

赞:

在常规代码模块的顶部:

Dim savePath As String '<< global variable for saved file path

在您的第一个用户表单中:

Sub cmdSave_Click()
    '...
    '...
    savePath = ProposedFilePath & ProposedFileName  '<< save to Global var

    wDoc.SaveAs2 ProposedFilePath & ProposedFileName, _
        FilterIndex = 1, _
        FileFormat:=wdFormatDocument
    '...
    '...
End Sub

在以后的用户表单中:

Sub cmdReOpenFile_Click()

    Dim doc
    '...
    Set doc = wApp.Documents.Open(savePath) '<< read from global var
    '... work with doc

End Sub

仅供参考,您似乎正在使用后期绑定,因此您需要定义任何Word常量,例如wdFormatDocument

如果将Option Explicit放在代码模块的顶部,它将警告您类似的事情。