Rabu, 26 November 2014

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

 

Graham
Maybe it's a holdover from earlier languages that don't use declarations.

Bill

Sent from my Kindle Fire




From: "'Graham Mandeno' graham@mandeno.com [MS_Access_Professionals]" <MS_Access_Professionals@yahoogroups.com>
Sent: Wed Nov 26 14:24:11 PST 2014
To: MS_Access_Professionals@yahoogroups.com
Subject: RE: [MS_AccessPros] Re: Convert Active Word Doc to PDF

 

Indeed!  Thanks, Bill J

I will never understand why implicit declaration persists in being the default.  It would be very easy to make "Require variable declaration" the default for all new installations, but MS have steadfastly refused to do so.

Cheers,
Graham

 

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

 

 

What Graham didn't mention is that you can automatically add Option Explicit to all new modules by changing a setting in the VBA editor. Tools>Options>Editor tab: check the box for Require Variable Declaration. You only have to do it once and it will stay on for all databases.

 

Bill Mosca



---In MS_Access_Professionals@yahoogroups.com, <graham@mandeno.com> wrote :

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_Ac

__._,_.___

Posted by: Bill Mosca <wrmosca@comcast.net>
Reply via web post Reply to sender Reply to group Start a New Topic Messages in this topic (9)

.

__,_._,___

Tidak ada komentar:

Posting Komentar