Converting MS Word API to Aspose Word API

Hi,
Could you please provide code for converting the following VB code using MS Word to VB.NET using Aspose Word?

'Old Code using Microsoft Word - Starts here.
objWord.Selection.Font.Bold = True
objWord.Selection.Font.Name = "Arial"
objWord.Selection.Font.Size = CSng("12")
objWord.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter
objWord.Selection.TypeText(Text:=headerLabel)
objWord.Selection.InsertParagraph()
objWord.Selection.InsertParagraph()
'KK - This to be converted:
objWord.Selection.Collapse(Direction:=Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd)
' Add picture to document (new tree)
If Action <> "delete" Then
aInLine = objWord.Selection.InlineShapes.AddPicture(fileName:=newTreePath, LinkToFile:=False, SaveWithDocument:=True)
If aInLine.Height > maxGifHeight Then
aInLine.Width = maxGifHeight / aInLine.Height * aInLine.Width
aInLine.Height = maxGifHeight
End If
aInLine = Nothing
End If
' Insert page break if necessary
If Action <> "add" Then
objWord.Selection.InsertBreak(Type:=Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak)
Else
If GIFPageCount > 1 And PageCount <> GIFPageCount Then
objWord.Selection.InsertBreak(Type:=Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak)
End If
End If
End If
'Old Code using Microsoft Word - ends here.

Hi
Thanks for you request. There is no analog of Selection in Aspose.Words. So to translate your code i need also your document and code where you define Selection. Please provide me this information and I will help you to translate this code.
Best regards.

Hi,
Thanks for your response. The purpose of this function which I have copied below in its entirety is to transfer a Visio document into a new Word document. So it creates a blank Word document and then opens the Visio document and goes page by page and copies each page of the Visio document into a new page of Word document. Also in addition we need to be able to insert images in the Word document. I don’t think we need to follow exactly how Word was coded in terms of “Selection” property which is not in Aspose. All we need to do is to create a section and / or paragraph in a new page and then give it its properties and then insert the image into the section. The following code shows the Word (old) version mixed with Aspose (new) code. One thing I could’t find was the COLLAPSE property.

