让按钮正常工作 [英] Getting a button to work properly
问题描述
按钮执行我想要的操作时遇到困难。我有一个MS Access数据库,我输入了特定项目的所有信息。我还有一个合同word文档,它是与此db合并的邮件。
到目前为止,在你们中的一些人的帮助下,我已经做到了这一点(下面的代码)。它工作正常,但如果我按下按钮时有120条记录,则会创建一个包含所有120条记录的长合约。我只想把当前的记录(当时我的屏幕上的记录
)只制作一个pdf。
我还想改变文件的位置保存到。
选项比较数据库
选项明确
私有子Command205_Click()
Dim strWordDoc As String
'Mail Merge的word文档路径
'### - 1更改以下行以指向您的文档!
strWordDoc =" C:\ Users \ ..... \01- Proposal\contract.docx"
'调用代码合并最新信息
startMerge strWordDoc
结束次级
'---- ------------------------------------------------
'自动邮件合并VBA和访问(早期绑定)
'------------------------------- ---------------------
'注意:要使用此代码,您必须引用
'Microsoft Word 14.0(或当前版本)单击菜单工具>
'对象库参考
'选中以下框:
'Word 2010中的Microsoft Word 14.0对象库
'Word Word中的Microsoft Word 15.0对象库
'单击确定
' - -------------------------------------------------- -
函数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
'设置模板路径
wdInputName = strDocPath'是CurrentProject.Path& " \mail_merge.docx"
'用分钟和秒创建唯一的保存文件名以防止覆盖
outFileName = Me.Product_Name.Value& " - " &安培; Me.Client_Name.Value
'输出文件路径w / outFileName
wdOutputName = CurrentProject.Path& " \" &安培; outFileName
设置oWord =新Word.Application
设置oWdoc = oWord.Documents.Open(wdInputName)
'开始邮件合并
'### - 2根据需要更改SQLSTATEMENT
使用oWdoc.MailMerge
.MainDocumentType = wdFormLetters
.OpenDataSource _
Name:= CurrentProject.FullName,_
ReadOnly:= True,_
AddToRecentFiles:= False,_
LinkToSource:= True,_
Connection:=" QUERY mailmerge",_
SQLStatement:=" SELECT * FROM [合同信息]其中Id =" &安培; Me.ID.Value& "" '更改表名或您的查询'
。目的= wdSendToNewDocument
。执行暂停:=错误
结束使用
'合并时隐藏单词
oWord.Visible = False
'将文件另存为PDF
'取消注释以下行并注释掉
'下面的行"将文件另存为Word文档"
'--------------------------------------------- ---
oWord.ActiveDocument.SaveAs2 wdOutputName& " .pdf",17
'将文件另存为Word文档
'### - 3如果您不想保存为新名称,请注意下一行
'oWord.ActiveDocument.SaveAs2 wdOutputName& " .docx",16
'显示文档
oWord.Visible = True
'关闭模板文件
如果oWord.Documents( 1).FullName = strDocPath然后
oWord.Documents(1)。关闭savechanges:= False
ElseIf oWord.Documents(2).FullName = strDocPath然后
oWord.Documents(2)。关闭savechanges:= False
Else
MsgBox"嗯,这绝不会发生!只有预期的两个文件才能打开"
结束如果
'退出Word以节省内存
'oWord.Quit savechanges:= False
'清理内存
' - -----------------------------------------------
设置oWord = Nothing
设置oWdoc = Nothing
结束功能
我只想让当前的记录(当时我的屏幕上的记录)只能制作一个pdf。
将记录输出为PDF文件的最简单方法是创建一个报告,其RecordSource属性是一个引用表单绑定控件的查询以相关表格的
主键作为参数。
然后调用DoCmd对象的OutputTo方法。
您可以在我的公共数据库文件夹中的InvoicePDF.zip中找到一个示例:
https://onedrive.live.com/?cid=44CC60D7FEA42912&id=44CC60D7FEA42912!169
请注意,如果您使用的是早期版本的Access,您可能会发现某些表单对象(如按钮)的颜色显示不正确,您需要
来相应地修改表单设计。
如果您无法打开链接复制其文本(NB,而不是链接位置)并将其粘贴到浏览器的地址栏中。
< p style ="margin:0px">
这个小小的演示文件使用以下内容用于将当前发票输出为以客户名称和发票编号命名的PDF文件的代码。
在我的情况下,文件将保存到文件夹中的子文件夹中,该文件夹的路径在首次启动时以开头的形式输入。
Private Sub cmdPDF_Click()
错误GoTo Err_Handler
Const FOLDER_EXISTS = 75
Const MESSAGE_TEXT1 =" No current invoice。"
Const MESSAGE_TEXT2 ="没有用于存储PDF文件的文件夹。"
Dim strFullPath As String
Dim varFolder As Variant
如果不是IsNull(Me.InvoiceNumber)那么
'构建保存PDF文件的路径
varFolder = DLookup(" Folderpath"," pdfFolder")
如果IsNull(varFolder)那么
MsgBox MESSAGE_TEXT2,vbExclamation,"无效操作"
其他
'创建文件夹(如果不存在)
varFolder = varFolder& " \" &安培; Me.Customer.Column(1)
MkDir varFolder
strFullPath = varFolder& " \" &安培; Me.Customer.Column(1)& " " &安培; Me.InvoiceNumber& " .pdf"
'确保在创建PDF文件之前保存当前记录
&NBSP;
Me.Dirty = False
DoCmd.OutputTo acOutputReport,"rptInvoice",acFormatPDF,strFullPath,True
结束如果
其他
MsgBox MESSAGE_TEXT1,vbExclamation,"无效操作"
结束如果
Exit_Here:
退出子
Err_Handler:
Select Case Err.Number
案例FOLDER_EXISTS
继续下一步
Case Else
MsgBox Err.Description
恢复退出_Here
结束选择
End Sub
I am having difficulties having a button perform what I want. I have an MS Access db which I input all of the information for a particular project. I also have a contract word document which is mail merged with this db.
So far with the help of some of you I've gotten this far (code below). It works, but if I have 120 records when the button is pressed it creates a long contract with all 120 records. I simply want to have just the current record (the record on my screen at the time) to only make a pdf.
I would also like to change where the file is saved to.
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\.....\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 = Me.Product_Name.Value & " - " & Me.Client_Name.Value ' 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 [Contract Information] Where Id = " & Me.ID.Value & "" ' 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
I simply want to have just the current record (the record on my screen at the time) to only make a pdf.
The simplest way to output a record as a PDF file is to create a report whose RecordSource property is a query which references a control on the form bound to the primary key of the relevant table as a parameter. Then call the OutputTo method of the DoCmd object. You'll find an example in InvoicePDF.zip in my public databases folder at:
https://onedrive.live.com/?cid=44CC60D7FEA42912&id=44CC60D7FEA42912!169
Note that if you are using an earlier version of Access you might find that the colour of some form objects such as buttons shows incorrectly and you will need to amend the form design accordingly.
If you have difficulty opening the link copy its text (NB, not the link location) and paste it into your browser's address bar.
This little demo file uses the following code to output the current invoice as a PDF file named with the customer name and invoice number. In my case the file is saved to a subfolder in a folder whose path is entered in the opening form at first start-up.
Private Sub cmdPDF_Click()
On Error GoTo Err_Handler
Const FOLDER_EXISTS = 75
Const MESSAGE_TEXT1 = "No current invoice."
Const MESSAGE_TEXT2 = "No folder set for storing PDF files."
Dim strFullPath As String
Dim varFolder As Variant
If Not IsNull(Me.InvoiceNumber) Then
' build path to save PDF file
varFolder = DLookup("Folderpath", "pdfFolder")
If IsNull(varFolder) Then
MsgBox MESSAGE_TEXT2, vbExclamation, "Invalid Operation"
Else
' create folder if does not exist
varFolder = varFolder & "\" & Me.Customer.Column(1)
MkDir varFolder
strFullPath = varFolder & "\" & Me.Customer.Column(1) & " " & Me.InvoiceNumber & ".pdf"
' ensure current record is saved before creating PDF file
Me.Dirty = False
DoCmd.OutputTo acOutputReport, "rptInvoice", acFormatPDF, strFullPath, True
End If
Else
MsgBox MESSAGE_TEXT1, vbExclamation, "Invalid Operation"
End If
Exit_Here:
Exit Sub
Err_Handler:
Select Case Err.Number
Case FOLDER_EXISTS
Resume Next
Case Else
MsgBox Err.Description
Resume Exit_Here
End Select
End Sub
这篇关于让按钮正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!