Skip to main content

Notifications

Announcements

No record found.

Small and medium business | Business Central, N...
Unanswered

Getting error while printing Document

Posted on by 245

Hello Experts , in my code I wrote code to print a Document like below

Print()

{

IF NOT TryPrintDocument(Path,Name) THEN
ERROR('Not able to print %1. \\ERROR: %2',Path+Name, GETLASTERRORTEXT)

}

[TryFunction] TryPrintDocument(PathP : Text;NameP : Text)

{
ProcessStartInfo := ProcessStartInfo.ProcessStartInfo(PathP+NameP);
WITH ProcessStartInfo DO BEGIN
Verb := 'Print';
CreateNoWindow := TRUE;
WindowStyle := ProcessWindowStyle.Hidden;
END;
Process.Start(ProcessStartInfo);
CLEAR(ProcessStartInfo);
CLEAR(Process);
CLEAR(ProcessStartInfo);

}

But  While running this code I am getting error 

Error : 

Not able to print C:\Temp\test.pdf.

ERROR: A call to System.Diagnostics.Process.Start failed with this message: No application is associated with the specified file for this operation

  • RE: Getting error while printing Document

    Just one more thing:

    If you need to research this further in non-NAV forums, then C/AL example won't help much, so below is C# code that can reproduce the same thing completely outside of NAV, in case it can help you bring this up in Windows / c# forums.

    hth

    Lars

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Net.Configuration;

    using System.Net.Http;

    using System.Text;

    using System.Threading.Tasks;

    using System.Diagnostics;

    namespace ConsoleApp1

    {

       class Program

       {

           static void Main(string[] args)

           {

               System.Diagnostics.ProcessStartInfo ProcessStartInfo = new ProcessStartInfo("c:\\temp\\Test.pdf");

               ProcessStartInfo.Verb = "print";

               System.Diagnostics.Process Process = new Process();

               Process.Start(ProcessStartInfo);

               Console.WriteLine("Done");

               Console.ReadKey();

           }

       }

    }

  • RE: Getting error while printing Document

    Hi, and thanks for more details. I can also repro now. I am pretty sure that this is a setting in Windows as for example mentioned here https://stackoverflow.com/questions/21032151/no-application-is-associated-with-the-specified-file-for-this-operation-vb-net . You will see that the code works fine on a .docx file, but not on a .pdf file. And you see the same if you right-click these types of files in Windows Explorer - .docx has a "Print" option. .Pdf doesn't.

    I don't know if you need to install another pdf renderer - mine is set to MSEdge, or mabye somewhere in registry can fix it, but you need to get Windows to add a "print"-action to pdf documents somehow. Sorry I am not sure about how to do that but I hope this helps you forward and let us know if you find out,

    /Lars

    Repro code:

    //Print()

    Path := 'c:\temp\';
    Name := 'Doc.docx';
    Name := 'Test.pdf';

    ///IF FALSE THEN
    IF NOT TryPrintDocument(Path,Name) THEN
    ERROR('Not able to print %1. \\ERROR: %2',Path+Name, GETLASTERRORTEXT);

    ///TryPrintDocument(Path,Name);

    MESSAGE('Done.');
    END;

    }
    CODE
    {
    VAR
    ProcessStartInfo@1000 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Diagnostics.ProcessStartInfo";
    Process@1001 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Diagnostics.Process";
    Path@1002 : Text;
    Name@1003 : Text;

    [TryFunction]
    [LineStart(15)]
    LOCAL PROCEDURE TryPrintDocument@1(PathP@1000 : Text;NameP@1001 : Text);
    BEGIN
    ProcessStartInfo := ProcessStartInfo.ProcessStartInfo(PathP+NameP);
    WITH ProcessStartInfo DO BEGIN
    Verb := 'Print';
    ///Verb := '';
    CreateNoWindow := TRUE;
    ///WindowStyle := ProcessWindowStyle.Hidden;
    END;
    Process.Start(ProcessStartInfo);
    CLEAR(ProcessStartInfo);
    CLEAR(Process);
    CLEAR(ProcessStartInfo);
    END;

  • Manikanta_501 Profile Picture
    Manikanta_501 245 on at
    RE: Getting error while printing Document

    Subtype of ProcessStartInfo DotNet Variable is --> System.Diagnostics.ProcessStartInfo.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

  • RE: Getting error while printing Document

    Hi,

    Thanks for more details. What sub type is the DotNet variable? Maybe if you can attach or copy in the whole object (exported as txt)? And would we be able to reproduce it?

  • Manikanta_501 Profile Picture
    Manikanta_501 245 on at
    RE: Getting error while printing Document

    Hello Lars Lohndorf-Larsen

    In my system default printer is setup.

    I am getting an error at Process.Start(ProcessStartInfo); Line

    and ProcessStartInfo is DotNet Variable .

  • RE: Getting error while printing Document

    Hi, a bit of a guess - maybe just check that the system has a default printer?

    If not, then do you know what line it errors on, and what kind of variable is ProcessStartInfo?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans