Results 1 to 15 of 15

Thread: Dealing with word from access ***RESOLVED***

  1. #1

    Thread Starter
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774

    Dealing with word from access ***RESOLVED***

    I have a module of code (Given below) that is supposed to find sections in a word document and then change them based on values it gets from the database and other word documents. In theory it works perfectly (to my mind) but in practice I get an error:

    Object variable or With block variable not set

    And this line is marked:

    VB Code:
    1. objWord.Selection.Text = Source
    now when ever I try to put SOMETHING_LIKELY.objWord... I get not possable, not supported, error - what do you think you are doing bo-zo... etc. All I need to do is select the section of text covered by the bookmark and replace with my own.

    I know that:

    VB Code:
    1. 'Where Start_BM is a variable for nameing bookmarks
    2.         Selected_Word_Text = sApp.Bookmarks(Start_BM).Range.Text
    Will actually pass the text out but passing it in seems to be problematical. Any help on getting this sequence finnished before the boss and the client murder me would be nice

    This is the code in context:
    VB Code:
    1. Option Compare Database
    2.  
    3. Global Const ClientTemplate = "CL1.DOT"
    4. Global ClientAccLet As String
    5.  
    6. Global WhoRu As Variant 'Client ID
    7. Global ThisJob As Variant 'Job ID
    8. Global ThisXS As Variant 'XS_Job_ID
    9. Global ThisList As Variant 'XS_List_ID
    10. Dim objWord As Word.Application
    11. Dim docWord As Word.Document
    12.  
    13.  
    14. Public Function Letter2Client()
    15. Dim AA As Variant
    16. Dim BB As Variant
    17. Dim CC As Variant
    18. Dim DD As Variant
    19. Dim temp As Variant
    20.  
    21.     'Establish details
    22.     Est_Det
    23.     'Create the letter
    24.     name_stuff
    25.    
    26.    
    27.     'FRANCHISE PHONE NUMBER
    28.     temp = DLookup("[Franchise ID]", "[Job]", "[Job ID] = " & ThisJob)  
    29.     AA = DLookup("phone", "[Franchise]", "[franchise ID] = " & temp)
    30.     temp = Word_Inst(AA, "F_tel_no", ClientAccLet)                      
    31.    
    32.     'FRANCHISE EMAIL
    33.     temp = DLookup("[Franchise ID]", "[Job]", "[Job ID] = " & ThisJob)  
    34.     AA = DLookup("eMail", "[Franchise]", "[franchise ID] = " & temp)
    35.     temp = Word_Inst(AA, "F_e_mail", ClientAccLet)                      
    36.    
    37.     Set objWord = CreateObject("Word.Application")
    38.     Set docWord = objWord.Documents.add(ClientAccLet)
    39.     'Make the application visible.
    40.     objWord.Visible = True
    41.    
    42. End Function
    43.  
    44.  
    45. Private Sub Est_Det()
    46. 'Get's variables based on the globals to ensure all are filled
    47. End Sub
    48.  
    49.  
    50. Private Function Word_Inst(Source, Target_BM, Target_Doc As Variant)
    51. Dim tApp As Object
    52. Dim Selected_Word_Text As Variant
    53.  
    54.     Set tApp = GetObject(WhereAmI & "\" & Target_Doc, "Word.Document")
    55.     'tApp.Visible = True
    56.     If tApp.Bookmarks.Exists(Target_BM) = True Then
    57.         tApp.Bookmarks(Target_BM).Select
    58.         objWord.Selection.Text = Source
    59.     Else
    60.         MsgBox "DEBUGGING: """ & Target_BM & """ does not exist error in Word_Inst."
    61.     End If
    62.    
    63. End Function
    64.  
    65. Private Sub name_stuff()
    66. 'Assigns "names" into the variables etc
    67. End Sub



    Thanx
    Last edited by Matt_T_hat; Aug 5th, 2003 at 10:26 AM.
    ?
    'What's this bit for anyway?
    For Jono

  2. #2
    Hyperactive Member Granty's Avatar
    Join Date
    Mar 2001
    Location
    London
    Posts
    439
    Does Word_Inst get called before Letter2Client?

    Because in Word_Inst, you do not Set objWord to anything, just in Letter2Client. If Word_Inst is called before Letter2Client sets ObjWord to an actual Word.Application, it will throw the error you mentioned.

  3. #3

    Thread Starter
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774
    I understand that Word_Inst works all by itself. certainly the read info in version which is almost the same requires no extra objects word just needs to be open (or it gets opened [by access I guess])
    ?
    'What's this bit for anyway?
    For Jono

  4. #4

    Thread Starter
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774
    Originally posted by Granty
    Does Word_Inst get called before Letter2Client?
    Letter2Client calls Word_Inst

    ...

    [Edit]
    Furthermore it would seem that I need a way of re-useing any curently open instences of word.
    [/edit]
    Last edited by Matt_T_hat; Aug 5th, 2003 at 06:34 AM.
    ?
    'What's this bit for anyway?
    For Jono

  5. #5
    Hyperactive Member Granty's Avatar
    Join Date
    Mar 2001
    Location
    London
    Posts
    439
    Well, you have to make sure that the objword is SET before it is used. Can't really see from the code you posted above.

    Yes, you need to keep track of your word instances. Talking to the Office applications is not too hard, but you must make sure they are all closed/quit and set to nothing when you have finished with them or you can cause yourself all sorts of troubles.

    Oh, and as it looks like you have added the MSOffice refernces to your project, you do not have to use CreateObject.

    Set objWord = New Word.Application should do fine.

  6. #6

    Thread Starter
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774
    Originally posted by Granty
    Oh, and as it looks like you have added the MSOffice refernces to your project, you do not have to use CreateObject.

    Set objWord = New Word.Application should do fine.
    I see, I did not know this. I have been working from several "examples" and an old Access 97 book that's no use at all.

    I need to read a bookmark name from the DB find the correct place in a template and read the bookmark content out of a big document and paste it into the templated one. The result is a long discription of everything that has been quoted for.

    I'll re-jig the code and post back here shortly.
    ?
    'What's this bit for anyway?
    For Jono

  7. #7

    Thread Starter
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774
    It does no like

    VB Code:
    1. Set docWord = objWord.Documents.Add(ClientAccLet)

    it now gives error 5151

    when I mouse over the variable names to get values I see this

    ClientAccLet = “ClientLetter5_8_2003 1323hrs.Doc”
    Word.Application = <Object variable or With block variable not set>
    ObjWord.Visible = <The remote server machine does not exist or is unavailable>


    Last time I trimed down the VB code but this time I'm going to post it all. This means that this is going to be a long post (sorry).

    WhereAmI is a fuction that returns the current application path

    Job and XS_Job are different tables (I'm not messing around with funny set-ups)

    VB Code:
    1. Option Compare Database
    2.  
    3. Global Const ClientTemplate = "CL1.DOT"
    4. Global ClientAccLet As String
    5.  
    6. Global WhoRu As Variant 'Client ID
    7. Global ThisJob As Variant 'Job ID
    8. Global ThisXS As Variant 'XS_Job_ID
    9. Global ThisList As Variant 'XS_List_ID
    10. Dim objWord As Word.Application
    11. Dim docWord As Word.Document
    12.  
    13.  
    14.  
    15. Public Function Letter2Client()
    16. Dim AA As Variant
    17. Dim BB As Variant
    18. Dim CC As Variant
    19. Dim DD As Variant
    20. Dim temp As Variant
    21.  
    22.     'Establish details
    23.     Est_Det
    24.     'Create the letter
    25.     name_stuff
    26.    
    27.     Set objWord = New Word.Application
    28.     Set docWord = objWord.Documents.Add(ClientAccLet)
    29.    
    30.     'Make the application visible.
    31.     objWord.Visible = True
    32.    
    33.     'FRANCHISE PHONE NUMBER
    34.     temp = DLookup("[Franchise ID]", "[Job]", "[Job ID] = " & ThisJob)  'ORDER IS VITAL   1
    35.     AA = DLookup("phone", "[Franchise]", "[franchise ID] = " & temp)
    36.     temp = Word_Inst(AA, "F_tel_no", ClientAccLet)                      'ORDER IS VITAL  2
    37.    
    38.     'FRANCHISE EMAIL
    39.     temp = DLookup("[Franchise ID]", "[Job]", "[Job ID] = " & ThisJob)  'ORDER IS VITAL   4
    40.     AA = DLookup("eMail", "[Franchise]", "[franchise ID] = " & temp)
    41.     temp = Word_Inst(AA, "F_e_mail", ClientAccLet)                      'ORDER IS VITAL  5
    42.    
    43.     'Others maybe done with the search replace system
    44.    
    45.     'Header
    46.     'List items for each feature in Job
    47.     'Terms and conditions
    48.     'List + Price of each feature
    49.     'Footer
    50.     'Search and replace {TAGS}
    51.     'Give Result succeed/fail
    52. End Function
    53.  
    54. Private Sub Est_Det()
    55.   'Double check the given values and fill in the blanks
    56.  
    57.   'Calculate the Job from the XS_Job
    58.   ThisJob = DLookup("[Job ID]", "[XS_JOB_link]", "[Job_Link_ID] = " & ThisXS)
    59.   'Get the attached Client ID
    60.   WhoRu = DLookup("[customer id]", "[Job]", "[Job ID] = " & ThisJob)
    61. End Sub
    62.  
    63.  
    64. Private Function Word_Inst(Source, Target_BM, Target_Doc As Variant)
    65. Dim tApp As Object
    66. Dim Selected_Word_Text As Variant
    67.  
    68.     Set tApp = GetObject(WhereAmI & "\" & Target_Doc, "Word.Document")
    69.     'tApp.Visible = True
    70.     If tApp.Bookmarks.Exists(Target_BM) = True Then
    71.         tApp.Bookmarks(Target_BM).Select
    72.         objWord.Selection.Text = Source
    73.     Else
    74.         MsgBox "DEBUGGING: """ & Target_BM & """ does not exist error in Word_Inst."
    75.     End If
    76.    
    77. End Function
    78.  
    79. Private Sub name_stuff()
    80. 'Name the new open doc (from template)
    81.     ClientAccLet = "ClientLetter" & Day(Now) & "_" & Month(Now) & "_" & Year(Now) & " " & Hour(Now) & Minute(Now) & "hrs" & ".Doc"
    82.     MsgBox ClientAccLet
    83.     FileCopy WhereAmI & "\" & ClientTemplate, WhereAmI & "\" & ClientAccLet
    84. End Sub
    ?
    'What's this bit for anyway?
    For Jono

  8. #8
    Hyperactive Member Granty's Avatar
    Join Date
    Mar 2001
    Location
    London
    Posts
    439
    From what I can see:

    Set docWord = objWord.Documents.Add(ClientAccLet)

    Is causing error 5151. I believe the argument after the .Add is supposed to be the location of a template file, thos that's really more of an educated guess.

    Are you wishing to use a template or are you creating a blank doc and trying to name it? I think you do that at the SaveAs stage.

    Sorry to be so unsure but I really only work in Access and Excel. Word brings me out in a rash

  9. #9

    Thread Starter
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774
    Originally posted by Granty
    Sorry to be so unsure but I really only work in Access and Excel. Word brings me out in a rash
    Me too. It just helps haveing a second opinion o this.


    I have a template

    VB Code:
    1. Global Const ClientTemplate = "CL1.DOT"
    which is copied into an actuale named example

    VB Code:
    1. Private Sub name_stuff()
    2. 'Name the new open doc (from template)
    3.     ClientAccLet = "ClientLetter" & Day(Now) & "_" & Month(Now) & "_" & Year(Now) & " " & Hour(Now) & Minute(Now) & "hrs" & ".Doc"
    4.     MsgBox ClientAccLet
    5.     FileCopy WhereAmI & "\" & ClientTemplate, WhereAmI & "\" & ClientAccLet
    6. End Sub
    ?
    'What's this bit for anyway?
    For Jono

  10. #10
    Hyperactive Member Granty's Avatar
    Join Date
    Mar 2001
    Location
    London
    Posts
    439
    Hmmm guess I didnt read your previous post too well

    Have you tried giving it the full explicit path of the new template rather than just the doc name?

  11. #11

    Thread Starter
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774
    Yeah WhereAmI figures that out. It's a fuction that gets the path of the current DB.

    DBug is a development time version of Msgbox but it only messages during develoment

    VB Code:
    1. Public Function WhereAmI() As String    'returns the App.path
    2. Dim strCurrAppDir As String
    3.     Set dbLocal = CurrentDb()
    4.     strCurrAppDir = Left$(dbLocal.Name, InStr(dbLocal.Name, MyNameIs))
    5.     DBug "WHEREAMI: " & strCurrAppDir & " - NTM - MNI: " & MyNameIs
    6.     WhereAmI = strCurrAppDir
    7. End Function

    the files and the DB all live in the same folder.
    ?
    'What's this bit for anyway?
    For Jono

  12. #12
    Hyperactive Member Granty's Avatar
    Join Date
    Mar 2001
    Location
    London
    Posts
    439
    From your above post, in debug:
    ClientAccLet = “ClientLetter5_8_2003 1323hrs.Doc”
    Have you tried

    Dim strPath as string

    strPath = WhereAmI

    Set docWord = objWord.Documents.Add(strPath & ClientAccLet) ?

  13. #13

    Thread Starter
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774

    Damn, I thought you'd cracked it then

    run time error 462
    The remote server machine does not exist or is unavailable

    the: Word.Application = <Object variable or With block variable not set>

    on the line above is worrying

    This line still stops everything
    VB Code:
    1. Set docWord = objWord.Documents.Add(WhereAmI & ClientAccLet)
    ?
    'What's this bit for anyway?
    For Jono

  14. #14
    Hyperactive Member Granty's Avatar
    Join Date
    Mar 2001
    Location
    London
    Posts
    439
    Word.Application will have that object variable message. It is objWord that you need to be checking (should say "Microsoft Word")

    Firstly, I think one problem may be that word templates have the extension .dot not .doc - the app MAY need that specific extension for that .Add parameter.

  15. #15

    Thread Starter
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774
    I added this line to my main sub

    VB Code:
    1. Set objWord = Nothing
    2. Set docWord = Nothing

    I put this after the DIMs in Letter2Client and BINGO. The text placement is wrong but it is getting put in (after a life time)

    So on wards and up wards as they say.

    Thankyou so much for your help.
    ?
    'What's this bit for anyway?
    For Jono

Posting Permissions

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



Click Here to Expand Forum to Full Width