ボタンを私が欲しいものにするのが難しいです。私は特定のプロジェクトのすべての情報を入力するMS Accessデータベースを持っています。私はまた、このdbにメールを差し込んだ契約書を持っています。Accessのボタンに機能を追加したい
これまでのところ皆さんの助けを借りて私はこれまでのところ(以下のコード)を得ています。それは動作しますが、ボタンが押されたときに120レコードがあると、120レコードすべてで長い契約が作成されます。私は単に現在のレコード(当時の私の画面上のレコード)をpdfだけ作成したいだけです。
作成されたpdfには、「製品名 - クライアント名」などの命名規則を使用することもできます。どちらもレコードのフィールドです。
私は追加したい私は毎日これを行うすべての想像力、名声のストレッチでコーダーではありません....あなたは未知のヒーローです。
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\...\Google Drive\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 = "[Product Name]_" & Format(Now(), "mmddyyyy")
' 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]" ' 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
これは* "私はDA CODEZ" *の質問の*とあなたがhav eは「あなたの一部の助けを借りていました」*「ヘルパー」と呼ばれている人たちは、私たちがここでやっていることに対する誤った期待をあなたに与えました。このような質問をして[ask]をレビューし、おそらく[mcve]を提供する前に、ぜひお試しください。これを済ませたら、これらのガイドラインを反映させるために質問を編集してください。 – Lankymart