Public Function CreateVisioDocs(ByVal fName As String, ByVal fNameCopyGIF As String, ByVal fNameProductionGIF As String, ByVal fType As String) As Boolean
' Create Word document for Visio (links or archives or PrintUser directory)
' Input: fName = file name for Word files (includes DOC file extension);
' fNameCopyGIF = file name for GIF copy file (without file extension);
' fNameProductionGIF = file name for GIF production file (without file extension);
' fType = link or archive or print
Dim returnCheck As Boolean
Dim myUtilObj As Object
Dim fTemp As String
Dim newTreePath As String
Dim oldTreePath As String
Dim fNameLength As Short
Dim fPath As String
Dim archiveFolder As String
Dim footerLabel As String
Dim headerLabel As String
Dim startLocation As Short
Dim endLocation As Short
Dim fNameLengthGIF As Short
Dim change As String
Dim changeLength As Short
Dim control_type As String
Dim control_number As String
'Dim aInLine As Microsoft.Office.Interop.Word.InlineShape
Dim aInLine As Aspose.Words.Inline
Dim GIFPageCount As Short
Dim PageCount As Short
Const maxGifHeight As Integer = 396 'original Height = 5.5" = 5.5*72=396points
If (pwFlag = "Yes") Or InStr(1, fName, "AT", 1) Then
companyCode = "pw"
Else
companyCode = "rd"
End If
On Error GoTo errorFlag
' Get action to be taken on document
If fType <> "print" Then
Action = GetAction(fName)
End If
' Create Utility object for file manipulation
myUtilObj = CreateObject("Utility.File")
myUtilObj.debugFlag = debugOn
myUtilObj.runFlag = testFlag
'' Create Word object
''returnCheck = LoadWord
'' Open new Word document
'objDoc = objWord.Documents.Add
'' Set page layout to landscape
'With objDoc.PageSetup
' .Orientation = Microsoft.Office.Interop.Word.WdOrientation.wdOrientLandscape
' .TopMargin = objWord.InchesToPoints(1)
' .BottomMargin = objWord.InchesToPoints(1)
' .LeftMargin = objWord.InchesToPoints(1)
' .RightMargin = objWord.InchesToPoints(1)
' .HeaderDistance = objWord.InchesToPoints(0.5)
' .FooterDistance = objWord.InchesToPoints(0.5)
'End With
' Create a blank Aspose.Word document. New code replacing above code
Dim doc As Aspose.Words.Document = New Aspose.Words.Document()
'Remove empty section in the beginning of every blank document.
doc.FirstSection.Remove()
Dim builder As DocumentBuilder = New DocumentBuilder(doc)
' Set page layout to landscape
With builder.PageSetup
.Orientation = Orientation.Landscape
.TopMargin = ConvertUtil.InchToPoint(1)
.BottomMargin = ConvertUtil.InchToPoint(1)
.LeftMargin = ConvertUtil.InchToPoint(1)
.RightMargin = ConvertUtil.InchToPoint(1)
.HeaderDistance = ConvertUtil.InchToPoint(0.5)
.FooterDistance = ConvertUtil.InchToPoint(0.5)
End With
' Path to save files
' Save directly to the links or archives or printuser directory
If fType = "link" Then
fPath = rootDir & companyCode & "\links\ha\tree\" & fName
ElseIf fType = "archive" Then
fPath = rootDir & companyCode & "\archives\ha\tree\" & fName
ElseIf fType = "print" Then
fPath = rootDir & "rd\ccs\fc\docs\printuser\" & fName
End If
' Save empty document
'objDoc.SaveAs(fileName:=fPath, FileFormat:=Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument)
doc.Save(fPath, SaveFormat.Doc)
' Turn off virus protection.
' Khosrow Khoramian - "Options" is a MS Word Application level feature and does not apply to Aspose Word which
' only operates at Document level. So these settings are commented.
' 
'With objWord.Options
' .Pagination = True
' .WPHelp = False
' .WPDocNavKeys = False
' .ShortMenuNames = False
' .RTFInClipboard = True
' .BlueScreen = False
' .EnableSound = False
' .ConfirmConversions = False
' .UpdateLinksAtOpen = True
' .SendMailAttach = True
' .MeasurementUnit = Microsoft.Office.Interop.Word.WdMeasurementUnits.wdInches
' .AnimateScreenMovements = False
' .VirusProtection = False
'End With
' Get admin's docs directory
archiveFolder = AdminDirectory()
' Get number of pages for Visio
If fType = "print" Or fType = "archive" Or Action = "delete" Then
GIFPageCount = GetGifPageCount(fNameProductionGIF, "print")
Else
GIFPageCount = GetGifPageCount(fNameCopyGIF, archiveFolder)
End If
' Loop through all pages
For PageCount = 1 To GIFPageCount
' Get file paths for old and new GIF files
If PageCount = 1 Then
newTreePath = rootDir & "rd\ccs\fc\docs\" & archiveFolder & "\" & fNameCopyGIF & ".GIF"
oldTreePath = rootDir & companyCode & "\ha\ft_gif\" & fNameProductionGIF & ".GIF"
Else
newTreePath = rootDir & "rd\ccs\fc\docs\" & archiveFolder & "\" & fNameCopyGIF & PageCount & ".GIF"
oldTreePath = rootDir & companyCode & "\ha\ft_gif\" & fNameProductionGIF & PageCount & ".GIF"
End If
' ***************** IS SECTION ****************************
' Add new version of file for shadow file
If fType = "link" And Action <> "delete" Then
' Add label
If Action = "edit" Then
headerLabel = "IS"
Else
headerLabel = ""
End If
'=============================================================
'New Code using Aspose Word - Starts here.
builder.MoveToSection(0)
builder.MoveToDocumentStart()
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center
builder.Font.Bold = True
builder.Font.Name = "Arial"
builder.Font.Size = CSng("12")
'builder.Font.Color = Color.Black
builder.Write(text:=headerLabel)
builder.InsertParagraph()
builder.InsertParagraph()
builder.InsertImage(fileName:=newTreePath)
'New Code using Aspose Word - Ends here.
'=============================================================
'=============================================================
'Old Code using Microsoft Word - Starts here.
objWord.Selection.Font.Bold = True
objWord.Selection.Font.Name = "Arial"
objWord.Selection.Font.Size = CSng("12")
objWord.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter
objWord.Selection.TypeText(Text:=headerLabel)
objWord.Selection.InsertParagraph()
objWord.Selection.InsertParagraph()
'KK - This to be converted:
objWord.Selection.Collapse(Direction:=Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd)
' Add picture to document (new tree)
If Action <> "delete" Then
aInLine = objWord.Selection.InlineShapes.AddPicture(fileName:=newTreePath, LinkToFile:=False, SaveWithDocument:=True)
If aInLine.Height > maxGifHeight Then
aInLine.Width = maxGifHeight / aInLine.Height * aInLine.Width
aInLine.Height = maxGifHeight
End If
aInLine = Nothing
End If
' Insert page break if necessary
If Action <> "add" Then
objWord.Selection.InsertBreak(Type:=Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak)
Else
If GIFPageCount > 1 And PageCount <> GIFPageCount Then
objWord.Selection.InsertBreak(Type:=Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak)
End If
End If
End If
'Old Code using Microsoft Word - ends here.
'=============================================================
' ***************** WAS SECTION ****************************
' See if need to include old Visio
If (fType = "link" And Action <> "add") Or (fType = "archive" And Action <> "add") Or fType = "print" Then
' Add label for old GIF file
If fType <> "print" Then
If Action = "edit" Then
headerLabel = "WAS"
Else
headerLabel = ""
End If
objWord.Selection.Font.Bold = True
objWord.Selection.Font.Name = "Arial"
objWord.Selection.Font.Size = CSng("12")
objWord.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter
objWord.Selection.TypeText(Text:=headerLabel)
objWord.Selection.InsertParagraph()
objWord.Selection.InsertParagraph()
objWord.Selection.Collapse(Direction:=Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd)
End If
' Insert old GIF file on new page
aInLine = objWord.Selection.InlineShapes.AddPicture(fileName:=oldTreePath, LinkToFile:=False, SaveWithDocument:=True)
If aInLine.Height > maxGifHeight Then
aInLine.Width = maxGifHeight / aInLine.Height * aInLine.Width
aInLine.Height = maxGifHeight
End If
aInLine = Nothing
' Insert page break, if necessary (more pages)
If GIFPageCount > 1 And PageCount <> GIFPageCount Then
'objWord.Selection.InsertBreak(Type:=Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak)
builder.InsertBreak(BreakType.PageBreak)
End If
End If ' Loop for ftype and action for "WAS" file
Next 'PageCount
' Get control_type and control_number for header
' Turn on virus protection
' Khosrow Khoramian - "Options" is a MS Word Application level feature and does not apply to Aspose Word which
' only operates at Document level. So these settings are commented.
' 
'With objWord.Options
' .Pagination = True
' .WPHelp = False
' .WPDocNavKeys = False
' .ShortMenuNames = False
' .RTFInClipboard = True
' .BlueScreen = False
' .EnableSound = False
' .ConfirmConversions = False
' .UpdateLinksAtOpen = True
' .SendMailAttach = True
' .MeasurementUnit = Microsoft.Office.Interop.Word.WdMeasurementUnits.wdInches
' .AnimateScreenMovements = False
' .VirusProtection = True
'End With
' Turn on revision tracking (visible on screen)
'objWord.ActiveDocument.TrackRevisions = True
'objWord.ActiveDocument.ShowRevisions = True
With doc
.TrackRevisions = True
End With
' Save Word file
'objDoc.Save()
doc.Save(fPath, SaveFormat.Doc)
' Close Word
'objDoc.Close(Microsoft.Office.Interop.Word.WdSaveOptions.wdSaveChanges)
'objDoc = Nothing
doc = Nothing
returnCheck = RemoveWord
' Delete temporary file
fTemp = "C:\Temp\" & fName
returnCheck = myUtilObj.DeleteFile(fTemp)
' Cleanup
myUtilObj = Nothing
' Normal exit
CreateVisioDocs = True
Exit Function
' Error handling
errorFlag:
errorLog(Err.Description, Err.Number, "CreateVisioDocs")
Message = "Error in CreateVisioDocs: " & Err.Description & ", Error number:" & Err.Number
userWarning()
returnCheck = RemoveWord
CreateVisioDocs = False
myUtilObj = Nothing
End Function

