1

I am automating processing of PDF files via VBA, but the problem I cannot seem to resolve is Word helpfully showing a dialog box regarding file conversion. I am looking for a solution which doesn't require modifying the Registry, as this is not permitted in the work environment

Word will now convert your PDF to an editable Word Document...

This dialog does offer a check-box 'Don't show this message again' which works as advertised, but I need the code to be portable across PCs and users

The host application is actually Access, where I have a reference to the Microsoft Word 16.0 Object Library. I also tried directly in Word with the same issue.

Below is the code I'm using - it does work, other than that Word provides a dialog box which I don't want.

    Sub convertPDFtoTextViaWord()
       Const filePath As String = "C:\myfilepath\"
       Dim file As String, fileName As String
       Dim myWord As Word.Application, myDoc As Word.Document
       Set myWord = New Word.Application
       file = Dir(filePath & "*.pdf")
       myWord.DisplayAlerts = wdAlertsNone
       Do While file <> ""
           fileName = Replace(file, "pdf", "txt")
           Set myDoc = myWord.Documents.Open(fileName:=filePath & file, ConfirmConversions:=False, Format:="PDF Files")
           myDoc.SaveAs2 filePath & fileName, FileFormat:=wdFormatText, Encoding:=1252, lineending:=wdCRLF
           myDoc.Close False
           file = Dir
       Loop
       Set myDoc = Nothing
       Set myWord = Nothing
    End Sub

I should note that this code does as intended now that I have selected the 'Don't show this message again' checkbox, but I want to programmatically avoid the need to do this, as I will not always be in a position to perform this manual step

I should also note that I tried running the same code (with a few appropriate modifications) directly in Word with the same result

I added the line myWord.DisplayAlerts = wdAlertsNone because this is how I would do it in Excel (where I have far more VBA experience)

I also tried changing Format:="PDF Files" to Format:=wdOpenFormatAuto (which is the default value for the parameter, anyway) with no change

To reiterate, the code works as desired other than that I can't suppress the dialog box which it seems should be exactly what ConfirmConversions:=False supports.

Using Office365 ProPlus

Thanks

emjaySX
  • 149
  • 11
  • This link might help: [MS Word VBA docx to txt message trap code](https://stackoverflow.com/questions/24793695/ms-word-vba-docx-to-txt-message-trap-code) – Rene Oct 21 '19 at 04:07
  • Or maybe this one (if you are willing/permitted to fiddle with the Windows registry): [Open PDF in Word without Conversion Message](https://social.msdn.microsoft.com/Forums/en-US/57f49b29-58cf-45e5-97cc-985fdd9a8c17/open-pdf-in-word-without-conversion-message?forum=isvvba) – Rene Oct 21 '19 at 04:08
  • @Rene - First link seems unrelated, the second link looks very relevant, but I have no way of programmatically interacting with the Windows registry (probably rightly) at work. The lack of other solutions there, though, make me pessimistic about there actually being a solution. Thanks anyway – emjaySX Oct 21 '19 at 04:17
  • I did a test with modified code within Word VBA to convert a specific file, not loop through folder. A Word document opens then saves as a .txt file. No popup. Likely won't make a difference but suggest not declaring a variable name same as function argument keyword (FileName). – June7 Oct 21 '19 at 04:40
  • @june7 The problem is specific to opening PDFs in Word - there is no dialog box in Word regarding file conversion when opening a Word document in Word. As I said in my post, the code works as presented, but I need to suppress the dialog box in Word, though I take your point that having a variable with the same name as a function argument should be avoided – emjaySX Oct 21 '19 at 05:06
  • Your question says "I also tried directly in Word with the same issue." I am now testing full procedure in Access. Get "Invalid procedure" error on `file=Dir`. First txt file created. Still no popup. – June7 Oct 21 '19 at 05:08
  • It seems my second call to the DIR function to test whether the file has already been converted doesn't work as intended, I will edit the original post to remove this line, good pikc-up – emjaySX Oct 21 '19 at 05:18
  • Now code executes without error and without popup. Cannot replicate issue. I am using Access 2010, not Office 365. – June7 Oct 21 '19 at 05:52
  • 1
    @June7 Since Word 2010 cannot open PDF files, testing using Office 2010 won't help with the main issue, I think... – Cindy Meister Oct 21 '19 at 07:52
  • 1
    Possible duplicate of [Loop over PDF files and transform them into doc with word](https://stackoverflow.com/questions/45890170/loop-over-pdf-files-and-transform-them-into-doc-with-word), See also https://stackoverflow.com/questions/58435301/c-sharp-word-interop-open-a-pdf-without-the-conversion-prompt – Cindy Meister Oct 21 '19 at 07:53
  • @CindyMeister - your 'possible duplicate' is entirely unrelated, despite the similarity of tags - that question boils down to "why am I getting an error/ why is there no output from my code?". My code is working, barring the suppression of the file conversion dialog. From my further reading, including some of the links provided, I conclude that this is a bug in the implementation of VBA in Word, and that the solution is to modify the registry, either programmatically or by clicking the 'Don't show this message again' check-mark on the dialog, which achieve the same outcome via different means – emjaySX Oct 21 '19 at 23:24

0 Answers0