Automation Error Using Word

BigJimSlade

Registered User.
Local time
Today, 14:03
Joined
Oct 11, 2000
Messages
173
Hey, Big Jim here:

This one has been bugging me for a while, so here I am turning it over to our lovely forum. Here is the problem:

This code is set to copy and paste from file A to file B....twice! The first time, it works great. The second time, I get an automation error:

2147023174(800706ba)
The RPC server is unavailable

I am using Windows NT and Access 97, Word 97

Here is my code:

Private Sub Form_Load()
Dim i As Variant
Dim WordObj As Word.Application
Dim WordDoc As Word.Document
Dim WordDoc1 As Word.Document

For i = 1 To 2

Set WordObj = New Word.Application
Set WordDoc = .Documents.Open("C:\Test.Doc")
WordDoc.Range.Copy
Set WordDoc1 = .Documents.Open("C:\TestA.doc")
WordDoc1.Activate
Selection.Paste
WordDoc1.Close (wdSaveChanges)
WordDoc.Close (wdSaveChanges)
DoEvents
PauseIta (5) ' This is just a function I use to create a pause
WordObj.Quit
End With
Set WordDoc = Nothing
Set WordDoc1 = Nothing
Set WordObj = Nothing
Next i
End Sub

The error occurs the second time I run the line "Selection.Paste". Thanks everyone for taking a look at it. I always appreciate your time.

Big Jim
 
Code:
Public Sub FormLoad()
    Dim i As Variant
    Dim WordObj As Word.Application
    Dim WordDoc As Word.Document
    Dim WordDoc1 As Word.Document
    
        For i = 1 To 2
            Set WordObj = New Word.Application
            With WordObj
                Set WordDoc = .Documents.Open("D:\Test.Doc")
                WordDoc.Range.Copy
                Set WordDoc1 = .Documents.Open("D:\TestA.doc")
                WordDoc1.Activate
                'Need a Period before Selection
                .Selection.Paste
                WordDoc1.Close (wdSaveChanges)
                WordDoc.Close (wdSaveChanges)
                DoEvents
                PauseIta (5) ' This is just a function I use to create a pause
                WordObj.Quit
            End With
            Set WordDoc = Nothing
            Set WordDoc1 = Nothing
            Set WordObj = Nothing
        Next i
End Sub
 
Hey Travis!

Where were you like a month ago when I was crying under my desk over that one? I guess I was accessing the "selection" property of Access as opposed to Word.

You are truly worthy of that picture of Hobbes which hangs beside your name!

Big ol Humble Jim
 
More "RPC server not available" problems

Hi Jim and Travis! Thanks for opening this subject, I've been having some problems with it too. Travis, the code really helped. Now I can get my function to run once, but the second time I run it, I get the infamous message "RPC server not available". And I have to close down access and restart to get it to work again. After closing down access a couple dozen times, I began to really hope there might be some other way...

Here's my code:

Function CreateDokumentTransmittal2()

Dim i As Variant
Dim WordObj As Word.Application
Dim WordDoc As Word.Document

