Improve company productivity with a Business Account.Sign Up

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

Weird Behavior with VB and Word

I'm opening up a word template(.dot) from VB, populating some bookmarks and then running a macro.  The macro actually populates the document itself.  The first time I run this it works fine.  Everything gets populated.  However, when I run it a second time I get and error: The remote server machine does not exist or is unavailable.

My code is simple:

Dim oWord as Word.Application
Set oWord = new Word.Application

With oWord
.Documents.Add (WORKING_DIR & "\templates\" & location)
.Visible = True

If .ActiveDocument.Bookmarks.Exists("bkFiscalYear") Then
.ActiveDocument.Bookmarks.Item("bkFiscalYear").Select
    Selection.TypeText (cmbFiscal.Item(ReportType).Text)
End If

.RunMacro
End With


Set oWord = nothing


it always seems to bomb on the selection.typetext part.

Does anyone have a clue as to what is going on?  It only happens the 2nd time I run it.

Thanks,
0
srobia
Asked:
srobia
  • 2
1 Solution
 
dmontgomCommented:


This is my code for creating templates for specific indivisuals:  period_1 is the name of the bookmark and PeriodTextfname is a string queired from a database.

 For i = 1 To TotalPhys
        Set objNewDoc = Documents.Add("C:\Documents and Settings\hm12648\Application Data\Microsoft\templates\pur.dot")
         


 
        Set objBookmark = objNewDoc.Bookmarks("period_1")
        Set objRange = objBookmark.Range
        objRange.Text = PeriodTextfname = "d:\my documents\pur\test\" & PhysName & ".doc"
           

objNewDoc.SaveAs FileName:=fname, FileFormat:=wdFormatDocument
            objNewDoc.Close wdDoNotSaveChanges
            Set objRange = Nothing
            Set objBookmark = Nothing
            Set objNewDoc = Nothing
next i

I hope this helps.
0
 
dmontgomCommented:


For i = 1 To TotalPhys
       Set objNewDoc = Documents.Add("C:\Documents and Settings\hm12648\Application Data\Microsoft\templates\pur.dot")
         


 
       Set objBookmark = objNewDoc.Bookmarks("period_1")
       Set objRange = objBookmark.Range
       objRange.Text = PeriodText

fname = "d:\my documents\pur\test\" & PhysName & ".doc"
           objNewDoc.SaveAs FileName:=fname, FileFormat:=wdFormatDocument
           objNewDoc.Close wdDoNotSaveChanges
           Set objRange = Nothing
           Set objBookmark = Nothing
           Set objNewDoc = Nothing
next i

0
 
srobiaAuthor Commented:
Works perfectly thanks!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

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