Replacement of VBS

Can you help me to replace the following lines with an astrick to Aspose Words syntax? I am currently evaluating the product and wonder if there’re any online reference for MS Word to Aspose Words lookup.

for i = 1 to recordCount then
myDoc.Selection.WholeStory()
myDoc.Selection.EndOf()
myDoc.Selection.InsertFile "C:\Report\Template\DetailList.doc"
if i < recordCount then
myDoc.Selection.Range.InsertBreak 2
if i > 1 then
myDoc.ActiveDocument.Sections(i).Headers(1).LinkToPrevious = False
end if
myDoc.Selection.TypeParagraph()
end if
next

Thanks!

Hi
Thank you for your interest in Aspose.Words. Here is translation of your code:

// Open document of create document
Document doc = new Document(@"C:\Temp\test.doc");
// Create document builder
DocumentBuilder builder = new DocumentBuilder(doc);
// Start some loop
int recordCount = 5;
for (int i = 0; i <= recordCount; i++)
{
    // Move DocumentBuilder cursor to the end of document
    builder.MoveToDocumentEnd();
    // Open another document
    Document srcDoc = new Document(@"C:\Temp\in.doc");
    // Insert srcDoc into main doc
    InsertDocument(builder.CurrentParagraph, srcDoc);
    if (i < recordCount)
    {
        // move document builder cursor to the end of document
        builder.MoveToDocumentEnd();
        // Insert section break next page
        builder.InsertBreak(BreakType.SectionBreakNewPage);
        if (i > 1)
        {
            // Link Header/Footer to previouse section
            builder.CurrentSection.HeadersFooters.LinkToPrevious(true);
        }
        builder.InsertBreak(BreakType.ParagraphBreak);
    }
}
// Save document.
doc.Save(@"C:\Temp\out.doc");

You can find InsertDocument method here:
https://docs.aspose.com/words/java/insert-and-append-documents/
But it is “word to word” translation. If you need just append one document to another then you can use the following code as well:

// Open document of create document
Document doc = new Document(@"C:\Temp\test.doc");
// Start some loop
int recordCount = 5;
for (int i = 0; i <= recordCount; i++)
{
    // Open another document
    Document srcDoc = new Document(@"C:\Temp\in.doc");
    // Append srcDoc to the main document
    AppendDoc(doc, srcDoc);
}
// Save document.
doc.Save(@"C:\Temp\out.doc");

You can find AppenDoc method here:
https://docs.aspose.com/words/net/insert-and-append-documents/
I hope this could help you. Please feel free to ask if you have any further questions. I will be glad to help you.
Best regards.

Thanks for the quick reply Alexey! I am impressed with your user support services. OK, I was able to understand and apply your code to copy everything I need from my source document (template) to my destination document except the header and footer from the template. I need to show header from my template on every page on my destination document. How do I do that? I’ll try to search the forum for sample code; but will appreciate if you could point me to the right directions.
Buddy Tea

Hi
Thanks for your inquiry. If you append document to your template then you can link headers/footer to previous. Please see the following code:

// Open document of create document
Document doc = new Document(@"C:\Temp\test.doc");
// Start some loop
int recordCount = 5;
for (int i = 0; i <= recordCount; i++)
{
    // Open another document
    Document srcDoc = new Document(@"C:\Temp\in.doc");
    // Clear headers/footers and kink they to previouse
    foreach (Section sect in srcDoc.Sections)
    {
        sect.ClearHeadersFooters();
        sect.HeadersFooters.LinkToPrevious(true);
    }
    // Append srcDoc to the main document
    AppendDoc(doc, srcDoc);
}
// Save document.
doc.Save(@"C:\Temp\out.doc");

Hope this helps.
Best regards.

Thanks Alexey! I got it to work both using your sample code and trial/error of my own. I have got my destination document sections established and I am ready to populate the table cells in the headers and table cells outside the headers.
How do I translate the following two line of ASP code…

wdApp2.ActiveDocument.Sections("<%=sectionNum%>").Headers(1).Range.Tables(1).Cell(1,2).Range.Text = "<%=Type%>: <%=recno%> Detail Report" & vbCrLf & "<%=from & to%>"
With wdApp2.ActiveDocument.Sections("<%=sectionNum%>").Range.Tables(1).Cell(1,1).Range.Characters(7)
.Select
.Text = " <%=rTitle%>"

I tried the following code myself and it didn’t work as “Aspose.Words.Range.Text” is a read only property.

int secNum = 1;
////Populate destination document with data
foreach (DataRow drCurrent in myDS.Tables[0].Rows)
{
    doc.Sections[secNum].HeadersFooters[HeaderFooterType.HeaderFirst].Tables[1].Rows[1].Cells[2].Range.Text = drCurrent["Type"] + ": " + drCurrent["recno"] + " Detail Report";
}

Thanks again for your help,
Buddy Tea

Hi
Thanks for your inquiry. Aspose.Words uses zero based indexes. So you should use the following code:

