PDF to TXT automating word from Excel.... RRS feed

  • Question

  • Hi...

    I am trying to do something I haven't tried to do before, and control Word from Excel VBA.

    I have a report that is only available as a PDF.  My idea was to open it in Word and save it as a .TXT file which I can import into my Excel spreadsheet.  When I try to do this manually it works well, but when I try to do it programmatically I get what looks like a .TXT file of the unconverted PDF.

    My Code is;

    Sub word()

    Dim wrdApp As Object

    Dim wrdDoc As Object

    Set wrdApp = CreateObject("Word.Application")
    wrdApp.Visible = True
    Set wrdDoc = wrdApp.Documents.Add

    (And then culled from Recording a Macro;)

    wrdApp.ChangeFileOpenDirectory "C:\Users\Kevin\Desktop\Downloaded docs\"

    wrdApp.Documents.Open Filename:="Filename.pdf", ConfirmConversions:=False, ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", WritePasswordTemplate:="" ', Format:=wdformatauto, XMLTransform:=""

    (Which loads the file into Word)

    wrdApp.ChangeFileOpenDirectory "C:\Users\Kevin\Desktop\Downloaded docs\"

    wrdApp.activedocument.SaveAs2 Filename:="Text.txt", FileFormat:=wdformattext, LockComments:=False, Password:="",      AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:=False, Encoding:=1252, InsertLineBreaks:=False, AllowSubstitutions:=False, LineEnding:=wdCRLF, CompatibilityMode:=0

    (which saves the Word Doc as a .TXT file)

    Set wrdApp = Nothing

    Set wrdDoc =Nothing

    End Sub

    In order to get this to work I have to remove Format:=wdformatauto from the Documents.Open command and remove the line wrdApp.ActiveProtectedViewWindow.Edit all together.

    I believe that my problem is somewhere in the two parts above, but am at the limit of my ability here!

    Thank you for any help you can give.


    Friday, October 28, 2016 11:13 AM

All replies

  • Hi Kevin,

    Based on your description, I will move your thread to Word for Developer forum:

    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us. Thank you for your understanding.

    Emi Zhang
    TechNet Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact

    Monday, October 31, 2016 8:56 AM
  • Hello Emi

    Thanks for your advice and help!



    Monday, October 31, 2016 9:26 AM