Question : Access 2003 -> Word 2003 - have a question on code that works to save doc as pdf file...

Hi EE,

Below in the code window is logic to loop thru a recordset
                                             create a page for each selected student
                                                       (checked off by user on a screen)
                                             save the entire doc to pdf

problem:
am selecting 2 students
am correctly producing 2 pages in the word doc
-but-
     the pdf has an extra blank 3 rd page

do you have any idea why i have a blank page at the end?

(when i manually do a ctl+p  in the word doc and choose adobe printer, the pdf correctly gets
  generated as 2 page)

tx for your ideas and advice, sandra
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
325:
326:
327:
328:
329:
330:
331:
332:
333:
334:
335:
336:
337:
338:
339:
340:
341:
342:
343:
344:
345:
346:
347:
348:
349:
Option Compare Database

Option Explicit
 
Public Sub cmdWordEvaluations_Click_CHECKBOXES(strTableName_Students As String, _
                                              strBackLabelStatus As Boolean)
On Error GoTo ErrorHandler

'  objects

Dim appWord                As Word.Application
Dim wdDocs                 As Word.Documents
Dim wdDoc                  As Word.Document

Dim wdImage                As Object
Dim wdTable                As Word.Table

Dim wdPrps                 As Object
Dim wdPrp                  As Object

Dim db                     As DAO.Database
Dim rst                    As DAO.Recordset

Dim objPDF_Distiller       As PdfDistiller

' strings
Dim strBackSql             As String
Dim strTest                As String
Dim strImageFile           As String
Dim strSubPDFFolder        As String

Dim strTitleName           As String

Dim strWordSourceFile      As String
Dim strWordBlankFile       As String
Dim strWordResultFile      As String

Dim strPDFfile             As String
Dim strPSfile              As String
Dim strLOGfile             As String

Dim strSourceFullPath      As String
Dim strBlankFullPath       As String
Dim strResultFullPath      As String

Dim strMsg                 As String
Dim strrst                 As String

' longs
Dim lngCount               As Long
Dim lngResponse            As Long
Dim lngWordCount           As Long
Dim lngBegCount            As Long
Dim lngBackRowCount        As Long
Dim lngPos                 As Long
Dim lngResult              As Long
Dim lngTablePos            As Long

' integers
Dim intTableCount          As Integer 'GLS
Dim intRecordNumber        As Integer 'GLS


' booleans
Dim blnBackGoodIO          As Boolean


Set db = CurrentDb()
'=================================================
' return msgbox of number of Checked Students detected
'=================================================

Call q823_Select_Table_Students_COUNT_Checkboxes _
                                    (strTableName_Students, _
                                     blnBackGoodIO, _
                                     lngBackRowCount)
Debug.Print lngBackRowCount    '      Recordset.Fields("ROWCOUNT") is the number of rows...

If lngBackRowCount = 0 Then
   MsgBox "No students to process"
   GoTo finishup
Else
    strMsg = lngBackRowCount & _
                  " Evaluation Report(s) sending to Word"
    strBackLabelStatus = True
    lngResponse = MsgBox(strMsg, vbInformation + vbOKCancel + vbDefaultButton1, _
                         "Please be advised...")
    If lngResponse = vbCancel Then
        strBackLabelStatus = False
        GoTo finishup
    End If
 End If
 
' ============================
' set record source to retrieve checked students
' ============================

Call q925_set_sql_rst_CHECKBOXES _
                           (strTableName_Students, _
                            strBackSql)
Set rst = db.OpenRecordset(strBackSql)
strrst = "y"

' ============================
' Calculate Word wdDoc to be retrieved and open it
' ============================

'set word wdDoc name

'note: if you change properties of FIELDS doc, than copy FIELDS doc to test doc
'      delete everything from test doc, and then save it as BLANK doc

strWordSourceFile = "xxTemplate_Electives_EvaluationForm_FIELDS.doc"
 strWordBlankFile = "xxTemplate_Electives_EvaluationForm_BLANK.doc"

