Rabu, 26 November 2014

RE: [MS_AccessPros] Re: Convert Active Word Doc to PDF

 

Hi Norbert

In addition to what Bill said, you clearly do not have ActiveDocument declared, so your code should have failed to even compile, IF you had followed best practice of using explicit variable declaration.

EVERY SINGLE code module in your project should have as its first line Option Explicit

This means that every variable must be explicitly declared – for example:

Dim Wrd As Object
Dim strMergeDoc As String
… etc

Without this, any previously unknown variable will be implicitly declared. This means that this code will compile and run just fine:

strDirWord = \\192.168.164.4\c$\WORDSERVER\EXHIBIT LIST - MASTERS\ <file:///\\192.168.164.4\c$\WORDSERVER\EXHIBIT%20LIST%20-%20MASTERS\>
strFileWordName = "Exhibits " & Me.ClientName & " " & Me.txtClaimNo & ".pdf"
strFileWord = strDirWrd & strFileWordName

[Note the difficult-to-spot typo in the third line.]

This code will find the previously unused variable strDirWrd and create it with a variant value of <empty>. This will all happen without complaint, with the result that your final variable strFileWord will not have any directory part. It could take you months to figure out why your DFs were not ending up on the server!

With Option Explicit the code will not compile and therefore will never run. Similarly, your undeclared variable ActiveDocument in the next line will raise the alarm.

If you have many modules with many implicitly declared variables, then adding Option Explicit will be a pain, but I assure you it will avoid hours of grief in the future!

Another point to note is that macros recorded in Word make extensive use of context-related objects such as ActiveDocument and Selection. It's a good idea, when automating word from another application, to replace these references with object variables (Document or Range in these cases) which are not context-specific.

For example, I would replace these first few lines:

Set Wrd = CreateObject("Word.Application")
strMergeDoc = Application.CurrentProject.path
strMergeDoc = strMergeDoc + "\Exhibits.dotx"
Wrd.Documents.Add strMergeDoc
With Wrd.ActiveDocument.Bookmarks

… with this:

Dim Wrd as Object
Dim WrdDoc as Object
Dim strMergeDoc as String
Set Wrd = CreateObject("Word.Application")
strMergeDoc = Application.CurrentProject.path
strMergeDoc = strMergeDoc & "\Exhibits.dotx"
Set WrdDoc = Wrd.Documents.Add strMergeDoc
With WrdDoc.Bookmarks

Then replace all other instances of Wrd.ActiveDocument with WrdDoc.

Good luck!

Graham

_____

Graham Mandeno
Microsoft Access MVP 1996 - 2014

From: MS_Access_Professionals@yahoogroups.com [mailto:MS_Access_Professionals@yahoogroups.com]
Sent: Thursday, 27 November 2014 02:54
To: MS_Access_Professionals@yahoogroups.com
Subject: [MS_AccessPros] Re: Convert Active Word Doc to PDF

Hi Bill,

Thanks for your suggestion.

This is the code in msaccess:

Private Sub cmdExhibits_Click()

Set Wrd = CreateObject("Word.Application")

strMergeDoc = Application.CurrentProject.path

strMergeDoc = strMergeDoc + "\Exhibits.dotx"

Wrd.Documents.Add strMergeDoc

With Wrd.ActiveDocument.Bookmarks

.Item("EXHIBITSLIST").Range.Text = strExhibits

.Item("WITNESSLIST").Range.Text = strWitnesses

.Item("CASENO").Range.Text = Me.txtClaimNo

.Item("CLIENTNAME").Range.Text = Me.ClientName

End With

 

strDBPath = CurrentDb.Name

strDBFile = Dir(strDBPath)

dbdir = Left$(strDBPath, Len(strDBPath) - Len(strDBFile))

strFileTempWord = dbdir & "TempWordDoc.Doc"

DoEvents

If FileExists(strFileTempWord) Then Kill strFileTempWord

Wrd.ActiveDocument.SaveAs strFileTempWord

strDirWord = "\\192.168.164.4\c$\WORDSERVER\EXHIBIT LIST - MASTERS\ <file:///\\192.168.164.4\c$\WORDSERVER\EXHIBIT%20LIST%20-%20MASTERS\> "

strFileWordName = "Exhibits " & Me.ClientName & " " & Me.txtClaimNo & ".pdf"

strFileWord = strDirWord & strFileWordName

'20141121----------------------------------------convert word to pdf-------------------------------

ActiveDocument.ExportAsFixedFormat OutputFileName:= _

strFileWord, ExportFormat:= _

wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _

wdExportOptimizeForPrint, Range:=wdExportAllDocument, _

Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _

CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _

BitmapMissingFonts:=True, UseISO19005_1:=False

Wrd.Quit

Set Wrd = Nothing

strDirWord = "\\server2008\WORDSERVER\EXHIBIT LIST - MASTERS\ <file:///\\server2008\WORDSERVER\EXHIBIT%20LIST%20-%20MASTERS\> "

strFileWordName = "Exhibits " & Me.ClientName & " " & Me.txtClaimNo & ".doc"

strFileWord = strDirWord & strFileWordName

If FileExists(strFileTempWord) Then Call CopyFile(strFileTempWord, strFileWord)

On Error GoTo 0

Exit Sub

cmdExhibits_Click_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure cmdExhibits_Click of VBA Document Form_frmExhibits"

End Sub

----------------------------------------------------------

Warmest regards

Norbert

[Non-text portions of this message have been removed]

__._,_.___

Posted by: "Graham Mandeno" <graham@mandeno.com>
Reply via web post Reply to sender Reply to group Start a New Topic Messages in this topic (5)

.

__,_._,___

Tidak ada komentar:

Posting Komentar