473,324 Members | 2,370 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Need help opening/populating a word template from Access

I'm new to VBA, and trying to learn how to write some code to open a word template from access and populate it using Document Properties. Here's my code:

Expand|Select|Wrap|Line Numbers
  1. Private Sub OpenWindows_Click()
  2.     Dim Word As Object
  3.     Dim doc As Word.Document
  4.     Dim prps As Object
  5.     Dim strLastName As String
  6.     Dim lngContactID As Long
  7.     Dim strTitle As String
  8.     Dim strPrompt As String
  9.     Dim Selection As Selection
  10.  
  11.     lngContactID = Nz(Me![ID])
  12.     If lngContactID = 0 Then
  13.         strPrompt = "No contact selected"
  14.         strTitle = "Problem"
  15.         MsgBox strPrompt, vbOKOnly + vbExclamation, strTitle
  16.     Else
  17.         strLastName = Nz(Me![LastName])
  18.     End If
  19.  
  20.     Set Word = CreateObject("Word.Application")
  21.     Word.Documents.Open CurrentProject.Path & "\test.dot"
  22.  
  23.     Set prps = doc.CustomDocumentProperties
  24.     prps.Item("LastName").Value = strLastName
  25.  
  26.     With Word
  27.         .Selection.WholeStory
  28.         .Selection.Fields.Update
  29.         .Selection.HomeKey unit:=wdStory
  30.     End With
  31.  
  32.     Word.Visible = True
  33.  
  34. End Sub
The document opens, but I get an error message:
"Run-time error '91':
Object Variable or With block variable not set.

When I debug it, line 23 shows the error.

Any guidance would be much appreciated.
Nov 12 '08 #1
3 6274
MikeTheBike
639 Expert 512MB
Hi

Just another guess, but you do not seem to have set the 'doc' object variable equal to any document object, hence the error message ??


MTB
Nov 13 '08 #2
southoz
24
take a look at this thread it'll tell you what you need to know.

http://bytes.com/forum/thread559954-southoz.html

southoz
Nov 13 '08 #3
missinglinq
3,532 Expert 2GB
Auslander,

"New to VBA" says absolutely nothing about your problem here! Ive re-titled your thread so that it actually reflects the question at hand. Having a title that does this clearly is important for two reasons.

First, it allows members, at a glance, to understand the nature of the question being asked. Thus, people who have never opened/populated a word document from Access are saved the time and trouble of opening your thread. They would have nothing to contribute.

Secondly, and just as important, responsible people with questions first search for threads that address similar issues. Having a clear title facilitates these searches and saves everyone time and trouble.

Welcome to Bytes!

Linq ;0)>

Moderator
Nov 14 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Sivaraman.S | last post by:
Issue: When I am opening a word document or a template from an ASP.NET Web page (language used VB.NET Visual Studio Version 2003) using Word.ApplicationClass object (Reference Word 11.0 object...
1
by: stoker | last post by:
can anybody, please, tell me how to open prepared earlier ms word document or template from ms access vba code? all my efforts failed. ive managed to open empty document, but when i want to open...
6
by: Ryan Muller | last post by:
My company just upgraded from Access 97 to Access 2003 today and we are having some issues in a database that generates a Word document from information selected in a form. Here is the code we...
4
by: Yohancef Chin | last post by:
Hi, Being fairly new to .NET I am looking for a way to call MS Word from an event on a webform, and after the user is finished save that created document to an SQL Server database. Has anyone...
3
by: Yohancef Chin | last post by:
Hi, Being fairly new to .NET I am looking for a way to call MS Word from an event on a webform, and after the user is finished save that created document to an SQL Server database. Has anyone...
0
by: Sivaraman.S | last post by:
Issue : When I am trying to open a Word Document or a Template from a Web Application (ASP.NET Web page - Language Used VB.NET Visual Studio Version 2003) using Word.ApplicationClass (Refering...
2
by: BerkshireGuy | last post by:
I want to open a word document from an Access form and transfer data from the Access form to the Word Document. For instance, if a user clicks an option box on my access form, I want to pass...
5
by: Cedric | last post by:
Hi Folks, I'm wondering if someone has already succeed to open a word2000 document located on server side with javascript code executed by a web browser on client side. In fact, i make it...
1
by: Coll | last post by:
I have a form with a subform on it. On my main form, you select an employee id number and the top portion of form fills in with data, and the bottom portion of the form,which is a subform, lists...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.