Results 1 to 4 of 4
  1. #1
    missniffy is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Feb 2018
    Posts
    2

    Question Assigning values to variables

    Hi

    I have some code that was working quite well - that essentially fills in some data stored in Access into a Word document using Bookmarks. This was all working fine, now I want to change it to not just use one Letter/Document the path for which I had hardcoded for testing but instead the user will select the Letter from a drop down list in Access. My problems are around LetterPath and LetterPath2.

    When I run it at the moment I get and "Object required" error and the Set Letter path line is highlighted - if I remove the word Set this bit seems to be ok but fails when it is trying to open the Document.

    Thanks,

    MissNiffy

    Private Sub Letter_Click()
    Dim wApp As Word.Application
    Dim wDoc As Word.Document
    Dim rs As DAO.Recordset
    Dim query As QueryDef
    Dim JobID As Variant


    Dim LetterName As Variant
    Dim LetterPath As Variant
    Dim LetterPathNew As Variant


    Set JobID = [Form_JOBS SUB].[JOBS.ID]
    Set LetterName = [Form_JOBS SUB].Letter
    Set LetterPath = "C:\temp\Letters\Install.docx"



    Set query = CurrentDb.QueryDefs("Letters")
    query!JobID2 = JobID

    Set rs = query.OpenRecordset(dbOpenDynaset, dbSeeChanges)

    Set wApp = New Word.Application
    Set wDoc = wApp.Documents.Open(LetterPath)


    If Not rs.EOF Then rs.MoveFirst

    Do Until rs.EOF

    wDoc.Bookmarks("FirstName").Range.Text = Nz(rs!FirstName, "")
    wDoc.Bookmarks("Surname").Range.Text = Nz(rs!Surname, "")
    wDoc.Bookmarks("PropertyNo").Range.Text = Nz(rs!PropertyNo, "")
    wDoc.Bookmarks("PropertyName").Range.Text = Nz(rs!PropertyName, "")
    wDoc.Bookmarks("Road").Range.Text = Nz(rs!Road, "")
    wDoc.Bookmarks("Town").Range.Text = Nz(rs!TOWN, "")
    wDoc.Bookmarks("County").Range.Text = Nz(rs!COUNTY, "")
    wDoc.Bookmarks("Postcode").Range.Text = Nz(rs!Postcode, "")
    wDoc.Bookmarks("InstallationDate").Range.Text = Nz(rs!InstallationDate, "")
    wDoc.Bookmarks("InstallationTime").Range.Text = Nz(rs!InstallationTime, "")
    wDoc.Bookmarks("Duration").Range.Text = Nz(rs!Duration, "")
    wDoc.Bookmarks("Client").Range.Text = Nz(rs!Client, "")

    wDoc.Bookmarks("PropertyNo2").Range.Text = Nz(rs!PropertyNo, "")
    wDoc.Bookmarks("PropertyName2").Range.Text = Nz(rs!PropertyName, "")
    wDoc.Bookmarks("Road2").Range.Text = Nz(rs!Road, "")
    Set LetterPath2 = "C:\temp\letters\output" & rs!Id & "_" & LetterName & ".docx"
    wDoc.SaveAs2 LetterPath2

    rs.MoveNext
    Loop

    wApp.Application.Visible = True
    wApp.Documents.Open LetterPath2



    End Sub

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,524
    Use SET command to assign objects
    SET obj = listbox

    normal variables are just assigned without the set (in fact its a LET, but nobody uses let.) :

    LetterPath2 = "C:\temp\letters\output" & rs!Id & "_" & LetterName & ".docX"
    OR
    LET LetterPath2 = "C:\temp\letters\output" & rs!Id & "_" & LetterName & ".docX"

  3. #3
    missniffy is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Feb 2018
    Posts
    2
    Thanks very much

  4. #4
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,791
    FYI: query is a reserved name. If you adopted one of the more popular naming conventions, you'd pretty much never use a reserved word. Here's a site that has a decent article on that, but what I think it lacks is suggestions on data type prefixes, such as:
    var, int, lng, sng, dbl, str, bol, etc. You can probably guess what those are for. Also, imagine that 6 months or so from now you're trouble shooting 100 lines deep in your code and wondering what data type your variable "amt" is.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 12-11-2012, 12:04 PM
  2. Replies: 29
    Last Post: 06-19-2012, 12:12 PM
  3. Assigning values to Text Boxes
    By jgelpi16 in forum Programming
    Replies: 4
    Last Post: 12-11-2010, 01:26 AM
  4. Assigning values to duplicate records
    By matteu1 in forum Queries
    Replies: 3
    Last Post: 02-17-2010, 10:35 PM
  5. Assigning Values/Scorecard
    By nicholali in forum Access
    Replies: 0
    Last Post: 11-17-2008, 03:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums