\NewDocumentCommand and pagecounter

I have tried to write a program that will let users input a file as an appendix. These pages should be numbered with roman numbers. It has worked well, but now I am trying to make an improvement: If the user do not have an appendix I would like the pagecounter to be set to zero. My approach was to set a counter to zero and have it updated in the document command. I can see that I have missed something, but I cannot find out what. Can someone direct me to a solution? Here is an example

\documentclass[a4paper,12pt]{article}

\usepackage{xparse}
\usepackage{pageslts}

\newcounter{Appcount}
\setcounter{Appcount}{0}

\ExplSyntaxOn

\NewDocumentCommand{\app}{m}
{
\newpage
\pagenumbering{roman}
\fancyhf{}
\rhead{\itshape Side \, \thepage\ av \, \lastpageref{pagesLTS.roman}}
\appendix

\section*{Vedlegg}
\input{#1}

\setcounter{Appcount}{\lastpageref{pagesLTS.roman}}

}

\ExplSyntaxOff

\begin{document}

\app{filename.tex} %can be left out

Number of pages in the appendix: \theAppcount
\end{document}

• You should mention what doesn't work. – Ulrike Fischer Apr 29 at 8:03
• Sorry, what doesn't work is that the \setcounter{Appcount}{\lastpageref{pagesLTS.roman}}. The counter will always be zero, but the number of pages shown in the appendix is correct. – Per G. Østerlie Apr 29 at 11:56

\lastpageref{pagesLTS.roman} is not a number and can not be used in a \setcounter. You can try the refcount package and the label pagesLTS.roman.local provided by pageslts:

\documentclass[a4paper,12pt]{article}

\usepackage{xparse}
\usepackage{pageslts}
\usepackage{fancyhdr}
\newcounter{Appcount}
\setcounter{Appcount}{0}
\usepackage{refcount}
\ExplSyntaxOn
\NewDocumentCommand{\app}{m}
{
\newpage
\pagenumbering{roman}
\fancyhf{}
\rhead{\itshape Side \, \thepage\ av \, \lastpageref{pagesLTS.roman}}
\appendix

\section*{Vedlegg}
\input{#1}
\setcounter{Appcount}{\getpagerefnumber{pagesLTS.roman.local}}

}

\ExplSyntaxOff

\begin{document}

\app{test3.tex} %can be left out

Number of pages in the appendix: \theAppcount
\end{document}