Exporting Word to pdf from Excel macros

edarkmat

New Member
Joined
Sep 25, 2018
Messages
3
Hello everyone,

i know this question have already been posted and there's a lot of info about it but im facing one problem that i hadnt solved yet and i have no idea how to solve it.

The purpose of this macro is simple: from an Excel macro, create a word object, open a file (not visible) an then export that document as pdf, and preview it.

The code im using:

Code:
Sub pdf()
' Create a Word object
    Set objWord = CreateObject("Word.Application")
' Create a word document object
    Set objDoc = objWord.Documents.Open("C:\Users\Win\Desktop\abcd.docx")
' Make it not visible
    objWord.Visible = False
' Activate the document object (just in case)
    objDoc.Activate
' Export the document as pdf
    objDoc.ExportAsFixedFormat OutputFilename:="C:\Users\Win\Desktop\abcd.pdf", ExportFormat:=wdExportFormatPDF, _
    OpenAfterExport:=True
End Sub

I have tried with the option Visible = True, and without Active even, but there is no way to get it working.
Excel stucks in the vba editor with the loading icon from windows but i get no results.
Sometimes Word open, but also stucks without finishing loading.
Sometimes Excel tell: "Excel its waiting for another aplication to complete an OLE". i've tried disabling DDE (dinamic data exchange) but same problem persists.

Have to mention that i have only achived this with a word macro that do the same but directly with
ActiveDocument.ExportAsFixedFormat ..., but when i try to force it to print my own created object fails same way as the excel macro.

I'm starting thinking its some permission problem with my windows setup rather than the code, but need your help to debug it.

Another aspect to mention: if i try to execute a Word macro from excel, it also fails.


I have some alternatives like making a copy of the template i want to print an then let the user print o save it as pdf, but i get the same errors when working with word objects through excel macros.

Can anyone give shed some light on this? im really frustrated with something that should be easy.

thanks in advance
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
What happens if you set Visible to True and step through the code? Does the document print, where does it hang?
 
Upvote 0
If i do step debugging, the document opens, but when it reacher the exportasfixedformat it hangs and nothing happens.
There is some misspeling in the ExportAsFixedFormat instruction?

i've also tried with objDoc.SaveAs but nothing happens, where i expected a dialog box asking Save As.
I think i dont really understand how excel vba code pases instructions to word.
 
Upvote 0
I think i dont really understand how excel vba code pases instructions to word.

More than that what i dont understand is the early or late binding.

I have solved it with:
Code:
objDoc.SaveAs2 [COLOR=#574123]"C:\Users\Win\Desktop\abcd.pdf", 17[/COLOR]
that is late binding.
It works as charm.

Ill keep trying to debug the other function if i have time, but already solved it.
Thankyou
 
Upvote 0

Forum statistics

Threads
1,223,878
Messages
6,175,141
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top