+ Reply to Thread
Results 1 to 13 of 13

Thread: Input different ranges from excel to a word template (letter)

  1. #1
    Registered User
    Join Date
    12-29-2011
    Location
    Perth
    MS-Off Ver
    Excel 2010
    Posts
    24

    Input different ranges from excel to a word template (letter)

    Hi pplz

    I work in a pharmacy and I am trying to organise a button in excel that will build a prescription request for a doctor.

    The word doc is attached and has ~17 bookmarks (eg. Doctor name, doc address, S1=script 1 requested, S2 = Script 2 requested, with a possible request up to 10 scripts)

    I have found some info regarding a vba code for it but I don't know how it works.. WHat I want is for the ranges in excel to replace the bookmarked words in the word doc.

    Excel will be
    A1 = "doc name" B1 = "John Smith"
    A2 = "doc address" B2 = 13 doctor st, doctorville

    etc etc
    etc..

    Anyone know a basic code that i can implement in a button to do this?
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    12-29-2011
    Location
    Perth
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Input different ranges from excel to a word template (letter)

    bump
    any ideas?

  3. #3
    Valued Forum Contributor smuzoen's Avatar
    Join Date
    10-28-2011
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003/2007/2010
    Posts
    359

    Re: Input different ranges from excel to a word template (letter)

    This is an example of code you could place into an excel spreadsheet with the data in Column B - if you are clever you can name your bookmarks in such a way that you could iterate through the bookmarks without having to name each bookmark. That being said this will place the Doctors Name and 1st Line of the address into the word template. You could also pass formatting to the routine that inserts the text however see how you go with this as a simple guide
    Sub CreateWordDoc()
        Dim wdApp As Object, a
        Dim wdDoc As Object
        Dim rCell As Range, rRng As Range
           
    Set rRng = Sheet2.Range("B1:B2") 'Column B contains the data in the same order as the bookmarks
    a = rRng 'Place values into array - you could use a loop to go through the array and
    'if the bookmarks are named correctly you place all this into a loop (e.g. bmk1, bmk2, bmk3 etc)
    Set wdApp = CreateObject("Word.Application")
    Set wdDoc = wdApp.Documents.Open("C:\temp\ScriptRequest.doc") ' location of word document
    rplBmk wdDoc, a(1, 1), "DocFull"
    rplBmk wdDoc, a(2, 1), "Add1"
    wdApp.Visible = True
    End Sub
    
    Sub rplBmk(ByRef wdDoc As Object, _
        ByVal vValue As Variant, _
        ByVal sBmName As String)
        Dim wdRng As Object
       Set wdRng = wdDoc.Bookmarks(sBmName).Range
      wdRng.Text = vValue
    End Sub
    You could iterate through the array and then use clever bookmark names bmk1 bmk2 etc that way in the loop you pass sBmName as a string
    e.g.
    For k = 1 to 20 'number of bookmarks
    sBmName = "bmk" & Cstr(k)
    rplBmk wdDoc, a(k, 1), sBmName
    Next k
    That would save you typing the name of each bookmark individually - that means you would have to redo the Word Document and change the bookmark names. Make sure the order of the bookmarks corresponds to the data in Column B in excel.
    Hope this makes sense.

    Hope this helps.
    Anthony
    “Confidence never comes from having all the answers; it comes from being open to all the questions.”
    PS: Remember to mark your questions as Solved once you are satisfied and rate the answer(s) questions.”
    Last edited by smuzoen; 02-02-2012 at 02:07 AM.

  4. #4
    Valued Forum Contributor
    Join Date
    12-05-2011
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2007
    Posts
    213

    Re: Input different ranges from excel to a word template (letter)

    Hi Gangel

    How VBA conversant are you?

    Here is some old code I had from about 10 years ago. I think there might be better ways to do it these days. Anyway, I've run it and it worked. It just needs some 'finessing' now.

    Sub TestDDE()
    tempfile$ = "c:\Script Request_bookmarks.doc"
    
    chan = DDEInitiate("winword", "system")
        exe$ = "[fileopen(""" + tempfile$ + """)]"
        DDEExecute chan, exe$
        DDETerminate chan
    
    chan = DDEInitiate("winword", "system")
    DDEExecute chan, "[appmaximize 1]"
    DDETerminate chan
    
    chan = DDEInitiate("winword", tempfile$)
    DDEExecute chan, "[docmaximize 1]"
    DDETerminate chan
    
    chan = DDEInitiate("winword", tempfile$)
    'You need to insert loops to cycle through the range of bookmarks and values.
    Application.DDEPoke chan, "docfull", Range("B1")
    
    DDETerminate chan
    
    chan = DDEInitiate("winword", "system")
    DDEExecute chan, "[filesave]"
    DDETerminate chan
    
    Application.ActivateMicrosoftApp xlMicrosoftWord
    
    End Sub
    I successfully used this to send a doctor's name to your template.

    Hope this is of use.

    Cheers, Rob.

  5. #5
    Valued Forum Contributor
    Join Date
    12-05-2011
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2007
    Posts
    213

    Re: Input different ranges from excel to a word template (letter)

    I knew someone would have better code than me!

  6. #6
    Registered User
    Join Date
    12-29-2011
    Location
    Perth
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Input different ranges from excel to a word template (letter)

    haha thanks guys ill ahve a go!

  7. #7
    Registered User
    Join Date
    12-29-2011
    Location
    Perth
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Input different ranges from excel to a word template (letter)

    Hi i tried it and im getting a runtime error 5174...
    Ive tried using bmk1 and bmk2 as well as Docfull and Add1....
    info is in correct sheet and in B1 and B2 as testers..

  8. #8
    Valued Forum Contributor smuzoen's Avatar
    Join Date
    10-28-2011
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003/2007/2010
    Posts
    359

    Re: Input different ranges from excel to a word template (letter)

    Can you post the letter you are using and an example workbook for me as well and I will take a look for you. The code is in the Excel workbook...yes?? and not in the Word document.? The path to the letter is correct and you have not missed any spaces etc - this is a file not found error generally
    Hope this helps.
    Anthony
    Pack my box with five dozen liquor jugs
    PS: Remember to mark your questions as Solved once you are satisfied. Please rate the answer(s) by selecting the Star in the lower left next to the Triangle. It is appreciated

  9. #9
    Registered User
    Join Date
    12-29-2011
    Location
    Perth
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Input different ranges from excel to a word template (letter)

    Oh ok, ill try again. im at work so ill see if it works here

  10. #10
    Registered User
    Join Date
    12-29-2011
    Location
    Perth
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Input different ranges from excel to a word template (letter)

    Attached are both files, the request Xls and the word doc.

    Ive changed the word doc location in the VBA code, and ive left the bookmarks as Docfull and Add1 (as in the VBA code)
    Attached Files Attached Files

  11. #11
    Registered User
    Join Date
    12-29-2011
    Location
    Perth
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Input different ranges from excel to a word template (letter)

    lol Duh i forgot to put the macro in the one i put on the net:

    Sub CreateWordDoc()
        Dim wdApp As Object, a
        Dim wdDoc As Object
        Dim rCell As Range, rRng As Range
           
    Set rRng = Worksheets("Request").Range("B1:B2") 'Column B contains the data in the same order as the bookmarks
    a = rRng 'Place values into array - you could use a loop to go through the array and
    'if the bookmarks are named correctly you place all this into a loop (e.g. bmk1, bmk2, bmk3 etc)
    Set wdApp = CreateObject("Word.Application")
    Set wdDoc = wdApp.Documents.Open("C:\Users\Fred\Documents\Pharmacy Business Documents\Webster Packs\Script Request letter\ScriptRequest.doc") ' location of word document
    rplBmk wdDoc, a(1, 1), "DocFull"
    rplBmk wdDoc, a(2, 1), "Add1"
    wdApp.Visible = True
    End Sub
    
    Sub rplBmk(ByRef wdDoc As Object, _
        ByVal vValue As Variant, _
        ByVal sBmName As String)
        Dim wdRng As Object
       Set wdRng = wdDoc.Bookmarks(sBmName).Range
      wdRng.Text = vValue
    End Sub

  12. #12
    Valued Forum Contributor smuzoen's Avatar
    Join Date
    10-28-2011
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003/2007/2010
    Posts
    359

    Re: Input different ranges from excel to a word template (letter)

    The error that you reported was a File Not Found Error - in your code you have the file name as ScriptRequest.doc however the file you uploaded is Script Request.doc (notice the space!) - Error 5174 is File not found. I have placed an error handler in the code. When an error like this occurs the Word application will be in your task manager - if the process is not quit then you will have a Word process running in the backgroud. The error handler will check to see if there is a Word process running and if so it will stop it.
    Sub CreateWordDoc()
    On Error GoTo errHandler
        Dim wdApp As Object, a
        Dim wdDoc As Object
        Dim rCell As Range, rRng As Range
        
    Set rRng = Worksheets("Request").Range("B1:B2") 'Column B contains the data in the same order as the bookmarks
    a = rRng 'Place values into array - you could use a loop to go through the array and
    'if the bookmarks are named correctly you place all this into a loop (e.g. bmk1, bmk2, bmk3 etc)
    Set wdApp = CreateObject("Word.Application")
    Set wdDoc = wdApp.Documents.Open("C:\Users\Fred\Documents\Pharmacy Business Documents\Webster Packs\Script Request letter\Script Request.doc") ' NOTICE THE SPACE IN WORD DOCUMENT
    rplBmk wdDoc, a(1, 1), "DocFull"
    rplBmk wdDoc, a(2, 1), "Add1"
    wdApp.Visible = True
    Exit Sub
    errHandler:
    MsgBox "Error in generating letter - error number - " & Err.Number & _
    " - " & Err.Description
    If Not wdApp Is Nothing And Err.Number = 5174 Then
    wdApp.Application.Quit
    Set wdApp = Nothing
    End If
    End Sub
    
    Sub rplBmk(ByRef wdDoc As Object, _
        ByVal vValue As Variant, _
        ByVal sBmName As String)
        Dim wdRng As Object
       Set wdRng = wdDoc.Bookmarks(sBmName).Range
      wdRng.Text = vValue
    End Sub
    Last edited by smuzoen; 02-08-2012 at 01:26 AM.
    Hope this helps.
    Anthony
    Pack my box with five dozen liquor jugs
    PS: Remember to mark your questions as Solved once you are satisfied. Please rate the answer(s) by selecting the Star in the lower left next to the Triangle. It is appreciated

  13. #13
    Registered User
    Join Date
    12-29-2011
    Location
    Perth
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Input different ranges from excel to a word template (letter)

    THAT IS GREAT :P

    Fantastic, now i just need to get t wokring with my patients and im in business!!!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0