Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1623
  • Last Modified:

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

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
donhannam
Asked:
donhannam
1 Solution
 
Kelvin SparksCommented:
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
 
Chris BottomleyCommented:
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
 
donhannamAuthor Commented:
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now