Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Weird Behavior with VB and Word

Posted on 2002-03-12
3
Medium Priority
?
333 Views
Last Modified: 2010-05-18
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
Comment
Question by:srobia
  • 2
3 Comments
 

Expert Comment

by:dmontgom
ID: 6858822


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
 

Accepted Solution

by:
dmontgom earned 600 total points
ID: 6858826


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
 

Author Comment

by:srobia
ID: 6858875
Works perfectly thanks!!!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

824 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