strSourceFullPath = CurrentProject.Path & "\" & strWordSourceFile
strBlankFullPath = CurrentProject.Path & "\" & strWordBlankFile

strSubPDFFolder = "Evaluation_ for_Clerkship"
Debug.Print "Opening document based on strTemplate: " & strSourceFullPath

'Open Word
Set appWord = GetObject(, "Word.Application")
appWord.Visible = True
   
Set wdDocs = appWord.Documents

'Open blank "template" file
wdDocs.Open strBlankFullPath

Set wdDoc = appWord.ActiveDocument


'====
   'Loop through table, exporting each record to Word
   ' http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Word/Q_21152049.html?sfQueryTermInfo=1+selection.inlineshapes.addpictur
   ' http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Word/Q_23785214.html?sfQueryTermInfo=1+cell+popul+tabl+word
   
Do Until rst.EOF
    intRecordNumber = intRecordNumber + 1
    strTitleName = rst!S_Title
    
    ' insert page break after page 1
    If intRecordNumber > 1 Then
        wdDoc.Bookmarks("\EndOfDoc").Range.InsertBreak wdSectionBreakNextPage
    End If
    
    ' insert source document into blank doc
    wdDoc.Bookmarks("\EndOfDoc").Range.InsertFile strSourceFullPath 'GLS
        
    ' calculate number of tables in the document on the first page
    If intRecordNumber = 1 Then 'GLS
        intTableCount = wdDoc.Tables.Count 'GLS
        
        Debug.Print wdDoc.Tables.Count 'GLS
    End If 'GLS
    
    Debug.Print rst!S_Lname & " " & rst!S_Fname; " " & rst!S_Id
     

    ' ======================
    ' display Properties for testing purposes
    ' ======================

    Set wdPrps = wdDoc.CustomDocumentProperties 'GLS
    For Each wdPrp In wdPrps
       Debug.Print wdPrp.Name & " : " & wdPrp.Value
    Next
    
    ' ======================
    ' populate Properties and update document
    ' ======================
    With wdPrps
        .Item("w_a_StudentName").Value = rst!S_Lname & ", " & rst!S_Fname
        .Item("w_b_Subject").Value = rst!S_Subject
        .Item("w_c_CourseTitle").Value = rst!S_Title
        .Item("w_d_CourseNumber").Value = rst!S_Course_Number
        .Item("w_e_CAD").Value = rst!S_Advisor_Lname & ", " & rst!S_Advisor_Fname
        .Item("w_f_StartDate").Value = rst!S_StartDate
        .Item("w_g_EndDate").Value = rst!S_EndDate
        
        .Item("w_h_InstructorName").Value = rst!S_Instructor_Fname & " " & rst!S_Instructor_Lname
        .Item("w_i_StreetLine1").Value = rst!S_street1
        .Item("w_j_StreetLine2").Value = rst!S_street2
       ' .Item("w_k_StreetLine3").Value = rst!S_street3
        .Item("w_l_City").Value = rst!S_city
        .Item("w_m_State").Value = rst!S_state
        .Item("w_n_Zip").Value = rst!S_zip
        .Item("w_o_CRN").Value = rst!S_CRN

        
    End With
    
    ' ======================
    ' update Properties and convert to text, so that props cannot be re-updated
    ' ======================
    
    wdDoc.fields.Update 'GLS
    wdDoc.fields.Unlink 'GLS convert fields to text
        
    '========================
    ' position to first element of table to insert picture
    '========================
    ' add in line image
    
    
    strImageFile = "O:\COM Photos\MED Pics Banner\" & _
                         rst!S_Id & ".jpg"
    
    If dir(strImageFile) <> "" Then
    Else
       strImageFile = "O:\COM Photos\MED Pics Banner\" & _
                         "a_missingPhoto.jpg"
    End If

                         
    'http://www.experts-exchange.com/Microsoft/Development/MS_Access/Access_Coding-Macros/Q_23702385.html?sfQueryTermInfo=1+cell+imag+insert+tabl+word
    
'    calculate table position for new page, otherwise will keep inserting picture into
'    the first table on the first page

     lngTablePos = 1 + intTableCount * (intRecordNumber - 1)

    
     Set wdTable = wdDoc.Tables(lngTablePos) 'GLS
    
