View Full Version : Solved: Pass Word doc reference in Access

06-08-2007, 09:57 AM
Hello, all,

Okay, I have been working for a while with code in MS Access that creates and formats documents in MS Word. Trouble is, all my experience involves the document's variable being instantiated and closed within just one function.

Now I have a procedure that is building a 400+ page Word document with Access data. The original code has become so long that MS Access refuses to even run it. I just get an error message that says "Procedure too long."

So, I am attempting to split the original procedure into several functions where one function calls the next. The part I'm not sure of is how to pass a reference to the document from one function to the next.

Function 1
Dim objWord as Word.Application, docWord as Word.Document
Set objWord = New Word.Application
Set docWord = objWord.Documents.Add("MyTemplate.dot")
' do lots of stuff in here to start manipulating the document
' call the next function and pass [what?] so that it can manipulate the same document

I tried saving the file with a certain name, then passing that name to the next function by using something like objWord.Documents(strFilename), but it just kept saying "Bad file name." I also tried defining the second function as Function2(docWord as Word.document) and then calling it with Call Function2(docWord). That got me a "ByRef type mismatch" error.

I know there is probably a simple way to do this, but I am clueless. I just can't seem to get myself to the solution. I appreciate any insight that the far more experienced folks here can offer.

By the way, I am working with Access and Word 2003 in Windows 2000 Pro. Thanks a million!
~ eed

06-08-2007, 10:23 AM
Hi Eed,
You can definatly pass as Word.Document or Word.Application. If you are getting a mismatch error double check you didn't accidently Declare an argument as Something other then they object or Vice-Versa.

06-08-2007, 10:44 AM

Thanks for the reply. I double-checked my declarations, and they seemed okay, but I was still getting the error.

Then I tried creating just a super stripped-down pair of functions, where I was literally doing nothing but creating the doc in one, passing its reference to the other, and displaying a message box to prove it worked.

And THAT worked, with passing a variable for Word.Document. So, obviously, I must have something else going on in the midst of my code that is messing me up, rather than the reference passing. I guess I will start copying and pasting my original code into my new stripped-down module, one step at a time, until something makes it choke.

Thanks for your help, Aaron. I haven't found the real source of my problem yet, but at least now I know the correct method for passing the reference to the document!

~ eed

06-08-2007, 11:10 AM
Oookay, well that only took a minute to find the real problem: I'm just a bit of a ditz.

I conveniently forgot that, when I call the second procedure, I actually have an If/ElseIf/Then statement which calls one of two similar procedures, based on a certain condition. I had updated the declarations in ONE of the two possible procedures being called, but not in the OTHER one. That's what was kicking back the "ByRef Type Mismatch" error, because the other possible procedure that could be called had not been set to accept the Word.Document variable being passed.

Okay, well, I feel retarded, but at least I can get everything working now. :o: Thanks for your help, Aaron.