We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

VB code to Aspose word

I have to convert code written in VB/VBScript to Aspose format. The output of the report is in the .doc format. What is the starting point? Do you have any example?

This message was posted using Page2Forum from Technical Support - Aspose.Words for .NET and Java

Hi

Thanks for your request. There are code examples in the product documentation. See the following link:

http://www.aspose.com/documentation/file-format-components/aspose.words-for-.net-and-java/programmers-guide.html

If you have problems with converting your code to Aspose.Words please post your code here and I will help you to convert it.

Best regards.

Could you please what is the Aspose code for the VB code below? Thanks.

wdApp.ActiveDocument.Sections(1).Headers(1).Range.Tables(1).Cell(1,2).Range.Text = “”

wdApp.ActiveDocument.Sections(1).Headers(1).Range.Tables(1).Cell(2,2).Range.Text = " #<%=FlightNo%> Flight/Stage Affectivity (<%=FlightLaunchDate%>) - Status as of <%=myDate%>"

--------------------------------------------------------------------------

With wdApp.ActiveDocument.Tables(1)

With .Borders(-1)

.LineStyle = 1

.LineWidth = 24

End With

With .Borders(-2)

.LineStyle = 1

.LineWidth = 24

End With

With .Borders(-3)

.LineStyle = 1

.LineWidth = 24

End With

With .Borders(-4)

.LineStyle = 1

.LineWidth = 24

End With

End With

set wdApp = Nothing

----------------------------------------------------------------------

Set flightsTable = wdApp.ActiveDocument.Tables(1)

with flightsTable.cell(rowcount, colcount).range

with .characters(1)

.text = " " & val

.font.bold = 0

end with

with .characters(1)

.text = label & “:”

.font.bold = 1

end with

end with

end sub 'output

sub setup_rows

dim i

dim arow

with flightsTable

for i = 1 to 5

.rows.add

next

.rows(.rows.count - 4).borders(-1).linewidth = 24

.cell(.rows.count - 4, 1).split 1,5

.cell(.rows.count - 1, 1).split 1,3

end with

end sub 'setup_rows

Hi

Thank you for additional information. Here is code:

  1. You can use DocumentBuidler to insert value into the table

