none
Best Solution to insert a file on a placeholder RRS feed

  • Question

  • I have this commitment and I don't know which is the best way to follow.

    I produce in HTML a text like this, where inside a content I have to put some placeholder (they could be <p>, <span> <h3> etc.; obviously they cannot be Word bookmarks; I surrounded the placeholder with # only for sample...)

    Then I copy-paste in Word and launching a VSTO Code from a custom ribbon, I have to substitute the placeholder with the content of other Word files, named like the placeholders (path is common; eg c:\temp\MyFile001.docx, c:\temp\MyFile002.docx)

    The Range.InsertFile() works perfectly for my purpose.

    But how can I iterate on my document, find and select my placeholders? (over the selection then I will apply the Range.InsertFile)

    Do you have some suggestions of what the placeholders could be in html, to ease the searching in Word ?

    Thanks in advance
    Sandro

    Wednesday, January 31, 2018 5:37 PM

Answers

  • Hello SandroR,

    Did you put any string in the placeholder? Such as MYFILE001, MYFILE002.. in your screenshot. 

    If so, you could try to use Find to get the range using the string and then try to use Range.InsertFile to insert content in the document.

    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.


    • Edited by Terry Xu - MSFT Thursday, February 1, 2018 9:01 AM
    • Marked as answer by SandroR Thursday, February 1, 2018 2:39 PM
    Thursday, February 1, 2018 9:00 AM

All replies

  • Hello SandroR,

    Did you put any string in the placeholder? Such as MYFILE001, MYFILE002.. in your screenshot. 

    If so, you could try to use Find to get the range using the string and then try to use Range.InsertFile to insert content in the document.

    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.


    • Edited by Terry Xu - MSFT Thursday, February 1, 2018 9:01 AM
    • Marked as answer by SandroR Thursday, February 1, 2018 2:39 PM
    Thursday, February 1, 2018 9:00 AM
  • Thanks Terry, that's was my first idea and you confirm I was on the right path.

    Here my skeleton solution:

    public void InsertMaintenanceProcedures() { int intFound = 0; Document document = Globals.ThisAddIn.Application.ActiveDocument; Range rng = document.Content; rng.Find.ClearFormatting(); rng.Find.Forward = true; rng.Find.Text = "MPLPROC_"; rng.Find.MatchCase = true; rng.Find.Execute(); while (rng.Find.Found) { if (InsertFileProc(rng)) intFound++; rng.Find.Execute(); } MessageBox.Show("Procedures Inserted: " + intFound.ToString()); } private bool InsertFileProc(Range rng) { bool result = false;

    // +4 because format of filename is MPLPROC_0000 Range rngProc = Globals.ThisAddIn.Application.ActiveDocument.Range(rng.Start, rng.End + 4); rngProc.Select(); String path = @"C:\temp\testmpl\"; String fileProc = Path.Combine(path, rngProc.Text + ".docx"); if (File.Exists(fileProc)) { rngProc.InsertFile(fileProc); result = true; } else { MessageBox.Show("File " + fileProc + " not found."); } return result; }


    Thursday, February 1, 2018 2:41 PM