在Access中创建按钮以打开Word文档 [英] Creating a button in Access to to open a word document

查看:216
本文介绍了在Access中创建按钮以打开Word文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Word文档,该文档使用邮件合并功能并从Access数据库中获取其信息.当我使用此代码时,它不会使用当前信息打开word文档.它将打开单词文档以及最近保存的信息.

I have a word document that uses mail merge feature and gets its information from the access db. When I use this code it does not open the word document with the current information. It opens the word document with the last saved information.

如果我从任务栏上自己打开Word文档,它会询问我是否要运行SQL,然后单击是",一切将正常运行.我想从访问权限中单击一个按钮以完成同一任务以打开合同.

If I open the word document on its own, from the task bar, it asks if I want to run the SQL and I click yes and everything operates normally. I want to click a button from within access to accomplish this same task to open the contract.

这是我使用的代码:

Private Sub Command205_Click()

Dim LWordDoc As String
Dim oApp As Object

'Path to the word document
LWordDoc = "C:\Users\.....k Up\01- Proposal\contract.docx"

If Dir(LWordDoc) = "" Then
  MsgBox "Document not found."

Else
  'Create an instance of MS Word
  Set oApp = CreateObject(Class:="Word.Application")
  oApp.Visible = True

  'Open the Document
  oApp.Documents.Open FileName:=LWordDoc
End If

End Sub

***我要补充一点,我不是编码人员,也不了解VBA,我从本网站复制了此内容,因此,您能提供的任何帮助将不胜感激.如果您可以为我提供编码或足够的指导以帮助我上路,那将是不错的选择.谢谢

***I should add that I am not a coder and know nothing about VBA, I copied this from this website so any help you can offer would be greatly appreciated. If you can provide me with coding or enough guidance to get me on the way would be great. Thank you

推荐答案

此代码将在Access中运行,以打开邮件合并"文档并更新内容并保存.

This code will run in Access to open a Mail Merge document and update content and save.

使用我最初发布的链接( http://www.minnesotaithub.com/2015/11/automatic-mail-merge-with-vba-and-access/),我进行了一些修改并能够使该代码正常工作

Using the link I originally posted (http://www.minnesotaithub.com/2015/11/automatic-mail-merge-with-vba-and-access/), I made a couple of modifications and was able to get that code to work.

我需要添加:ReadOnly:= True,_以防止共享冲突 然后我更改了源数据的表名.

I needed to add: ReadOnly:=True, _ to prevent a sharing violation and I changed the Table Name of the source data.

注意!!您将需要更改标有"###"的鞋底,如下所示:

NOTE!! You will need to change sode marked with'###' as follows:

###-1进行更改以指定模板的完整路径!

###-1 Change to specify the full path of your TEMPLATE!!!

###-2更改SQLSTATEMENT以指定您的记录源!!!

###-2 Change the SQLSTATEMENT to specify your recordsource!!!

将此代码粘贴到窗体中,确保您具有要执行的命令按钮单击事件"(在此代码中重命名"Command205",或更改控件名称).

Paste this code into your form, make sure you have a Command Button Click Event that executes (Either rename 'Command205' in this code, or change your control name).

Option Compare Database
Option Explicit

Private Sub Command205_Click()
Dim strWordDoc  As String

    'Path to the word document of the Mail Merge
    '###-1 CHANGE THE FOLLOWING LINE TO POINT TO YOUR DOCUMENT!!
    strWordDoc = "C:\Users\.....k Up\01- Proposal\contract.docx"

    ' Call the code to merge the latest info
    startMerge strWordDoc

End Sub


'----------------------------------------------------
' Auto Mail Merge With VBA and Access (Early Binding)
'----------------------------------------------------
' NOTE: To use this code, you must reference
' The Microsoft Word 14.0 (or current version)
' Object Library by clicking menu Tools > References
' Check the box for:
' Microsoft Word 14.0 Object Library in Word 2010
' Microsoft Word 15.0 Object Library in Word 2013
' Click OK
'----------------------------------------------------
Function startMerge(strDocPath As String)
    Dim oWord           As Word.Application
    Dim oWdoc           As Word.Document
    Dim wdInputName     As String
    Dim wdOutputName    As String
    Dim outFileName     As String

    ' Set Template Path
    wdInputName = strDocPath            ' was CurrentProject.Path & "\mail_merge.docx"

    ' Create unique save filename with minutes and seconds to prevent overwrite
    outFileName = "MailMergeFile_" & Format(Now(), "yyyymmddmms")

    ' Output File Path w/outFileName
    wdOutputName = CurrentProject.Path & "\" & outFileName

    Set oWord = New Word.Application
    Set oWdoc = oWord.Documents.Open(wdInputName)

    ' Start mail merge

    '###-2 CHANGE THE SQLSTATEMENT AS NEEDED
    With oWdoc.MailMerge
        .MainDocumentType = wdFormLetters
        .OpenDataSource _
            Name:=CurrentProject.FullName, _
            ReadOnly:=True, _
            AddToRecentFiles:=False, _
            LinkToSource:=True, _
            Connection:="QUERY mailmerge", _
            SQLStatement:="SELECT * FROM [tblEmployee]"         ' Change the table name or your query
        .Destination = wdSendToNewDocument
        .Execute Pause:=False
    End With

    ' Hide Word During Merge
    oWord.Visible = False

    ' Save file as PDF
    ' Uncomment the line below and comment out
    ' the line below "Save file as Word Document"
    '------------------------------------------------
    'oWord.ActiveDocument.SaveAs2 wdOutputName & ".pdf", 17

    ' Save file as Word Document
    ' ###-3 IF YOU DON'T WANT TO SAVE AS A NEW NAME, COMMENT OUT NEXT LINE
    oWord.ActiveDocument.SaveAs2 wdOutputName & ".docx", 16

    ' SHOW THE DOCUMENT
    oWord.Visible = True

    ' Close the template file
    If oWord.Documents(1).FullName = strDocPath Then
        oWord.Documents(1).Close savechanges:=False
    ElseIf oWord.Documents(2).FullName = strDocPath Then
        oWord.Documents(2).Close savechanges:=False
    Else
        MsgBox "Well, this should never happen! Only expected two documents to be open"
    End If

    ' Quit Word to Save Memory
    'oWord.Quit savechanges:=False

    ' Clean up memory
    '------------------------------------------------
    Set oWord = Nothing
    Set oWdoc = Nothing

End Function

这篇关于在Access中创建按钮以打开Word文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