How to convert this VB code into C#? [Resolved]

Posted by Gudevg under DotNetFunda.Com on 5/3/2012 | Points: 10 | Views : 1743 | Status : [Member] | Replies : 1
Hi everyone,
I develop the below code to fetch figures from word document and convert it into Image format. I develop this in VB, I tried to convert it into C#.

First of all i tried to convert it into VB.Net using Upgrade VB using Visual Studio but it shows some errors and it is not convert it into VB.net format Please anyone look into this code and help me in this regards,


Sub ImageToIncludePicture()
Dim oShp As InlineShape

If ActiveDocument.InlineShapes.Count <> 0 Then
Dim oFSO As New Scripting.FileSystemObject

If oFSO.FolderExists("c:\temp\epub") = False Then
If oFSO.FolderExists("c:\temp") = False Then
oFSO.CreateFolder ("c:\temp")
End If
oFSO.CreateFolder ("c:\temp\epub")
End If

Dim strImgPath As String
strImgPath = ActiveDocument.Path & "\images\"
If oFSO.FolderExists(strImgPath) = False Then
oFSO.CreateFolder (strImgPath)
End If

Dim oTmp As Template
Dim sPath As String

Dim sToolPath As String
sToolPath = "D:\00_Projects\ePub\Templates\text2img.exe"

img = 1
For Each oShp In ActiveDocument.InlineShapes
oShp.Select
If Selection.Fields.Count = 0 Then
If Selection.Paragraphs(1).Range.Text <> "" Then
Selection.Paragraphs.Style = "Normal"
End If
Dim strImg As String
Dim strExt As String
strImg = oShp.Range.XML
Dim oReg As New VBScript_RegExp_55.RegExp
Dim oMat As Match
Dim oMatC As MatchCollection
strImg = Replace(strImg, ChrW(13), "<br/>")
strImg = Replace(strImg, ChrW(10), "<br/>")
oReg.Pattern = "<w:binData w:name=""wordml://(.*?)"">(.*?)</w:binData>"
oReg.Global = True
oReg.IgnoreCase = True
oReg.Multiline = True
If oReg.test(strImg) = True Then
Set oMatC = oReg.Execute(strImg)
Set oMat = oMatC.Item(0)
Dim strTest As String
strTest = oMat.SubMatches(1)
strTest = Replace(oMat.SubMatches(1), "<br/><br/>", vbLf)

Open "c:\temp\ePub\input.txt" For Output As #1
Print #1, strTest
Close #1
strExt = oMat.SubMatches(0)
Selection.Delete
strExt = Right(strExt, 4)
Shell sToolPath & " ""c:\temp\epub\input.txt"" """ & strImgPath & "\image" & Format(img, "00") & strExt & """", vbHide
While (oFSO.FileExists(strImgPath & "\image" & Format(img, "00") & strExt) = False)

Wend
Selection.Fields.Add Selection.Range, wdFieldIncludePicture, """images/image" & Format(img, "00") & strExt & """"
img = img + 1
End If
End If
Next
End If
End Sub


Thanks in advance.
Deepak G.

Deepakkumar G.


Responses

Posted by: Oswaldlily on: 5/3/2012 [Member] Starter | Points: 50

Up
0
Down

Resolved
void ImageToIncludePicture() {
InlineShape oShp;
if ((ActiveDocument.InlineShapes.Count != 0)) {
Scripting.FileSystemObject oFSO = new Scripting.FileSystemObject();
if ((oFSO.FolderExists("c:\\temp\\epub") == false)) {
if ((oFSO.FolderExists("c:\\temp") == false)) {
oFSO.CreateFolder("c:\\temp");
}
oFSO.CreateFolder("c:\\temp\\epub");
}
string strImgPath;
strImgPath = (ActiveDocument.Path + "\\images\\");
if ((oFSO.FolderExists(strImgPath) == false)) {
oFSO.CreateFolder(strImgPath);
}
Template oTmp;
string sPath;
string sToolPath;
sToolPath = "D:\\00_Projects\\ePub\\Templates\\text2img.exe";
img = 1;
foreach (oShp in ActiveDocument.InlineShapes) {
oShp.Select;
if ((Selection.Fields.Count == 0)) {
if ((Selection.Paragraphs(1).Range.Text != "")) {
Selection.Paragraphs.Style = "Normal";
}
string strImg;
string strExt;
strImg = oShp.Range.XML;
VBScript_RegExp_55.RegExp oReg = new VBScript_RegExp_55.RegExp();
Match oMat;
MatchCollection oMatC;
strImg = strImg.Replace('\r', "<br/>");
strImg = strImg.Replace('\n', "<br/>");
oReg.Pattern = "<w:binData w:name=\"wordml://(.*?)\">(.*?)</w:binData>";
oReg.Global = true;
oReg.IgnoreCase = true;
oReg.Multiline = true;
if ((oReg.test(strImg) == true)) {
oMatC = oReg.Execute(strImg);
oMat = oMatC.Item[0];
string strTest;
strTest = oMat.SubMatches(1);
strTest = oMat.SubMatches(1).Replace("<br/><br/>", "\n");
Open;
"c:\\temp\\ePub\\input.txt";
for (object Output; ; Output++) {
// TODO: # ... Warning!!! not translated
1;
Print;
// TODO: # ... Warning!!! not translated
1;
strTest;
Close;
// TODO: # ... Warning!!! not translated
1;
strExt = oMat.SubMatches(0);
Selection.Delete;
strExt = strExt.Substring((strExt.Length - 4));
Shell;
(sToolPath + (" \"c:\\temp\\epub\\input.txt\" \""
+ (strImgPath + ("\\image"
+ (Format(img, "00")
+ (strExt + "\""))))));
vbHide;
while ((oFSO.FileExists((strImgPath + ("\\image"
+ (Format(img, "00") + strExt)))) == false)) {
Selection.Fields.Add;
Selection.Range;
wdFieldIncludePicture;
("\"images/image"
+ (Format(img, "00")
+ (strExt + "\"")));
img = (img + 1);
}
}
}
}
}
}
}

Gudevg, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response