Results 1 to 4 of 4
  1. #1
    Josha is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2019
    Location
    Victoria, Australia
    Posts
    42

    Access, how to save a Word document (with data from an Access form) with a specific filename?

    So I have a database where students record their examination notes.



    At the end of their examination I expect them to add important photos to a Word document template called "Continuity Imaging Record" that I have pre-made. The Word document successfully pulls information from the Access database such as Examiner details, Examination date, Case number ect ect using bookmarks. That's not the issue.

    The issue is;

    When they are nearing the end of the examination, I want them to be able to press a button on the form in Access which will copy the "Continuity Imaging Record" template, pull certain information (mentioned above) BUT THEN save the filename of this new Word document with information from a control located in the access database form. The control is called "FilenameCont" and it usually looks like this..... 12345-2022 JRS Cont Imag Record....

    [VBA is not my strength]

    I have the following code....

    -----------------

    Private Sub cmdPrint_Click()


    Dim appWord As Word.Application
    Dim DOC As Word.Document
    'Avoid error 429, when Word isn’t open.
    On Error Resume Next
    Err.Clear
    'Set appWord object variable to running instance of Word.
    Set appWord = GetObject(, "Word.Application")
    If Err.Number <> 0 Then
    'If Word isn’t open, create a new instance of Word.
    Set appWord = New Word.Application
    End If
    Set DOC = appWord.Documents.Open("H:\Abilas 1.16.8\Continuity Imaging Record.docx", , True)
    With DOC
    .FormFields("flddateexam").Result = Me![Dateofexamination]
    .FormFields("fldexaminer").Result = Me![examiner]
    .FormFields("fldcasenum").Result = Me![Casenumber]
    .Visible = True
    .Activate
    .SaveAs2 "Test.docx"



    End
    Set DOC = Nothing
    Set appWord = Nothing
    Exit Sub

    errHandler:
    MsgBox Err.Number & ": " & Err.Description




    End With
    End Sub


    ----------------------------

    This code works to my amazement but saves the new Word document filename with Test.docx

    I tried replacing this line with the following

    .SaveAs2 "Me![FilenameCont].docx"

    But then it saves the word document filename with Me![FilenameCont].docx... When I want it to grab the data from [FilenameCont] control and make it the filename. So the filename will be "1234-2022 JRS Cont Imag Record"

    Can anyone help??

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,928
    Don't use quote marks around variable reference and concatenate literal string parts.

    .SaveAs2 Me![FilenameCont] & ".docx"

    For future reference: please post code between CODE tags to retain indentation and readability.

    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Josha is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2019
    Location
    Victoria, Australia
    Posts
    42
    Wow!

    It worked - thank you for your quick reply ! I'm so happy!

    Apologies, I will use the CODE tags next time.

    Thanks again!

  4. #4
    ssanfu is offline Master of Nothing
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I noticed that the error handler lines are within the WITH.... END WITH code.


    Click image for larger version. 

Name:	LogicError1.png 
Views:	8 
Size:	151.5 KB 
ID:	47172


    Good luck with your project.....

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

Similar Threads

  1. Replies: 1
    Last Post: 09-04-2019, 03:19 PM
  2. Replies: 2
    Last Post: 07-27-2016, 01:29 PM
  3. Replies: 5
    Last Post: 05-18-2015, 08:08 AM
  4. Replies: 8
    Last Post: 07-29-2014, 06:41 PM
  5. Word document INTO Access form
    By jonathonhicks in forum Forms
    Replies: 0
    Last Post: 04-30-2007, 05:59 PM

Tags for this Thread

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