7

When I open a PDF file with Word automation, it show a dialog that ask me to confirm the convertion (With a "do not show again" checkbox). enter image description here

Word will now convert your PDF to an editable Word document. This may take a while. The resulting Word document will be optimized to allow you to edit the text, so it might not look exactly like the original PDF, especially if the file contained lots of graphics.

How to hide this dialog ?

var application = new Microsoft.Office.Interop.Word.Application();
application.Visible = false;

try { application.ShowStartupDialog = false; }
catch { }
try { application.DisplayAlerts = WdAlertLevel.wdAlertsNone; }
catch { }

var doc = application.Documents.Open(
    inputFilePath,
    ConfirmConversions: false,
    ReadOnly: true,
    AddToRecentFiles: false,
    Revert: true,
    NoEncodingDialog: true);

PS : ConfirmConversions:true add an other dialog.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Kalten
  • 4,092
  • 23
  • 32
  • What if you set `ConfirmConversions` to `true`? – diiN__________ Mar 07 '16 at 11:01
  • `ConfirmConversions` to `true` add an other dialog box that allow to select the inupt file format (plain text; RTF; PDF; ...) – Kalten Mar 07 '16 at 12:13
  • 1
    This is Word 2013? 2016? This is a user configuration setting for the application, probably stored in the Windows Registry, but I don't know if it has a separate key or is part of the binary "Data" key... Short of figuring that out and changing the Registry setting I don't think there's much you can do. Keep in mind that changing the Registry setting might be annoying for the user, who may rely on this message. So if you do figure it out and change it, be sure to change it back. Someone in the Word IT Pro forum on TechNet might know the Registry info, since they deal with the Registry a lot. – Cindy Meister Mar 07 '16 at 17:30
  • I can't resist it, here it goes: 'just click on the "do not show again" checkbox'. Now seriously, I've had the same issue in an excel macro that opened a hundred of pdf files - what I did was opening the first file with the `WordApplication` visible, let the user deal with the dialog, and then made the application invisible for the remainig files. I added a warning instructing the user to click on that checkbox (that's why the joke) - and that was enough for my particular scenario. Editing the registry on each user's machine was not an option to me back then. I hope that there is a better way. – dnep Mar 07 '16 at 22:41

1 Answers1

8

The solution is to edit office registry. Software\Microsoft\Office\{version}\Word\Options : DisableConvertPdfWarning

The code below add or edit the registry key and restore state when disposing.

public class WordPdfImportWarningRemover : IDisposable
{
    private const string RegistryDirectoryFormat = @"Software\Microsoft\Office\{0}\Word\Options";
    private const string RegistringKeyName = "DisableConvertPdfWarning";
    private object _oldValue;
    private RegistryValueKind _oldValueKind;
    private bool _keyExists;
    private bool _registryExists;

    public WordPdfImportWarningRemover()
    {
        EditRegistry("16.0");
    }

    public WordPdfImportWarningRemover(string version)
    {
        if(version == null)
            throw new ArgumentNullException(nameof(version));

        EditRegistry(version);
    }

    private void EditRegistry(string version)
    {
        RegistryKey officeOptions = Registry.CurrentUser.OpenSubKey(string.Format(RegistryDirectoryFormat, version), true);
        if (officeOptions != null)
        {
            using (officeOptions)
            {
                _registryExists = true;
                var keys = officeOptions.GetValueNames();
                if (keys.Contains(RegistringKeyName))
                {
                    _keyExists = true;
                    _oldValue = officeOptions.GetValue(RegistringKeyName);
                    _oldValueKind = officeOptions.GetValueKind(RegistringKeyName);
                }
                else
                {
                    _keyExists = false;
                }
                officeOptions.SetValue(RegistringKeyName, 1, RegistryValueKind.DWord);
                officeOptions.Close();
            }
        }
        else
        {
            _registryExists = false;
        }
    }

    public void Dispose()
    {
        if (_registryExists)
        {
            RegistryKey officeOptions = Registry.CurrentUser.OpenSubKey(string.Format(RegistryDirectoryFormat, "16.0"), true);
            if (officeOptions != null)
            {
                using (officeOptions)
                {
                    if (_keyExists)
                    {
                        officeOptions.SetValue(RegistringKeyName, _oldValue, _oldValueKind);
                    }
                    else
                    {
                        officeOptions.DeleteValue(RegistringKeyName, false);
                    }

                    officeOptions.Close();
                }
            }
        }
    }
}

The office version can be found with this function

private static string FindWordVersion()
{
    var application = new Microsoft.Office.Interop.Word.Application();
    try
    {
        string version = application.Version;
        return version;
    }
    finally
    {
        application.Quit(SaveChanges: false);
    }
}
Kalten
  • 4,092
  • 23
  • 32