Set WordObj = New Word.Application
With WordObj
Set WordDoc = .Documents.Open("M:\DB-Projekten\Zeichnungs-DB-Marine\Dok-Transmittal-Bookmark.doc")
End With
WordObj.Visible = True
With WordObj
' Open the document.
.ActiveDocument.bookmarks("DokListe").Select
.Selection.InsertFile FileName:="c:\temp\DocTransList.xls", Range:="", _
ConfirmConversions:=False, Link:=False, Attachment:=False
'select the table, convert it to text, convert it back to a table:
'this gets shading and other hidden formatting out.
.Selection.MoveUp Unit:=wdLine, Count:=2
.Selection.Tables(1).Select
.Selection.Rows.ConvertToText Separator:=wdSeparateByTabs
.Selection.ConvertToTable Separator:=wdSeparateByTabs, NumColumns:=7, _
NumRows:=3, Format:=wdTableFormatNone, ApplyBorders:=True, ApplyShading:= _
True, ApplyFont:=True, ApplyColor:=True, ApplyHeadingRows:=True, _
ApplyLastRow:=False, ApplyFirstColumn:=True, ApplyLastColumn:=False, _
AutoFit:=False
'position the cursor for setting column widths:
.Selection.HomeKey Unit:=wdLine
'select column and adjust column widths for each column:
.Selection.SelectColumn
'code tends to bomb here:
.Selection.Cells.SetWidth ColumnWidth:=CentimetersToPoints(0.87), _
RulerStyle:=wdAdjustNone
.Selection.Rows.SpaceBetweenColumns = CentimetersToPoints(0.25)
.Selection.MoveRight Unit:=wdCharacter, Count:=1
.Selection.SelectColumn
.Selection.Cells.SetWidth ColumnWidth:=CentimetersToPoints(0.751), _
RulerStyle:=wdAdjustNone
.Selection.Rows.SpaceBetweenColumns = CentimetersToPoints(0.25)
.Selection.MoveRight Unit:=wdCharacter, Count:=1
.Selection.SelectColumn
.Selection.Cells.SetWidth ColumnWidth:=CentimetersToPoints(0.751), _
RulerStyle:=wdAdjustNone
.Selection.Rows.SpaceBetweenColumns = CentimetersToPoints(0.25)
.Selection.MoveRight Unit:=wdCharacter, Count:=1
.Selection.SelectColumn
.Selection.Cells.SetWidth ColumnWidth:=CentimetersToPoints(3.51), RulerStyle _
:=wdAdjustNone
.Selection.Rows.SpaceBetweenColumns = CentimetersToPoints(0.25)
.Selection.MoveRight Unit:=wdCharacter, Count:=1
.Selection.SelectColumn
.Selection.Cells.SetWidth ColumnWidth:=CentimetersToPoints(1.49), RulerStyle _
:=wdAdjustNone
.Selection.Rows.SpaceBetweenColumns = CentimetersToPoints(0.25)
.Selection.MoveRight Unit:=wdCharacter, Count:=1
.Selection.SelectColumn
.Selection.Cells.SetWidth ColumnWidth:=CentimetersToPoints(6.96), _
RulerStyle:=wdAdjustNone
.Selection.Rows.SpaceBetweenColumns = CentimetersToPoints(0.25)
.Selection.MoveRight Unit:=wdCharacter, Count:=1
.Selection.SelectColumn
.Selection.Cells.SetWidth ColumnWidth:=CentimetersToPoints(2.294), _
RulerStyle:=wdAdjustNone
.Selection.Rows.SpaceBetweenColumns = CentimetersToPoints(0.25)
'delete the top line of imported table and bottom line of existing table.
.Selection.MoveUp Unit:=wdLine, Count:=1
.Selection.Delete Unit:=wdCharacter, Count:=1
.Selection.MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend
.Selection.Cells.Delete ShiftCells:=wdDeleteCellsEntireRow
'WordDoc.Close (wdSaveChanges)
DoEvents
'WordObj.Quit
End With
Set WordDoc = Nothing
Set WordObj = Nothing
End Function

***

I would really appreciate it if somebody could see what I'm doing wrong here... Thanks!
 
RPC not available

I was (am) struggling with the error message 'RPC does not exist or is unavailable' when running my code more than once. I have managed to find a work around which although it's not ideal, it works for me.
rather than use the following,

WordApp = New Word.application
or
Set WordApp = CreateObject("Word.Application")
both of which I tried

I open Word manually and select File->Close so there is no active document open.

I replaced the above code with
Set WordApp = GetObject(, "Word.Application")

at the end of the function
I save and close the document created but do not close Word with,
WordApp.quit
Set WordApp = Nothing

Instead I manually close Word when I've finished in Access.

I have managed to create numerous docs this way without erroring.
It's not elegant but it works for me, hope it helps others.
If you know a elegant answer to this let me know.

Pete
 
Last edited:

Users who are viewing this thread

Back
Top Bottom