Solved

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

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Office 365 home questions 7 65
Launch app from Access custom ribbon 8 33
Binding recordsets to a form 6 22
Dirty form - conditional formatting 5 17
Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

775 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