Solved

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

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
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…
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.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

706 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