Solved

Weird Behavior with VB and Word

Posted on 2002-03-12
3
327 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 150 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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses
Course of the Month9 days, 23 hours left to enroll

624 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