0

With the help of some users on stack, I've compiled a macro that takes certain values from an excel workbook and copies them into a word template. The macro works fine sometimes, but others, I get an error - "Run time error -2146950355 (80080005): Server Execution Failed". I am not sure why I get this error sometimes but not others. Attached is my code and screenshots of the errors and debug.

Const FilesPath As String = "filespath"
Const FilesPathh As String = "filespathh"
Const FilesPathhh As String = "filespathhh"
Const TemplateFile As String = "tempa.docx"


Sub Letters()
    Dim wd As Word.Application, doc As Word.Document
    Dim NomCell As Range, ws As Worksheet
    Dim Result As Integer
    

    Set ws = ActiveSheet
    Set wd = New Word.Application
    wd.Visible = True
    

   Columns("H:H").Select
    Application.CutCopyMode = False
    Selection.Copy
    Columns("I:I").Select
    Selection.Insert Shift:=xlToRight
    Application.CutCopyMode = False
        
        
    Columns("I:I").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Columns("J:J").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Columns("H:H").Select
    Selection.TextToColumns Destination:=Range("H1"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=False, _
        Semicolon:=False, Comma:=True, Space:=True, Other:=False, FieldInfo:= _
        Array(Array(1, 1), Array(2, 1), Array(3, 1)), TrailingMinusNumbers:=True
    Range("H1").Select
    ActiveCell.FormulaR1C1 = "Last Name"
    Range("I1").Select
    ActiveCell.FormulaR1C1 = "First Name"
    Range("J1").Select
    ActiveCell.FormulaR1C1 = "Other"
    
    


For Each NomCell In ws.Range("A2", ws.Cells(Rows.Count, 1).End(xlUp)).Cells
        'open as read-only
        
        
        Set doc = wd.Documents.Open(FilesPath & TemplateFile, ReadOnly:=True)
        With NomCell.EntireRow
        
        doc.Bookmarks("date").Range.Text = Date
        doc.Bookmarks("name").Range.Text = .Columns("I").Value
        doc.Bookmarks("course").Range.Text = .Columns("A").Value
    .Columns("A").Select
    Selection.Replace What:="&", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
    Selection.Replace What:=":", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
    Selection.Replace What:="/", Replacement:=" ", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
        
               doc.SaveAs2 FilesPathh & .Columns("K").Value & " " & .Columns("A").Value & ".pdf", _
                                                   wdExportFormatPDF
               doc.Close False
            
        End With
    Next NomCell
    wd.Quit
    ActiveSheet.Cells.ClearContents
    Result = MsgBox("The letters have been created. Would you like to view them?", vbYesNo)
    If Result = vbYes Then
    Call Shell("explorer.exe " & FilesPathhh, vbNormalFocus)
    End If
End Sub

enter image description here

enter image description here

Any help would be greatly appreciated. Thanks!

chris neilsen
  • 52,446
  • 10
  • 84
  • 123
z_11122
  • 153
  • 8
  • Use another approach to load Word: [link](https://stackoverflow.com/questions/47158574/how-to-open-word-application-using-vba) and DIM some variables not defined: Templatefile, (why these three?: FilesPath, FilesPathh, FilesPathhh ) – ALeXceL Aug 26 '22 at 19:24
  • And load these mentioned variables with some text, as they're passed as a null strings on the sample code – ALeXceL Aug 26 '22 at 19:31
  • Where and when do you automate Excel and Word? – Eugene Astafiev Aug 27 '22 at 11:08
  • Does this answer your question? [80080005 Server execution failed when using Word Automation](https://stackoverflow.com/questions/58736431/80080005-server-execution-failed-when-using-word-automation) – Eugene Astafiev Aug 27 '22 at 11:08

2 Answers2

0

Run time error -2146950355 (80080005): Server Execution Failed

You may find the exactly the same error described in the Error when you start many COM+ applications: Error code 80080005 -- server execution failed article.

The Considerations for server-side Automation of Office article states the following:

All current versions of Microsoft Office were designed, tested, and configured to run as end-user products on a client workstation. They assume an interactive desktop and user profile. They do not provide the level of reentrancy or security that is necessary to meet the needs of server-side components that are designed to run unattended.

Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution.

Consider using the Open XML SDK if you deal with open XML documents only. Otherwise, you may consider using any third-party components designed for the server-side execution, for example, Aspose.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
0

I had the same issue, which always occurred when I tried to create a word application object, while Word was already running. I solved the issue by trying to reference to the already running instance of Word. Only if this instance was not available, I would create a new one.

Be aware that I used late binding in my code:

Dim wrdApp As Object

'Temporarily turn off error handling
On Error Resume Next

'Try to get a reference to an already running instance of Word
Set wrdApp = GetObject(, "Word.Application")

'If referencing to the running instance failed
If wrdApp Is Nothing Then
    'Create a new instance of Word
    Set wrdApp = CreateObject("Word.Application")
End If

'Turn error handling back on
On Error GoTo 0
Michael Wycisk
  • 1,590
  • 10
  • 24