// Open document
Document doc = new Document(@"Test032\in.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
// Get header cell
Cell hCell = doc.Sections[0].HeadersFooters[HeaderFooterType.HeaderPrimary].Tables[0].Rows[0].Cells[1];
// Move Document builder cursor to this cell
builder.MoveTo(hCell.FirstParagraph);
// Insert some value
builder.Write("this is value in header");
// Get cell from body
Cell bCell = doc.Sections[0].Body.Tables[0].Rows[0].Cells[0];
// move to this cell and insert some value
builder.MoveTo(bCell.FirstParagraph);
builder.Write("This is value in body");
// Save document
doc.Save(@"Test032\out.doc");

You can also do the same using DocumentBuilder.MoveToCell method.

// Open document
Document doc = new Document(@"Test032\in.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
// Move document Builder cursor to primary header
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
// Move to the corresponding cell
builder.MoveToCell(0, 0, 1, 0);
// Insert some value
builder.Write("this is value in header");
// Move cursor to Body
builder.MoveToSection(0);
// Move to the corresponding cell
builder.MoveToCell(0, 0, 0, 0);
builder.Write("This is value in body");
// Save document
doc.Save(@"Test032\out.doc");

I hope this could help you.
Best regards.

Hi Alexey,
The sample code you provided works great when I write to the table cell of each section; but I received “Specified argument was out of the range of valid values.
Parameter name: tableIndex” on line 8 when secNum = 1. It seems like if I have more than one section; I can’t write to the table cell in the header.

//Aspose.Words uses zero based indexes
int secNum = 0;
//Populate destination document with data
foreach (DataRow drCurrent in myDS.Tables[0].Rows)
{
    builder.MoveToSection(secNum);
    builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
    builder.MoveToCell(0, 0, 0, -1);
    builder.Write(drCurrent["Type"] + ": " + drCurrent["recno"] + " Detail Report");
    builder.MoveToCell(0, 0, 0, -1);
    builder.Write(drCurrent["Title"].ToString());
    builder.MoveToCell(0, 0, 1, -1);
    builder.Write(drCurrent["Description"].ToString());
    //Next section
    secNum = secNum + 1;
}

Any idea what I might have missed?
Thanks for your help!

Hi
Thanks for your inquiry. Could you please attach your template for testing? I will check it and let you know what is wrong.
Best regards.

Here’s the template (detailReport7.doc) I am using and an sample Aspose Words report created (Aspose_Eval_Detail_Report.doc). As you can see, I was able to populate the table cells in each document sections as I loop through my dataset row. What I wasn’t able to do is write the record # of each dataset row at the table cell marked “Record #:” after section 1.
Thanks again for your help!

Hi
Thank you for additional information. This occurs because actually second section does not have primary header, it is linked to previous section. So when you move DocumentBuilder cursor to primary header it is created from scratch and does not have any nodes. As a workaround you can clone header from first section. Please see the following code:

// Aspose.Words uses zero based indexes
int secNum = 0;
// Populate destination document with data
HeaderFooter primaryHeader = null;
for (int i = 0; i < 3; i++)
{
    builder.MoveToSection(secNum);
    // Check whether section has primary header
    if (builder.CurrentSection.HeadersFooters[HeaderFooterType.HeaderPrimary] != null)
    {
        // Save clone of section in the temporary variable
        primaryHeader = (HeaderFooter)builder.CurrentSection.HeadersFooters[HeaderFooterType.HeaderPrimary].Clone(true);
        builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
    }
    else
    {
        // Insert Header
        builder.CurrentSection.HeadersFooters.Add(primaryHeader.Clone(true));
        // And unlink header
        builder.CurrentSection.HeadersFooters.LinkToPrevious(false);
        builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
    }
    builder.MoveToCell(0, 0, 0, -1);
    builder.Write("Detail Report");
    builder.MoveToCell(0, 0, 0, -1);
    builder.Write("Title");
    builder.MoveToCell(0, 0, 1, -1);
    builder.Write(secNum.ToString());
    // Next section
    secNum = secNum + 1;
}

Also I would like to suggest you to use MailMerge feature for populating your template.
Here is code example:

// Create some dataTable
DataTable table = new DataTable("myTable");
// Insert few columns
table.Columns.Add("ID");
table.Columns.Add("FirstName");
table.Columns.Add("LastName");
table.Columns.Add("Company");
// Insert some data
table.Rows.Add(new object[] { "1", "Alexey", "Noskov", "Aspose" });
table.Rows.Add(new object[] { "2", "Mike", "Smith", "Apple" });
// etc...
// Open template
Document doc = new Document(@"Test037\in.doc");
// Execute mail merge
doc.MailMerge.Execute(table);
// Save the document
doc.Save(@"Test037\out.doc");

Also see the attached documents.
You can find more information about mail merge in the documentation:
https://docs.aspose.com/words/java/mail-merge-and-reporting/
Best regards.

Thank Alexey! Your sample code is very helpful. Does Aspose Words, Cells and Slices support .NET Framework 3.5?

Hi
Thanks for your inquiry. Yes of course you can use Aspose.Words, Aspose.Cells and Aspose.Slides under .NET framework 3.5. Just use .NET 2.0 assemblies.
Best regards.

Great! Can Mac users open up files output from Aspose.Words, Slices and Cells?

Hello!
Aspose.Words, Aspose.Slides and Aspose.Cells produce the same files as MS Office applications. So if you have any software to open them on Mac there is no problem. Do you mean anything else?
Regards,

Thanks Viktor! I guess the best way to find out is to point our Mac users to Aspose Demo site to test run some reports. You guys have been wonderful as far as getting back to me with answers and sample code.

Yes, it is reasonable. If they can open result documents then proper software is already installed. I think that’s not a problem to work with MS Office formats on Mac. They are very popular and common. Moreover Aspose demo applications show some frequent business cases. Please feel free to ask any further questions.
Regards,