none
Can't open MS publisher file pro-grammatically using C# RRS feed

  • Question

  • Hi,

    We are developing an application in C# (.NET Framework 4.5) to open MS Publisher File and convert into PDF.

    We are having an issue while opening a file in Windows Server 2016 with Office (2013 - exact version 15.0.4869.1000 ).

    Same file works as expected in Windows 2012 R2 with Office (2013 - exact version 15.0.4869.1000).

    Here is the sample code we are using for opening a file,

    public MSPublisherDocument Open(string filename, bool ReadOnly, bool AddToRecentFiles, Microsoft.Office.Interop.Publisher.PbSaveOptions options)
            {
             object[] Params = new object[]
                    {
                        filename,
                        ReadOnly,  //true
                        AddToRecentFiles, //false
                        options
                    };

                    //Open the first work sheet
                    MSPublisherDocument It = new MSPublisherDocument(_Application.GetType().InvokeMember(
                        "Open", 
                        System.Reflection.BindingFlags.InvokeMethod,
                    null,
                    _Application,
                    Params,
                    CultureInfo.InvariantCulture));
                    return It;
            }

    Here is the sample,

    https://drive.google.com/open?id=1g-ynEkpg33Ic95ky6hLbvjX0RU1TgC-2

    It would be great if you can help us to figure out the actual cause.

    Thanks.

    Monday, April 16, 2018 9:03 AM

All replies

  • Hello AvinashMR,

    What is MSPublisherDocument ? I checked Publisher Object Model and did not find such an Object.

    For opening MS Publisher File and convert into PDF, we usually try to use Publisher Interop dll. Please try to refer to below code.

    using Publisher=Microsoft.Office.Interop.Publisher;
    
    
                Publisher.Application application = new Publisher.Application();
                application.ActiveWindow.Visible = true;
                string path = @"C:\Users\terryx\Downloads\Test_File.pub";
                Publisher.Document document = application.Open(path, false, true, Publisher.PbSaveOptions.pbPromptToSaveChanges);
                document.ExportAsFixedFormat(Publisher.PbFixedFormatType.pbFixedFormatTypePDF, @"C:\Users\terryx\Desktop\ExportedPDF.pdf");
                document.Save();
                document.Close();
                application.Quit();

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 17, 2018 8:02 AM
  • Thanks for the quick response, Terry. 

    I tried your code and it works fine in Windows Console App. But we have all the logic in Windows Service.

    Application.Open() doesn't return and getting hanged at following line of code,

                Publisher.Document document = application.Open(path, false, true, Publisher.PbSaveOptions.pbPromptToSaveChanges);

    Any help really appreciated.

    Thanks.

    Wednesday, April 25, 2018 3:37 PM
  • Hello AvinashMR,

    As far as I know, Microsoft does not support office automation on server-side now.

    Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

    Please check below link for more information.

    Considerations for server-side Automation of Office

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 26, 2018 2:21 AM