'  insert picture into the 1st element of the table
     wdDoc.Range.InlineShapes.AddPicture strImageFile, _
                                        False, True, wdTable.Cell(1, 1).Range
                                            
nextrec:

    rst.MoveNext

Loop
   
'======================
' save as word doc file
'======================

strWordResultFile = Format(Date, "YYYY_MM_DD") & "_" & strTitleName & ".doc"
                   
strResultFullPath = CurrentProject.Path & "\" & strWordResultFile

'* Delete the existing file
If dir(strResultFullPath) <> "" Then
  Kill (strResultFullPath)
End If

wdDoc.SaveAs strResultFullPath

'=========================
' save as pdf file if Adobe is on the pc
'=========================
If IsAdobeInstalled = True Then

' save default printer settings
   pg_strDefaultPrinter = appWord.ActivePrinter
    
' Create PS, LOG, PDF file names from Excel file name
   lngPos = InStr(strResultFullPath, ".")
   strPSfile = Left(strResultFullPath, lngPos) & "ps"
   strLOGfile = Left(strResultFullPath, lngPos) & "log"
   strPDFfile = Left(strResultFullPath, lngPos) & "pdf"
    
   Debug.Print strPSfile
   Debug.Print strLOGfile
   Debug.Print strPDFfile
    
' override default printer with Adobe printer
   appWord.ActivePrinter = "Adobe PDF on LPT2:"
    
' remember to set:   Tools/references Acrobat distiller
    

  Set objPDF_Distiller = New PdfDistiller
      
' print is really an export to pdf
    
  appWord.PrintOut , Copies:=1, _
                 PrintToFile:=True, OutputFileName:=strPSfile
     
' Convert Acrobat PS file to PDF file
   lngResult = objPDF_Distiller.FileToPDF(strPSfile, strPDFfile, "")
    
' delete work files
   Kill strPSfile
   Kill strLOGfile
    
   Set objPDF_Distiller = Nothing
                                      
' Restore the default printer settings
   appWord.ActivePrinter = pg_strDefaultPrinter

End If

wdDoc.Close savechanges:=False
appWord.Quit

finishup:
If strrst = "y" Then
   rst.Close
End If

db.Close

Set appWord = Nothing
Set wdDocs = Nothing
Set wdDoc = Nothing

Set wdImage = Nothing
Set wdTable = Nothing

Set wdPrps = Nothing
Set wdPrp = Nothing

Set objPDF_Distiller = Nothing

Set db = Nothing
Set rst = Nothing


ErrorHandlerExit:
   Exit Sub
ErrorHandler:
   If Err = 429 Then
      'Word is not running; open Word with CreateObject
      Set appWord = CreateObject("Word.Application")
      Resume Next
   Else
      MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
      Resume ErrorHandlerExit
   End If
End Sub


Public Function IsAdobeInstalled() As Boolean
Dim strTemp As String

IsAdobeInstalled = False

strTemp = dir("C:\Program Files\Adobe\acrobat*", vbDirectory)

Do Until strTemp = ""
   IsAdobeInstalled = True
   strTemp = dir()
Loop

End Function

Answer : Access 2003 -> Word 2003 - have a question on code that works to save doc as pdf file...

Yes, when setting image, you may try setting the size.  I think the code is attached with two lines for sizing:
    ActiveSheet.Pictures.Insert("F:\Users\DDaneke\Pictures\DSC00059.jpg").Select
    Selection.ShapeRange.Height = 144
    Selection.ShapeRange.ScaleHeight 0.1, msoFalse, msoScaleFromTopLeft

HEIGHT sets the size, SCALEHEIGHT is a percentage.- pick one to work for you.
1:
2:
3:
ActiveSheet.Pictures.Insert("Filename.jpg").Select
    Selection.ShapeRange.Height = 144
    Selection.ShapeRange.ScaleHeight 0.1, msoFalse, msoScaleFromTopLeft
Random Solutions  
 
programming4us programming4us