Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide pdf importer popup in word automation

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.

like image 499
Kalten Avatar asked Mar 07 '16 10:03

Kalten


People also ask

How do I open an embedded PDF in Word?

Double-click the file to view its details and contents in Windows Explorer. 3. Click the word folder, then click the embeddings folder. Embedded content (such as embedded PDF files) will be displayed as oleObject.

Can you open a PDF in Word?

Go to File > Open. Find the PDF and open it (you might have to select Browse and find the PDF in a folder). Word tells you that it's going to make a copy of the PDF and convert its contents into a format that Word can display. The original PDF won't be changed at all.


1 Answers

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);
    }
}
like image 126
Kalten Avatar answered Oct 23 '22 01:10

Kalten