Interop.word, find and replace, error:" The string parameter is too long."

Asked

Viewed 313 times

2

I am using a template to create a document, I open a document that would be a template and use find and replace keywords that is in this document and soon after saved on my desktop as another document, everything goes perfectly, but when I try to put a text higher than 255 characters I come across this error " The string parameter is too long." what can I do? I am a little layman, if you can post a code that solves my problem I would appreciate it very much , obg from now

private void FindAndReplace(Microsoft.Office.Interop.Word.Application wordApp, object findText, object replaceWithText)
    {


        object missing = Type.Missing;

        object matchCase = true;
        object matchWholeWord = true;
        object matchWildCards = false;
        object matchSoundLike = false;
        object nmatchAllForms = false;
        object forward = true;
        object format = false;
        object matchKashida = false;
        object matchDiactitics = false;
        object matchAlefHamza = false;
        object matchControl = false;
        object read_only = false;
        object visible = true;
        object replace = 2;
        object wrap = 1;

        object replaceText = true;



                wordApp.Selection.Find.Execute(ref findText,
                        ref matchCase, ref matchWholeWord,
                        ref matchWildCards, ref matchSoundLike,
                        ref nmatchAllForms, ref forward,
                        ref wrap, ref format, ref replaceWithText,
                        ref replace, ref matchKashida,
                        ref matchDiactitics, ref matchAlefHamza,
                        ref matchControl);



    }

    private void CreateWordDocument(object filename, object savaAs)
    {
        List<int> processesbeforegen = getRunningProcesses();
        object missing = Missing.Value;

        Word.Application wordApp = new Word.Application();

        Word.Document aDoc = null;

        if (File.Exists((string)filename))
        {
            DateTime today = DateTime.Now;

            object readOnly = false; 
            object isVisible = false;

            wordApp.Visible = false;


            aDoc = wordApp.Documents.Open(ref filename, ref missing, ref readOnly,
                                        ref missing, ref missing, ref missing,
                                        ref missing, ref missing, ref missing,
                                        ref missing, ref missing, ref missing,
                                        ref missing, ref missing, ref missing, ref missing);

            aDoc.Activate();

            this.FindAndReplace(wordApp, "<n>", tbPlanoNmr.Text);
            this.FindAndReplace(wordApp, "<k>", tbPortaria.Text);
            this.FindAndReplace(wordApp, "<data>", tbData.Text);
            this.FindAndReplace(wordApp, "PPPPP", tbNomeEtec.Text);
            this.FindAndReplace(wordApp, "<codigo>", tbCodigoEtec.Text);
            this.FindAndReplace(wordApp, "<municipio>", tbMunicipioEtec.Text);
            this.FindAndReplace(wordApp, "<eixo>", tbEixo.Text);
            this.FindAndReplace(wordApp, "<habilitacao>", tbHabilitacao.Text);
            this.FindAndReplace(wordApp, "<qualificacao>", tbQualificacao.Text);
            this.FindAndReplace(wordApp, "<serie>", tbModulo.Text);
            this.FindAndReplace(wordApp, "<componente>", tbMateria.Text);
            this.FindAndReplace(wordApp, "<ch>", tbCHS.Text);
            this.FindAndReplace(wordApp, "<professor>", tbNomeProf.Text);
            this.FindAndReplace(wordApp, "<modulo>", Session["modulo"].ToString());

            this.FindAndReplace(wordApp, "<VI>", txtVI.InnerText);
            this.FindAndReplace(wordApp, "<VII>", txtVII.InnerText);
            this.FindAndReplace(wordApp, "<X>", txtX.InnerText);


            foreach (GridViewRow row in GridView1.Rows)
            {
                txtDidatico.Text += ((TextBox)row.FindControl("txtProcedimento")).Text;
            }


            this.FindAndReplace(wordApp, "<procedimento>", txtDidatico.Text);

            this.FindAndReplace(wordApp, "<instrumentos>", txtInstrumentos.InnerText);
            this.FindAndReplace(wordApp, "<criterios>", txtCriterios.InnerText);

            this.FindAndReplace(wordApp, "<mes1>", mes1.InnerText);
            this.FindAndReplace(wordApp, "<mes2>", mes2.InnerText);
            this.FindAndReplace(wordApp, "<mes3>", mes3.InnerText);
            this.FindAndReplace(wordApp, "<mes4>", mes4.InnerText);
            this.FindAndReplace(wordApp, "<mes5>", mes5.InnerText);
            this.FindAndReplace(wordApp, "<mes6>", mes6.InnerText);

            this.FindAndReplace(wordApp, "<1.1>", c11.InnerText);
            this.FindAndReplace(wordApp, "<1.2>", c12.InnerText);
            this.FindAndReplace(wordApp, "<1.3>", c13.InnerText);
            this.FindAndReplace(wordApp, "<1.4>", c14.InnerText);
            this.FindAndReplace(wordApp, "<1.5>", c15.InnerText);
            this.FindAndReplace(wordApp, "<1.6>", c16.InnerText);
            this.FindAndReplace(wordApp, "<2.1>", c21.InnerText);
            this.FindAndReplace(wordApp, "<2.2>", c22.InnerText);
            this.FindAndReplace(wordApp, "<2.3>", c23.InnerText);
            this.FindAndReplace(wordApp, "<2.4>", c24.InnerText);
            this.FindAndReplace(wordApp, "<2.5>", c25.InnerText);
            this.FindAndReplace(wordApp, "<2.6>", c26.InnerText);
            this.FindAndReplace(wordApp, "<3.1>", c31.InnerText);
            this.FindAndReplace(wordApp, "<3.2>", c32.InnerText);
            this.FindAndReplace(wordApp, "<3.3>", c33.InnerText);
            this.FindAndReplace(wordApp, "<3.4>", c34.InnerText);
            this.FindAndReplace(wordApp, "<3.5>", c35.InnerText);
            this.FindAndReplace(wordApp, "<3.6>", c36.InnerText);
            this.FindAndReplace(wordApp, "<4.1>", c41.InnerText);
            this.FindAndReplace(wordApp, "<4.2>", c42.InnerText);
            this.FindAndReplace(wordApp, "<4.3>", c43.InnerText);
            this.FindAndReplace(wordApp, "<4.4>", c44.InnerText);
            this.FindAndReplace(wordApp, "<4.5>", c45.InnerText);
            this.FindAndReplace(wordApp, "<4.6>", c46.InnerText);
            this.FindAndReplace(wordApp, "<5.1>", c51.InnerText);
            this.FindAndReplace(wordApp, "<5.2>", c52.InnerText);
            this.FindAndReplace(wordApp, "<5.3>", c53.InnerText);
            this.FindAndReplace(wordApp, "<5.4>", c54.InnerText);
            this.FindAndReplace(wordApp, "<5.5>", c55.InnerText);
            this.FindAndReplace(wordApp, "<5.6>", c56.InnerText);
            this.FindAndReplace(wordApp, "<atribuicoes>", taAtribuicoes.InnerText);

            this.FindAndReplace(wordApp, "<atividades>", taAtividades.Text);
            this.FindAndReplace(wordApp, "<competencia_extenso>", txtCompetenciaExtenso.InnerText);
            this.FindAndReplace(wordApp, "<evidencias>", txtEvidencia.InnerText);
            this.FindAndReplace(wordApp, "<habilidades>", txtHabilidade.InnerText);
            this.FindAndReplace(wordApp, "<competencia>", txtCompetencia.InnerText);
            this.FindAndReplace(wordApp, "<base>", txtBase.InnerText);

            foreach (GridViewRow row in GridView1.Rows)
            {
                txtHabilidadeP33.Text += ((TextBox)row.FindControl("txtHabilidadeP3")).Text;
            }
            foreach (GridViewRow row in GridView1.Rows)
            {
                txtBaseP33.Text += ((TextBox)row.FindControl("txtBaseP3")).Text;
            }

            this.FindAndReplace(wordApp, "<basenum>", txtHabilidadeP33.Text);
            this.FindAndReplace(wordApp, "<habilidadesnum>", txtBaseP33.Text);

            this.FindAndReplace(wordApp, "#", "^l");

            //this.FindAndReplace(wordApp, "$$Date$$"     DateTime.Now.ToShortDateString());




        }
        else
        {
            //Exibir mensagem: "a dose do arquivo não existe"

            return;
        }


        aDoc.SaveAs2(ref savaAs, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing);

        aDoc.Close(ref missing, ref missing, ref missing);

        List<int> processesaftergen = getRunningProcesses();
        killProcesses(processesbeforegen, processesaftergen);
    }


    public List<int> getRunningProcesses()
    {
        List<int> ProcessIDs = new List<int>();

        foreach (Process clsProcess in Process.GetProcesses())
        {
            if (Process.GetCurrentProcess().Id == clsProcess.Id)
                continue;
            if (clsProcess.ProcessName.Contains("WINWORD"))
            {
                ProcessIDs.Add(clsProcess.Id);
            }
        }
        return ProcessIDs;
    }


    private void killProcesses(List<int> processesbeforegen, List<int> processesaftergen)
    {
        foreach (int pidafter in processesaftergen)
        {
            bool processfound = false;
            foreach (int pidbefore in processesbeforegen)
            {
                if (pidafter == pidbefore)
                {
                    processfound = true;
                }
            }

            if (processfound == false)
            {
                Process clsProcess = Process.GetProcessById(pidafter);
                clsProcess.Kill();
            }
        }
    }



    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void tEnabled(bool state)
    {
        tbNomeProf.Enabled = state;

    }

    protected void btnGerarPDF_Click(object sender, EventArgs e)
    {
        var Template = "C:/Users/Bird/Desktop/temppp.doc";

        var NovoDocumento = "C:/Users/Bird/Desktop/PTD.doc";
        tEnabled(true);

        CreateWordDocument(Template, NovoDocumento);
        tEnabled(false);




    }
}
  • https://social.msdn.microsoft.com/Forums/pt-BR/09c58208-d709-42ec-adab-4b933c62a002/replacementtext-o-parmetro-de-sequncia-de-caracteres-muito-longo-?forum=clientept

  • Obg, I managed to solve my problem

  • Put your resolution as an answer, so you can help another person to search for the same problem =]

1 answer

1

I was using find and replace this way

this.FindAndReplace(wordApp, "<texto>", texto.Text);

but it was a mistake, because the string was too long, so I switched to:

foreach (Word.Range tmpRange in aDoc.StoryRanges)
            {
                object findText = "<texto>";
                object replaceText = texto.Text;

                if (tmpRange.Find.Execute(ref findText, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing))
                {
                    tmpRange.Select();
                    wordApp.Selection.Text = replaceText.ToString();
                }
            }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.