[C#]

//Open document and create DocumentBuilder

Document doc = new Document(@“C:\Temp\in.doc”);

DocumentBuilder builder = new DocumentBuilder(doc);

//Get primary header from the first section

HeaderFooter primaryHeader = doc.FirstSection.HeadersFooters[HeaderFooterType.HeaderPrimary];

//Remove value of second cell of first row

primaryHeader.Tables[0].Rows[0].Cells[1].ChildNodes.Clear();

primaryHeader.Tables[0].Rows[0].Cells[1].EnsureMinimum();

//Move documentBuilder cursor to the second cell of the second row

builder.MoveTo(primaryHeader.Tables[0].Rows[1].Cells[1].FirstParagraph);

//Insert some text

builder.Write("#<%=FlightNo%> Flight/Stage Affectivity (<%=FlightLaunchDate%>) - Status as of <%=myDate%>");

//Save document

doc.Save(@“C:\Temp\out.doc”);

[VB]

'Open document and create DocumentBuilder

Dim doc As Document = New Document(“C:\Temp\in.doc”)

Dim builder As DocumentBuilder = New DocumentBuilder(doc)

'Get primary header from the first section

Dim primaryHeader As HeaderFooter = doc.FirstSection.HeadersFooters(HeaderFooterType.HeaderPrimary)

'Remove value of second cell of first row

primaryHeader.Tables(0).Rows(0).Cells(1).ChildNodes.Clear()

primaryHeader.Tables(0).Rows(0).Cells(1).EnsureMinimum()

'Move documentBuilder cursor to the second cell of the second row

builder.MoveTo(primaryHeader.Tables(0).Rows(1).Cells(1).FirstParagraph)

'Insert some text

builder.Write("#<%=FlightNo%> Flight/Stage Affectivity (<%=FlightLaunchDate%>) - Status as of <%=myDate%>")

'Save document

doc.Save(“C:\Temp\out.doc”)

  1. You can’t directly set borders of the table. But you can do the same using cells borders and rows borders. See the code below

[C#]

//Open document and create DocumentBuilder

Document doc = new Document(@“C:\Temp\in.doc”);

//Get first table from the document

Table tab = doc.FirstSection.Body.Tables[0];

//Set top and bottom borders

tab.FirstRow.RowFormat.Borders[BorderType.Top].LineStyle = LineStyle.Single;

tab.FirstRow.RowFormat.Borders[BorderType.Top].LineWidth = 2.25;

tab.LastRow.RowFormat.Borders[BorderType.Bottom].LineStyle = LineStyle.Single;

tab.LastRow.RowFormat.Borders[BorderType.Bottom].LineWidth = 2.25;

//Set fight and left borders

foreach (Row row in tab.Rows)

{

row.FirstCell.CellFormat.Borders[BorderType.Left].LineStyle = LineStyle.Single;

row.FirstCell.CellFormat.Borders[BorderType.Left].LineWidth = 2.25;

row.LastCell.CellFormat.Borders[BorderType.Right].LineStyle = LineStyle.Single;

row.LastCell.CellFormat.Borders[BorderType.Right].LineWidth = 2.25;

}

//Save document

doc.Save(@“C:\Temp\out.doc”);

[VB]

'Open document and create DocumentBuilder

Dim doc As Document = New Document(“C:\Temp\in.doc”)

'Get first table from the document

Dim tab As Table = doc.FirstSection.Body.Tables(0)

'Set top and bottom borders

tab.FirstRow.RowFormat.Borders(BorderType.Top).LineStyle = LineStyle.Single

tab.FirstRow.RowFormat.Borders(BorderType.Top).LineWidth = 2.25

tab.LastRow.RowFormat.Borders(BorderType.Bottom).LineStyle = LineStyle.Single

tab.LastRow.RowFormat.Borders(BorderType.Bottom).LineWidth = 2.25

'Set fight and left borders

For Each row As Row In tab.Rows

row.FirstCell.CellFormat.Borders(BorderType.Left).LineStyle = LineStyle.Single

row.FirstCell.CellFormat.Borders(BorderType.Left).LineWidth = 2.25

row.LastCell.CellFormat.Borders(BorderType.Right).LineStyle = LineStyle.Single

row.LastCell.CellFormat.Borders(BorderType.Right).LineWidth = 2.25

Next

'Save document

doc.Save(“C:\Temp\out.doc”)

  1. Code is similar to the first example

[C#]

//Open document and create DocumentBuilder

Document doc = new Document(@“C:\Temp\in.doc”);

DocumentBuilder builder = new DocumentBuilder(doc);

//Get first table from the document

Table tab = doc.FirstSection.Body.Tables[0];

int rowIdx = 1;

int cellIdx = 4;

//Remove value from the cell

tab.Rows[rowIdx].Cells[cellIdx].RemoveAllChildren();

tab.Rows[rowIdx].Cells[cellIdx].EnsureMinimum();

//Move documentBuilder curcor to the cell

builder.MoveTo(tab.Rows[rowIdx].Cells[cellIdx].FirstParagraph);

builder.Font.Bold = true;

builder.Write(“lable:”);

builder.Font.Bold = false;

builder.Write(“value”);

//Save document

doc.Save(@“C:\Temp\out.doc”);

[VB]

'Open document and create DocumentBuilder

Dim doc As Document = New Document(“C:\Temp\in.doc”)

Dim builder As DocumentBuilder = New DocumentBuilder(doc)

'Get first table from the document

Dim tab As Table = doc.FirstSection.Body.Tables(0)

Dim rowIdx As Integer = 1

Dim cellIdx As Integer = 4

'Remove value from the cell

tab.Rows(rowIdx).Cells(cellIdx).RemoveAllChildren()

tab.Rows(rowIdx).Cells(cellIdx).EnsureMinimum()

'Move documentBuilder curcor to the cell

builder.MoveTo(tab.Rows(rowIdx).Cells(cellIdx).FirstParagraph)

builder.Font.Bold = True

builder.Write(“lable:”)

builder.Font.Bold = False

builder.Write(“value”)

'Save document

doc.Save(“C:\Temp\out.doc”)

  1. The best way to create table using Aspose.Words is using DocumentBuilder:

[C#]

//Open document and create DocumentBuilder

Document doc = new Document(@“C:\Temp\in.doc”);

DocumentBuilder builder = new DocumentBuilder(doc);

//build table

Table tab = builder.StartTable();

for (int i = 0; i < 5; i++)

{

for (int j = 0; j < 5; j++)

{

builder.InsertCell();

//Insert some value

builder.Write(“test”);

}

builder.EndRow();

}

builder.EndTable();

//Save document

doc.Save(@“C:\Temp\out.doc”);

[VB]

'Open document and create DocumentBuilder

Dim doc As Document = New Document(“C:\Temp\in.doc”)

Dim builder As DocumentBuilder = New DocumentBuilder(doc)

'build table

Dim tab As Table = builder.StartTable()

For i As Integer = 0 To 4

For j As Integer = 0 To 4

builder.InsertCell()

'Insert some value

builder.Write(“test”)

Next

builder.EndRow()

Next

builder.EndTable()

'Save document

doc.Save(“C:\Temp\out.doc”)

Please let me know in case of any issues, I will be glad to help you.

Best regards.

I really appreciate your quick help.

I am having compilation issue with this statement.

builder.Write("#<%=FlightNo%> Flight/Stage Affectivity (<%=FlightLaunchDate%>) - Status as of <%=myDate%>");

The value for FlightNo and FlightLaunchDate is coming from querying the database. Somehow it thinks It should have matching double quote.

Thank you for your help

I am having compilation issue with this statement also.

Dim tab As Table = Doc.FirstSection.Body.Tables(0)

‘Table’ is ambigious.

Thank you

Hi

Thanks for your inquiry.

  1. I think you can build the string before inserting it into the document. I suppose using String.Format is the best way to achieve this. See the following code:

string FlightNo = “some value”;

string FlightLaunchDate = “10.10.2008”;

string myDate = “10.14.2008”;

string value = String.Format("#{0} Flight/Stage Affectivity ({1}) - Status as of {2}", FlightNo, FlightLaunchDate, myDate);

  1. You should just use Aspose.Words.Tables.Table instead Table

Hope this could help you.

Best regards.

Thank you for your help. How can I convert this code to Aspose format.

with flightsTable.cell(rowcount, colcount).range

with .characters(1)

.text = " " & val

.font.bold = 0

end with

with .characters(1)

.text = label & “:”

.font.bold = 1

end with

end with

Thank you

Hi

Thanks for your inquiry. I translated the similar code yesterday. Please see item 3 in my previous post.

Best regards.

Thank you for your help. I applied # 3) code in my file. The compilation is complete but I am getting run time error. Actuly I am very new to VB also and it is hard to understand what the code is trying to do. I know it is creating rows and I guess splitting at the column 3 and 5.

sub setup_rows

dim i

dim arow

with flightsTable

for i = 1 to 5

.rows.add

next

.rows(.rows.count - 4).borders(-1).linewidth = 24

.cell(.rows.count - 4, 1).split 1,5

.cell(.rows.count - 1, 1).split 1,3

end with

end sub 'setup_rows

And also should I define this before I get into the main loop?

flightsTable.Rows(rowcount).Cells(colcount).RemoveAllChildren()

flightsTable.Rows(rowcount).Cells(colcount).EnsureMinimum()

builder.MoveTo(flightsTable.Rows(rowcount).Cells(colcount).FirstParagraph)

Thank you

Hi

Thanks for your inquiry. If you need to add rows to an existing table then I think you should clone existing row and add it at the end of table as shown in the following code:

'Get last row from the table

Dim refRow As Row = myTable.LastRow

'Clone and insert rows after ref row

For i As Integer To 10

'Clone row

Dim clone As Row = CType(row.Clone(True), Row)

'Inser clone of row

myTable.Rows.Add(clone)

Next

Hope this helps.

Best regards.

The code does not seem to be working. May be because initially I have an empty table. I have to add 5 rows to the empty table. I have to create 5 rows and split first row into 5 columns and the 4th row into three colums and then create border/line at the bottom of these five rows.

The clone code does not work in this case I guess because the table is empty initially. Could you please provide assistance? My original code is this.

sub setup_rows

dim i

dim arow

with flightsTable

for i = 1 to 5

.rows.add

next

.rows(.rows.count - 4).borders(-1).linewidth = 24

.cell(.rows.count - 4, 1).split 1,5

.cell(.rows.count - 1, 1).split 1,3

end with

end sub 'setup_rows

Thanks,

Hi

Thanks for your request. Please try using the following code:

Sub Main()

'Open document

Dim doc As Document = New Document(“C:\Temp\in.doc”)

'Get table from the document

Dim tab As Table = doc.FirstSection.Body.Tables(0)

Dim refRow As Row = tab.LastRow

'Add 5 rows

For i As Integer = 1 To 5

tab.Rows.Add(refRow.Clone(True))

Next

'Set bottom border of the ref row

For Each cell As Cell In refRow

cell.CellFormat.Borders(BorderType.Bottom).LineWidth = 3

Next

'There is no method to split cells in Aspose.Words.

'But we can just add cells to the row and calculate width of each cell

SplitCell(tab.Rows(tab.Rows.Count - 5).FirstCell, 5)

SplitCell(tab.Rows(tab.Rows.Count - 2).FirstCell, 3)

doc.Save(“C:\Temp\out_aw.doc”)

End Sub

Sub SplitCell(ByVal cell As Cell, ByVal splitCount As Integer)

cell.CellFormat.Width = cell.CellFormat.Width / splitCount

For i As Integer = 1 To splitCount - 1

cell.ParentRow.InsertAfter(cell.Clone(True), cell)

Next

End Sub

Please let me know in case of any issues, I will be glad to help you.

Best regards.

My coade breaks here. It gives me exception error.

I have template like this. I am getting values from the database and tryong to insert in each cell by using the following code.

builder.MoveToCell(0, rowcount, colcount, -1)

builder.Write(val)

When cursor reaches to the 3rd coloumn of the first row it throws and exception. The column count is 3 at that point. Why is this happening?

The error is 'Specified argument was out of the range of valid values. Parameter name: columnIndex

builder.MoveToCell(0, rowcount, colcount, -1)

builder.Write(value)

Hi

Thanks for your request. I think this occurs because the last row contains only one cell. Please try using the following code:

//Get first table from current section

Table tab = builder.CurrentSection.Body.Tables[0];

//move cursor to the last right cell in the table

builder.MoveToCell(0, tab.Rows.Count - 1, tab.LastRow.Cells.Count - 1, -1);

//Insert value

builder.Write(“My value”);

Hope this helps.

Best regards,