Hi
Thank you for additional information. Please try using the following code snippet.

'Old Code using Microsoft Word - Starts here.
builder.Font.Bold = True
builder.Font.Name = "Arial"
builder.Font.Size = 12
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center
builder.Writeln()
builder.Writeln()
'KK - This to be converted:
' Add picture to document (new tree)
If Action <> "delete" Then
Dim aInLine As Shape = builder.InsertImage(newTreePath)
If aInLine.Height > maxGifHeight Then
aInLine.Width = maxGifHeight / aInLine.Height * aInLine.Width
aInLine.Height = maxGifHeight
End If
aInLine = Nothing
End If
' Insert page break if necessary
If Action <> "add" Then
builder.InsertBreak(BreakType.PageBreak)
Else
If GIFPageCount > 1 And PageCount <> GIFPageCount Then
builder.InsertBreak(BreakType.PageBreak)
End If
End If

Hope this helps.
Best regards.

Thank you for your response. I will give it try momentarily. However, I did not see any code for the following (COLLAPSE). Can you please convert this code to Aspose too?

'KK - This to be converted:
objWord.Selection.Collapse(Direction:=Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd)

Thanks.

Hi
Thanks for your request. Collapse just moves cursor to the end of selection. I told you earlier that there is no analog of Selection on Aspose.Words. So I think you can just use current position of DocumentBuilder cursor.
Best regards.

Alexey, thanks for the new info.