If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. |
|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
Accessing Word from Microsoft Access
I am trying to copy a routine written by Alan Simpson to print a contacts address directly to a word document. I have created the word document with bookmarks for FullName, Street, Address, and Salutation. When I run the routine, the compiler balks on the 'with' items, saying 'invalid use of property'. I know the routine finds the word document in question, because if I rem out all the with items, the document is displayed. Routine is given below. Any help appreciated. Private Sub cmdMergetoWord_Click() Dim strFullName, strSalutation, strStreet, strAddress As String Dim Wrd As New Word.Application Set Wrd = CreateObject("Word.Application") Dim strMergeDoc As String strMergeDoc = "C:\Users\rip\Documents\Data\word\OneContact.D ot" 'Open the word document, make it visible Wrd.Documents.Add strMergeDoc Wrd.Visible = True With Wrd.ActiveDocument.Bookmarks .item("FullName") = strFullName .item("Street") = strStreet .item("Address") = strAddress .item("Salutation") = strSalutation End With Wrd.ActiveDocument.PrintPreview End Sub Rip West Saint Paul, MN |
#2
|
|||
|
|||
Accessing Word from Microsoft Access
I haven't seen anywhere in your code where you set a value on your
variables. What's the value of strFullName, etc.? -- Arvin Meyer, MCP, MVP http://www.datastrat.com http://www.mvps.org/access http://www.accessmvp.com "Rip West" wrote in message ... I am trying to copy a routine written by Alan Simpson to print a contacts address directly to a word document. I have created the word document with bookmarks for FullName, Street, Address, and Salutation. When I run the routine, the compiler balks on the 'with' items, saying 'invalid use of property'. I know the routine finds the word document in question, because if I rem out all the with items, the document is displayed. Routine is given below. Any help appreciated. Private Sub cmdMergetoWord_Click() Dim strFullName, strSalutation, strStreet, strAddress As String Dim Wrd As New Word.Application Set Wrd = CreateObject("Word.Application") Dim strMergeDoc As String strMergeDoc = "C:\Users\rip\Documents\Data\word\OneContact.D ot" 'Open the word document, make it visible Wrd.Documents.Add strMergeDoc Wrd.Visible = True With Wrd.ActiveDocument.Bookmarks .item("FullName") = strFullName .item("Street") = strStreet .item("Address") = strAddress .item("Salutation") = strSalutation End With Wrd.ActiveDocument.PrintPreview End Sub Rip West Saint Paul, MN |
#3
|
|||
|
|||
Accessing Word from Microsoft Access
In your code example you don't show where you setup strtreet etc. Just
looking at your code, we have quite few things to fix. Without getting a lot more details here, you have to fix a good of things. Remember, defining something as strAddress, or strZoo don't make a differnce. You are NOT setting strZoo, or strAddress ANYWHERE in your code. I going to suggest that you also drop the use of bookmarks. They are a royal pain, force you to hard code both your code and the document. (each time you change something, you have to change your code). I would suggest you use the built-in word merge as it is far more easy. (I also suspect you are somewhat new to coding, and thus bookmarks are going to be quite a challenge for you. To solve all of your woes, I going to suggest you use my super easy word merge. The sample I have can be found he http://www.members.shaw.ca/AlbertKal.../msaccess.html What is nice/interesting about my sample is that is specially designed to enable ANY form with ONE LINE of code to merge the ONE record you are looking at. Thus, each time you build a new form, you can word merge enable it with great ease. And, you can build new document without bookmarks. Make sure you read the instructions from above, and you should eventually get to the following page http://www.members.shaw.ca/AlbertKal...rge/page2.html Note that the merge can also use a query, and thus you don't have to merge just "one" record.. After the merge occurs, you get a plain document WITHOUT any merge fields, and this allows the end user to save, edit, or even email the document (since the merge fields are gone after the merge occurs). If you following the instructions step by step, you get a working merge.... So, give my sample a try, it is quite automated, and you DO NOT have to write more then ONE line of code to do your merge, and you don't have to insert bookmarks into your word docuemnt. -- Albert D. Kallal (Access MVP) Edmonton, Alberta Canada |
#4
|
|||
|
|||
Accessing Word from Microsoft Access
Albert,
Thank you for your reply. Actually, I was familiar with your routine, and was going to try that as well. But before doing that, I wanted to understand why the compiler balks on my routine. As you pointed out, the variables were never set up. I have fixed that, but that is not the reason that I get the error message. The error message points to the with statements, but it never compliles at all. I would really like to know what else is wrong with the routine. I do intend to try your routine. It sounds exactly what I am looking for, but, at this point, I am trying to learn something. Any help would be greatly appreciated. Another mistake I just made was in responding to you personally, rather than to the forum. I apologize for that. Rip West Saint Paul, MN -- Rip West Saint Paul, MN |
#5
|
|||
|
|||
Accessing Word from Microsoft Access
Arvin, hank you for your reply. I have corrected the variables problem.
Silly mistake. But that is not the reason that I get the error message. The error message points to the with statements, but it never compliles at all. I would really like to know what else is wrong with the routine. Any help would be greatly appreciated. Rip West Saint Paul, MN |
#6
|
|||
|
|||
Accessing Word from Microsoft Access
"Rip West" wrote in message
... Albert, I wanted to understand why the compiler balks on my routine. ok, lets take a look at the code: When I pasted the code into a form, and when I try to comple, the first thing that went wrong was: Dim Wrd As New Word.Application So, to "check" the code, you can (and should) go debug-compile. So, to fix the above, we have set a reference to word. While looking at the code, you go tools-references Then scroll down to Microsoft word 11.0 object libary Note that 11 is for office 2003, 10 for office 2002. You have to choose whatever version you are using. if you move your application and code to run on a different machine, then you will again have to change this reference. This tend to be a pain, and for that reason one should use a more advanced coding technique called late binding, (my super merge does this). This means your code will run on any computer with any version of word annual not have to set a reference (However let's just leave this issue of early or late binding for another post and another learning session). Ok, now lets try to compile again (debug-compile). This process of compiling over and over is appropriately called "compiling and debugging" code. So, there is a good reason the compile option is under the debug menu so when you're debugging and trying to fix code you'll be compiling over and over all day long as you work your way to fix problems in the syntax of your code. Ok, when I try to compile again I get an error on the following line With Wrd.ActiveDocument.Bookmarks .Item("FullName") = strFullName ---- error message The compile error I'm am receiving is: --------------------------- Microsoft Visual Basic --------------------------- Compile error: Invalid use of property --------------------------- OK Help --------------------------- (as a side note most times when you are looking at a dialog box, you can hit control C. and the contents of that dialog box who will be copied into your paste item, I did not have to type the above error text into this message, I simply hit control c, then came back to this message and then pressed control v to paste the above). Ok, So at this point the only property I am using is something called ".item" Item is a read only propoerty, and can't be modifed. However, looking at other bookmark examples, we see that there is somthing called "selection", and that's what we need to use. (rememember, I never used bookmark merge, so this all new to me also). Anway, the follwing seems to work better: .Item("FullName").Range.Text = strFullName So, you have to use .Range.Text to represent what the actual text for the bookmark will become. Continue to make the above changes and each time you make a change to your code, try going debug-compile to see if your changes now actually compile. Eventually your code should compile. However the fact that compiles correctly doesn't necessarily mean it'll run correctly. We now have to work and find what's called runtime errors. For example our code can compile now but if you haven't set the value of strFullName, then your code still will not function correctly now. I'm not sure where you got the code example to use ".item", but clearly using ".item" was incorrect and clearly it did not compile.... -- Albert D. Kallal (Access MVP) Edmonton, Alberta Canada |
#7
|
|||
|
|||
Accessing Word from Microsoft Access
Albert,
Thanks so much for taking the time to demystify me. I really appreciate it. The routine works, but I agree with you that your solution is better, so I am on to that. Rip West Saint Paul, MN -- Rip West Saint Paul, MN |
#8
|
|||
|
|||
Accessing Word from Microsoft Access
SQL Server 2008 Reporting Services has free 'export report to word'
functionality (in addition to Excel, PDF, HTML, TIFF, etc) you shoudn't have to write code to do this-- this is built in functionality (for a real database) On Dec 14, 3:13*pm, "Rip West" wrote: *I am trying to copy a routine written by Alan Simpson to print a contacts address directly to a word document. I have created the word document with bookmarks for FullName, Street, Address, and Salutation. When I run the routine, the compiler balks on the *'with' items, saying 'invalid use of property'. I know the routine finds the word document in question, because if I rem out all the with items, the document is displayed. Routine is given below. Any help appreciated. Private Sub cmdMergetoWord_Click() * * Dim strFullName, strSalutation, strStreet, strAddress As String * * Dim Wrd As New Word.Application * * Set Wrd = CreateObject("Word.Application") * * Dim strMergeDoc As String * * strMergeDoc = "C:\Users\rip\Documents\Data\word\OneContact.D ot" * * 'Open the word document, make it visible * * Wrd.Documents.Add strMergeDoc * * Wrd.Visible = True * * With Wrd.ActiveDocument.Bookmarks * * * * .item("FullName") = strFullName * * * * .item("Street") = strStreet * * * * .item("Address") = strAddress * * * * .item("Salutation") = strSalutation * * End With * * Wrd.ActiveDocument.PrintPreview End Sub Rip West Saint Paul, MN |
Thread Tools | |
Display Modes | |
|
|