wrdMailMerge.Destination = Word.WdMailMergeDestination.wdSendToPrinter

This line of code gives an error saying requested object is not available
Does anyone know why? If so can you point me in the right direction to fix it.

Dani AI

Generated

Short diagnosis and a pragmatic fix list for the "Requested object is not available" (run-time 5852) error when automating Word mail-merge (observed after an update in this thread). The root cause is almost always that the MailMerge object is not ready — the document is not a mail-merge main document with an attached data source, so setting the merge destination fails. Check the MailMerge.State before changing the destination and only act when the state indicates a main document with data. See the MailMerge.State and Destination docs for the supported workflow. (learn.microsoft.com)

Use the Document object you opened and attach the data explicitly rather than relying on ActiveDocument or the UI prompt. When Word is opened programmatically it can show a SQL prompt for the mail-merge data source; if that prompt is suppressed or answered “No” the document will open without a data source and you will get 5852. Open the template into a Document variable, check/or call OpenDataSource if needed, then set the destination and Execute. For the VBA/automation prompt behavior see Microsoft’s article about the SQL prompt and community reports about this error. (support.microsoft.com)

Practical VB.NET pattern (outline — use your own names and parameters):

Dim app As New Word.Application()
Dim doc As Word.Document = app.Documents.Open(templatePath)
Dim mm As Word.MailMerge = doc.MailMerge

If mm.State <> Word.WdMailMergeState.wdMainAndDataSource Then
  mm.OpenDataSource(dataPath, ...)
End If

' now set destination and call Execute

If the problem appeared after an Office/Service-Pack upgrade (as noted), update the project reference to the Word object library or switch to late binding to avoid version mismatches. In Visual Studio use Project → Add Reference → COM → "Microsoft Word X.0 Object Library" (or use CreateObject for late binding). ’s suggestion to check references is on target. Microsoft’s automation guidance covers these steps. (learn.microsoft.com)

Quick checklist: confirm the document is a mail-merge main document, confirm the data source is attached (or call OpenDataSource), prefer working with the Document variable you opened, update the Word COM reference or use late binding, and test the sequence inside Word (recorded macro) to reproduce the successful steps before automating. This sequence resolves the majority of 5852 cases reported in Word 2003-era updates; follow the links above for the authoritative APIs and the SQL‑prompt behavior. (learn.microsoft.com)

Have you set your references to include Word? If not then the code will not understand what you are doing. Have you used Word in other parts of your code and it work? It might help put the code up so we can see it. You may have a problem elsewhere in your code and it does not become evident until the referenced statement.

Chester

Yes I have a reference to word. I found out that this line works under service pack 2 but not under service pack 3 which is what I am currently using. Any reason why it would work under sp2 but not sp3. Do I have to change my references after the update is done.

I am not sure, but I think SP3 may change the core from 10 to 11. This may be the problem. Check to see what Office and Word Objects version you are referencing.

Chester

Currently it is referencing 10 but I'll check to see if that is the problem. Thanks.

I found your question posted a few months ago.

wrdMailMerge.Destination = Word.WdMailMergeDestination.wdSendToPrinter

This line of code gives an error saying requested object is not available

I have the same problem as yours. It works totally fine in Service pack 2, but it doesn't work for service packe 3. How did you solve the problem?

In your post, you mention you change your reference. How can i find the reference. I have no idea about the reference. Can you share your solution? Many thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.