Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Weird Behavior with VB and Word

Posted on 2002-03-12
3
Medium Priority
?
331 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

722 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