VBA code to open word from excel

lukemeyer

New Member
Joined
Jun 25, 2011
Messages
15
I have not taken VBA for 10 years and am very rusty. I am not sure if this is possible, but I figure I would ask.


I am trying to write a macro in excel that does 3 things.

1. Opens a word file C:\Users\xxxx\Desktop\test.docx
2. Does a save as, and saves the file as a pdf in word.
If I can make this so it an easy programmable location that would be awesome.
3. It then closes the word without saving it.

If anyone knows please let me know if this is possible and example code would be great.


I have written it so it opens the file so far.
Code is posted below

Set wordapp = CreateObject("word.Application")

wordapp.documents.Open "C:\Users\xxxx\Desktop\test.docx"
wordapp.Visible = True
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
I'm not an expert by any means, but one way you could do this would be to first make your word doc a macro-enabled one (I called mine "test.docm"), then put your "save as" routine in the document's _open event.
Then you could open the document with your existing code, and the save as .pdf would be done from the Word doc. Seems to work for me:

Workbook code:
Code:
Sub tst()

Set wordapp = CreateObject("word.Application")
wordapp.documents.Open "C:\Users\XXXX\Desktop\test.docm"
wordapp.Visible = True
End Sub

Word doc code:
Code:
Private Sub Document_Open()


    ChangeFileOpenDirectory "C:\Users\XXXXX\Desktop\"
    ActiveDocument.ExportAsFixedFormat OutputFileName:= _
        "C:\Users\XXXXX\Desktop\zzzz.pdf", ExportFormat:=wdExportFormatPDF, _
        OpenAfterExport:=True, OptimizeFor:=wdExportOptimizeForPrint, Range:= _
        wdExportAllDocument, From:=1, To:=1, Item:=wdExportDocumentContent, _
        IncludeDocProps:=True, KeepIRM:=True, CreateBookmarks:= _
        wdExportCreateNoBookmarks, DocStructureTags:=True, BitmapMissingFonts:= _
        True, UseISO19005_1:=False

End Sub

Obviously, substitute the XXXXX for the real paths.

I used the macro recorder to get the Word code, so this could probably be cleaned up considerably.
Hopefully this will get you on the right track, at least.

Good luck.....
 
Upvote 0
Thank you. That makes sense. I was trying to do it all from excel, but this makes more sense. Thank you again for your help.
 
Upvote 0

Forum statistics

Threads
1,224,504
Messages
6,179,142
Members
452,892
Latest member
JUSTOUTOFMYREACH

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