Solved

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

Posted on 2013-05-19
3
1,249 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now