Solved

vba creating word doc from template - error on second one opening.

Posted on 2013-05-19
3
1,321 Views
Last Modified: 2013-05-19
I have set up some vba in access to create a word doc from a template and then populate the document using bookmarks - all works fine except when I try and create a second one and then get errors:-

If I close the word document first created then run again I get - "The remote server machine does not exist or is unavailable"

If I leave the first word doc open I get "Object variable or with block variable not set"

Error on "Set objdoc = word.Documents.Add ... " in below code presumably because the file is in use.

Code used - abbreviated but I only add more bookmarks with variables.

    Dim objword As Object
    Dim objdoc As Object
   
    Set objword = CreateObject("Word.Application")
    Set objdoc = word.Documents.Add("C\:Test.dotx", , False)
       
    With objword.Selection
        .Goto what:=wdGoToBookmark, Name:="BookMark1"
        .TypeText ("Testinsert")
    End With
   
    objword.Visible = True
   
    Set objdoc = Nothing
    Set objword = Nothing


Appreciate any ideas.
0
Comment
Question by:donhannam
3 Comments
 
LVL 22

Accepted Solution

by:
Kelvin Sparks earned 500 total points
ID: 39179975
This is the code I use to do a similar process - used to run 40+ letters at a time. This is set to print the letters rather than save - but that's a one line change to this code.

Kelvin

rs.MoveFirst
Do Until rs.EOF

    Set wdDoc = CreateObject("Word.Application")
    wdDoc.Documents.Add sFile
    wdDoc.Visible = False
    With wdDoc.ActiveDocument.Bookmarks
        .Item("Client").Range.Text = rs!Client
        .Item("Addr1").Range.Text = rs!Address1
        .Item("Addr2").Range.Text = Nz(rs!Address2, "")
        .Item("Addr3").Range.Text = Nz(rs!Address3, "")
        .Item("Addr4").Range.Text = rs!Address4
    End With
   
    wdDoc.ActiveDocument.PrintOut
   
    wdDoc.ActiveDocument.Close False
    wdDoc.Quit False
    Set wdDoc = Nothing

   
   
    rs.Edit
        rs!WelcomeSentDate = Date
        rs!WelcomeLetterSent = True
    rs.Update

    rs.MoveNext
Loop
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 39180036
You will get that error when the file is open you should close the file after you have finished before reopening i.e.

Dim objword As Object
    Dim objdoc As Object
    
    Set objword = CreateObject("Word.Application")
    Set objdoc = word.Documents.Add("C\:Test.dotx", , False)
        
    With objword.Selection
        .Goto what:=wdGoToBookmark, Name:="BookMark1"
        .TypeText ("Testinsert")
    End With
    
    objword.Visible = True
    objDoc.close false
    Set objdoc = Nothing
    Set objword = Nothing

Open in new window


Chris
0
 

Author Closing Comment

by:donhannam
ID: 39180100
Thanks Kelvin,

This worked fine - not sure what the difference is but works.

I set visible to true and took off the code to printout, close and quit the document.

Opened first one as Doc1, second as Doc2 etc with no problems.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question