By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,728 Members | 2,462 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,728 IT Pros & Developers. It's quick & easy.

Printing a single record to a word document

P: 3
I have my word document setup with bookmarks and it will print out all of my records each on a separate page. What I am wanting to do is select which record to print and dumping the rest of the records and not save the extra documents.
Expand|Select|Wrap|Line Numbers
  1. Public Sub btnPrintInv_Click()
  2. Dim wApp As Word.Application
  3. Dim wDoc As Word.Document
  4. Dim rsInvoice As Recordset
  5.     Set wApp = New Word.Application
  6.     Set wDoc = wApp.Documents.Open("C:\DECA Database\DECAInv.docx")
  7.     Set rsInvoice = CurrentDb.OpenRecordset("qryHGInv")
  9.     If Not rsInvoice.EOF Then rsInvoice.MoveFirst
  11.         Do Until rsInvoice.EOF
  12.             wDoc.Bookmarks("MTicket").Range.Text = Nz(rsInvoice!Mticket, "")
  13.             wDoc.Bookmarks("TCustomerNumber").Range.Text = Nz(rsInvoice!TCustomerNumber, "")
  14.             wDoc.Bookmarks("TCustomerName").Range.Text = Nz(rsInvoice!TCustomerName, "")
  15.             wDoc.Bookmarks("CName").Range.Text = Nz(rsInvoice!CName, "")
  16.             wDoc.Bookmarks("Street").Range.Text = Nz(rsInvoice!Street, "")
  17.             wDoc.Bookmarks("City").Range.Text = Nz(rsInvoice!City, "")
  18.             wDoc.Bookmarks("State").Range.Text = Nz(rsInvoice!State, "")
  19.             wDoc.Bookmarks("Zip").Range.Text = Nz(rsInvoice!Zip, "")
  20.             wDoc.Bookmarks("Phone").Range.Text = Nz(rsInvoice!Phone, "")
  21.             wDoc.Bookmarks("Suite").Range.Text = Nz(rsInvoice!Suite, "")
  22.             wDoc.Saveas2 "C:\DECA Database\" & rsInvoice!Mticket & "Inv" & Format(Now(), "mmddyy") & ".docx"
  23.                 rsInvoice.MoveNext
  25.         Loop
  26.             wDoc.PrintOut
  28.             wApp.Quit
  29.             Set wApp = Nothing
  30.             Set wDoc = Nothing
  31. End Sub
Apr 4 '19 #1
Share this Question
Share on Google+
6 Replies

Expert Mod 2.5K+
P: 3,283

Welcome to Bytes!

What I am wanting to do is select which record to print and dumping the rest of the records and not save the extra documents.
I'm not exactly sure what your request is here. If you are looking for exclusive records, then simply set the appropriate criteria in your Query qryHGInv (or use a different query).

I'm not sure what you mean by "dumping the rest of the records" unless you simply mean not using any of them.

Hope this hepps!
Apr 4 '19 #2

Expert Mod 15k+
P: 31,489
Too much wrong with the code to cover all now but using CurrentDb() directly is likely to cause you problems.
In line with what Twinny's already suggested you might like to change things to something like :
Expand|Select|Wrap|Line Numbers
  1. 'Insert after line #1
  2. Dim strSQL As String
  3. 'Instead of your line #7
  4.     strSQL = Replace("SELECT * FROM [qryHGInv] WHERE ([YourField]=%V)" _
  5.                    , "%V", XXX)
  6.     Set rsInvoice = CurrentDb().OpenRecordset(strSQL)
Whatever field you use to determine the page you're after is what you'll use for [YourField] and XXX is whatever reference you have to the value of that.
%V is fine as it stands if the value is numeric but dates and string literal values are treated differently so you may need to adjust to handle (See Literal DateTimes and Their Delimiters (#)).

Good luck.
Apr 4 '19 #3

P: 3
I have 15 customer records. The code I am using produces 15 documents. I only want to select and print 1 customer record not all 15.
Apr 4 '19 #4

Expert Mod 15k+
P: 31,489
Just to be clear, that was in response to Twinny's question in his post. Well done for answering such questions.

Now you need to consider where you're at in response to everything that's been posted.
Apr 4 '19 #5

P: 3
I am not as sharp as most of you on this subject. I will just stick with the ugly InputBox to extricate the record I want to print on my word document.
Apr 5 '19 #6

Expert Mod 2.5K+
P: 3,283
Just to be clear, the “ugly InputBox” might not necessarily be an “ugly” solution. However, the key that solves this problem is reducing the record set from “all records” to “one record”. Many ways to do that, but that is, in fact, the answer.
Apr 6 '19 #7

Post your reply

Sign in to post your reply or Sign up for a free account.