Solved

Weird Behavior with VB and Word

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now