Open PDF, save DOCX bugs out after a few dozen documents and outputs garbled/corrupted files - vba

I have a few thousand PDF files that I needs to convert to DOCX. I wrote the following macro:
Sub convertPDFtoDOCX()
'
' convertPDFtoDOCX Macro
'
'
Dim docDirectory As String
Dim pdfDirectory As String
Dim docPath As String
Dim doc As Document
docDirectory = "C:\Users\<USER>\DOCX\"
pdfDirectory = "C:\Users\<USER>\PDF\"
pdfFile = Dir(pdfDirectory & "*.*")
Do While pdfFile <> ""
docPath = docDirectory & pdfFile & ".docx"
Set doc = Documents.Open(FileName:=pdfDirectory & pdfFile)
ActiveDocument.SaveAs2 FileName:=docPath, FileFormat:=wdFormatXMLDocument
Documents.Close
pdfFile = Dir
Loop
End Sub
It works fine for the first few dozen documents, but then starts outputting "corrupted files", that aren't docx and can't be opened with a PDF viewer either. There is no error message when it starts bugging out. The problem doesn't come from the PDF files, since if I stop the macro and start it again on the same documents, they are correctly converted the second time.
"Corrupted" files looks like this:
%PDF-1.5
%µµµµ
1 0 obj
<</Type/Catalog/Pages 2 0 R/Lang(fr-FR) /StructTreeRoot 91 0 R/MarkInfo<</Marked true>>>>
endobj
2 0 obj
<</Type/Pages/Count 21/Kids[ 3 0 R 27 0 R 31 0 R 42 0 R 44 0 R 46 0 R 48 0 R 55 0 R 59 0 R 61 0 R 63 0 R 65 0 R 67 0 R 69 0 R 71 0 R 73 0 R 75 0 R 77 0 R 79 0 R 81 0 R 88 0 R] >>
endobj
3 0 obj
<</Type/Page/Parent 2 0 R/Resources<</Font<</F1 5 0 R/F2 9 0 R/F3 11 0 R/F4 16 0 R/F5 18 0 R/F6 20 0 R/F7 25 0 R>>/ExtGState<</GS7 7 0 R/GS8 8 0 R>>/ProcSet[/PDF/Text/ImageB/ImageC/ImageI] >>/MediaBox[ 0 0 595.2 841.8] /Contents 4 0 R/Group<</Type/Group/S/Transparency/CS/DeviceRGB>>/Tabs/S/StructParents 0>>
endobj
4 0 obj
<</Filter/FlateDecode/Length 4428>>
stream
xœ­\Ën7Ýð?Ô.Ý ¨Ä7«‚ ¹%e4ð+²’Y$Yt¤¶£A,9RÛÈüÕ|Æ|ÆìÙäæ^²ÈzðQ-¦ È]U¼$//:<yØÞ¾__o«££Ã“ív}ýóæ¦úþðÅýv{ÿñÇë}Ú¾]¸½[ooïï
What causes the issue and how can I fix it?
I use Word 2016 on Windows 10.

I don't think you can fix the issue without a patch from Microsoft. Meanwhile, you can move your code to run outside Word and create a new Word.Application object for each iteration.

Related

Coordinates extracted from PDF are not exact

I'm working on rendering a georeferenced pdf within a map, I was able to retrieve the geolocation information from the pdf, but the coordinates I receive are not correct, they are a few meters apart from the places they really should be.
Opening the same PDF in Avenza Maps, it indicates this list of coordinates, and these are correct:
[-26.413082, -51.561534, -26.435838, -51.561643, -26.435909, -51.543773,-26.413152, -51.543667]
In the format I'm doing (reading the PDF as a String and doing a RegEx) I get these values:
[-26.43302 -51.56133 -26.41418 -51.56124 -26.41424 -51.54409 -26.43309 -51.54418]
[-26.45579 -51.59842 -26.41777 -51.59822 -26.41811 -51.51036 -26.45613 -51.51053]
But unfortunately none of the two reflect in the correct place (as in avenza).
That said, I opened the PDF in Notepad and found other values (more related to conversion and information), and I believe that maybe there is some way to convert the coordinates that I got through this other information, to the correct coordinates.
Follow the informations:
<?xpacket end="w"?>
endstream
endobj
294 0 obj
3495
endobj
295 0 obj
/DeviceRGB
endobj
296 0 obj
<</Length 297 0 R>>stream
/GS_init gs
/Group_6 Do
endstream
endobj
297 0 obj
24
endobj
298 0 obj
<</ExtGState 2 0 R/ColorSpace << /CS_P 295 0 R >>/XObject << /Group_6 6 0 R >>>>endobj
299 0 obj
<</Type /Group/S /Transparency/CS 295 0 R/I false/K false>>endobj
300 0 obj
<</Type /Page/Parent 301 0 R/Contents 296 0 R/Resources 298 0 R/MediaBox [0 0 841.88808 1190.5488]/ArtBox [0 0 841.88808 1190.5488]/UserUnit 1/Group 299 0 R/VP[<</Type /Viewport/BBox [14.1732 147.400915455 822.0456 1133.350548016]/Name (þÿ T S B I I)/Measure<</Type /Measure/Subtype /GEO/Bounds [0 0 0 1 1 1 1 0 0 0]/GPTS [ -26.43302 -51.56133 -26.41418 -51.56124 -26.41424 -51.54409 -26.43309 -51.54418]/LPTS [ 0 0 0 1 1 1 1 0]/GCS<</Type /PROJCS/WKT (PROJCS["SIRGAS_2000_UTM_Zone_22S",GEOGCS["GCS_SIRGAS_2000",DATUM["D_SIRGAS_2000",SPHEROID["GRS_1980",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["False_Easting",500000.0],PARAMETER["False_Northing",10000000.0],PARAMETER["Central_Meridian",-51.0],PARAMETER["Scale_Factor",0.9996],PARAMETER["Latitude_Of_Origin",0.0],UNIT["Meter",1.0]])>>>>>><</Type /Viewport/BBox [14.1732 14.1732 239.961243463 122.688692878]/Name (þÿ R e f e r e n c i a _ M a p a)/Measure<</Type /Measure/Subtype /GEO/Bounds [0 0 0 1 1 1 1 0 0 0]/GPTS [ -26.45579 -51.59842 -26.41777 -51.59822 -26.41811 -51.51036 -26.45613 -51.51053]/LPTS [ 0 0 0 1 1 1 1 0]/GCS<</Type /PROJCS/WKT (PROJCS["SIRGAS_2000_UTM_Zone_22S",GEOGCS["GCS_SIRGAS_2000",DATUM["D_SIRGAS_2000",SPHEROID["GRS_1980",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["False_Easting",500000.0],PARAMETER["False_Northing",10000000.0],PARAMETER["Central_Meridian",-51.0],PARAMETER["Scale_Factor",0.9996],PARAMETER["Latitude_Of_Origin",0.0],UNIT["Meter",1.0]])>>>>>>]>>endobj
301 0 obj
<</Type /Pages/Kids [ 300 0 R ]/Count 1>>endobj
302 0 obj
<<>>endobj
303 0 obj
<</Type /Catalog/Pages 301 0 R/PageMode /UseNone/PageLayout /SinglePage/ViewerPreferences <</PrintScaling /None /FitWindow true /DisplayDocTitle true>>/OpenAction [300 0 R /Fit]/OCProperties<</OCGs [ 10 0 R 11 0 R 12 0 R 13 0 R 14 0 R 15 0 R 16 0 R 17 0 R 18 0 R 19 0 R 20 0 R 21 0 R 22 0 R 35 0 R 36 0 R 43 0 R 44 0 R 47 0 R 50 0 R 53 0 R 56 0 R 59 0 R 62 0 R 63 0 R 64 0 R 65 0 R 66 0 R 67 0 R 68 0 R 69 0 R 76 0 R 77 0 R 80 0 R 83 0 R 90 0 R 93 0 R 96 0 R 99 0 R 102 0 R 105 0 R 108 0 R 111 0 R 114 0 R 117 0 R 120 0 R 123 0 R 126 0 R 129 0 R 132 0 R 135 0 R 138 0 R 141 0 R 148 0 R 149 0 R 152 0 R 155 0 R 158 0 R 161 0 R 176 0 R ]/D<</Name (Layers Tree)/Order [ 176 0 R 161 0 R 158 0 R 148 0 R [ 155 0 R 152 0 R 149 0 R ] 141 0 R 138 0 R 135 0 R 132 0 R 129 0 R 126 0 R 123 0 R 120 0 R 117 0 R 114 0 R 111 0 R 108 0 R 105 0 R 102 0 R 99 0 R 96 0 R 93 0 R 90 0 R 83 0 R 80 0 R 62 0 R [ 76 0 R [ 77 0 R ] 63 0 R [ 69 0 R 68 0 R 67 0 R 64 0 R [ 66 0 R 65 0 R ] ] ] 10 0 R [ 59 0 R 43 0 R [ 56 0 R 53 0 R 50 0 R 47 0 R 44 0 R ] 11 0 R [ 36 0 R 35 0 R 22 0 R 21 0 R 12 0 R [ 20 0 R 19 0 R 18 0 R 17 0 R 16 0 R 15 0 R 14 0 R 13 0 R ] ] ] ]/ListMode /VisiblePages>>>>/Metadata 293 0 R>>endobj
304 0 obj
<</Type/XRef/Size 305/W[1 4 2]/Filter/FlateDecode/Info 292 0 R/Root 303 0 R/ID [<c9167b70223726438d277b1b4409c053> <c9167b70223726438d277b1b4409c053>]/Length 923>>stream
I needed someone to tell me some way to get the correct coordinates, I hope this information helps to find
The PDF content in your question includes two ViewPort dictionaries.
These dictionaries map a location on the page ("BBox")
onto the GPTS referencing the specified WKT.
This is covered in the PDF 2.0 reference ISO-32000-2 section 12.9 & 12.10.
Unfortunately, this spec is not freely available, and it's not cheap.
Here are some definitions from the spec:
BBox:
A rectangle in default user space coordinates specifying the location of the viewport on the page.
The two coordinate pairs of the rectangle shall be specified in normalised form; that is, lower-left followed by upper-right, relative to the measuring coordinate system. This ordering shall determine the orientation of the measuring coordinate system (that is, the direction of the positive x and y axes) in this viewport, which may have a different rotation from the page.
GPTS:
(Required; PDF 2.0) An array of numbers that shall be taken pairwise, defining points in geographic space as degrees of latitude and longitude, respectively when defining a geographic coordinate system. These values shall be based on the geographic coordinate system described in the GCS dictionary. When defining a projected coordinate system, this array contains values in a planar projected coordinate space as eastings and northings. For Geospatial3D, when Geospatial feature information is present (requirement type Geospatial3D) in a 3D annotation, the GPTS array is required to hold 3D point coordinates as triples rather than pairwise where the third value of each tripe is an elevation value.
NOTE 2 Any projected coordinate system includes an underlying geographic coordinate system.
WKT:
A string of Well Known Text describing the geographic coordinate system.
The assumption is, if you're interested in Geospatial coordinates,
then you know what a WKT is, and what the projection means.
This may be enough information for you to map the geo coordinates for the
separate viewports to their locations on the page.
Here are the PDF Viewports in more readable form:
/VP [
<<
/Type
/Viewport
/BBox [14.1732 147.400915455 822.0456 1133.350548016]
/Name (TSBII)
/Measure <<
/Type
/Measure
/Subtype
/GEO
/Bounds [0 0 0 1 1 1 1 0 0 0]
/GPTS [ -26.43302 -51.56133 -26.41418 -51.56124
-26.41424 -51.54409 -26.43309 -51.54418]
/LPTS [ 0 0 0 1 1 1 1 0]
/GCS<<
/Type
/PROJCS
/WKT (
PROJCS["SIRGAS_2000_UTM_Zone_22S",
GEOGCS["GCS_SIRGAS_2000",
DATUM["D_SIRGAS_2000",SPHEROID["GRS_1980",6378137.0,298.257222101]],
PRIMEM["Greenwich",0.0],
UNIT["Degree",0.0174532925199433]
],
PROJECTION["Transverse_Mercator"],
PARAMETER["False_Easting",500000.0],
PARAMETER["False_Northing",10000000.0],
PARAMETER["Central_Meridian",-51.0],
PARAMETER["Scale_Factor",0.9996],
PARAMETER["Latitude_Of_Origin",0.0],
UNIT["Meter",1.0]
]
)
>>
>>
>>
<<
/Type
/Viewport
/BBox [14.1732 14.1732 239.961243463 122.688692878]
/Name (Referencia_Mapa)
/Measure <<
/Type
/Measure
/Subtype
/GEO
/Bounds [0 0 0 1 1 1 1 0 0 0]
/GPTS [ -26.45579 -51.59842 -26.41777 -51.59822
-26.41811 -51.51036 -26.45613 -51.51053]
/LPTS [ 0 0 0 1 1 1 1 0]
/GCS<<
/Type
/PROJCS
/WKT (
PROJCS["SIRGAS_2000_UTM_Zone_22S",
GEOGCS["GCS_SIRGAS_2000",
DATUM["D_SIRGAS_2000",SPHEROID["GRS_1980",6378137.0,298.257222101]],
PRIMEM["Greenwich",0.0],
UNIT["Degree",0.0174532925199433]
],
PROJECTION["Transverse_Mercator"],
PARAMETER["False_Easting",500000.0],
PARAMETER["False_Northing",10000000.0],
PARAMETER["Central_Meridian",-51.0],
PARAMETER["Scale_Factor",0.9996],
PARAMETER["Latitude_Of_Origin",0.0],
UNIT["Meter",1.0]])
>>
>>
>>
]
>>
Note that a PDF file is a structured document and not parsable as a string. These specific elements could be compressed, or might occur multiple times for different pages. You'll need a toolkit that can access Pages and Resources and Dictionaries in order to locate the ViewPorts.

How can I fix the damage in PDF files requested using MS2XML.XMLHTTP?

Friends, this is my first question here... I've been facing some problems when downloading a PDF buffer using MS2XML.XMLHTTP. I've been using Genexus to do so but I also tried right in pure Visual Fox Pro. The problem is that when I send the ResponseText to a string variable, some characters are replaced by question marks, the sam happens when I send the ResponseText to a pdf or txt file. The object created in MS2XML.XMLHTTP.6.0 does not allow using the ResponseBody property. Any thoughts on how could I solve it using MS2XML.XMLHTTP? Thanks.
oHTTP = CreateObject("MSXML2.XMLHTTP.6.0")
oHTTP.Open("GET", 'https://homologacao.plugboleto.com.br/api/v1/boletos/impressa /lote/NIKLfYBWz',.F.)
oHTTP.setRequestHeader("content-type", "application/pdf")
oHTTP.Send()
? oHTTP.responseText
I've received someething like the following (full of question marks):
%PDF-1.4 %??2 0 obj <</ColorSpace/DeviceRGB/Subtype/Image/Height 38/Filter/DCTDecode/Type/XObject/Width 149/BitsPerComponent 8/Length 2619>>stream ???JFIF H H ??C  !"$"$??C?? & ?" ?? ??? } !1AQa"q2???#B??R?$3br? %&'()456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz???????????????????????????????????????????????????? ??? w !1AQaq"2?B????#3R??$4?&'()56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz??????????????????????????????????????????????????? ? ?h???8OO????a&??G?3? ?p1???|b?o?? ??x?_???%??E?en9??T???T>.????JG??rx??????????h?w????????:?!?????????jlm?Tn???????u??? ??Ey?PA?? (?? (?? (?? (??>B???;.??3?e??J??~?F??? y,s??i???#?m=kw???? ?[?K#????vR#G??^$?????k?[??BSu??#???M??????? _??Z?Fo??????/??*x?¾ mn??{)???80??s]W?x? ??+??k??=????????8 ?|D?c?j???h???$?8???:c??(???M/?Ze??;O?[?J????? '?~/j~!???n?urm???1^ITl;?3?%[?b??~?&<=u?Y\x??W6?¬$2?q?1?;??qc??_??qk>?&?v?????,??F?{??x???s??????{?k????r8.??<P?|,????q?]I?]?e???p;??/?W?x??)???A?????&)??dc,?d7?J?s??m?>???????!??9Va?? c???Zv??x+?b?wd??f?8a????????,6????????x?? ?-????<9F???????[~$?{??o???X??????y?ZgQ?#8??ox;? ???|??mZ?? I~a?k ~P ?? j??? '?c??4?F??l??$?8???(?'?"?.?????,????9V?????d???????UU)??? ???o?&???4?7?Z?? g???y?
W[?????d?Q$?#??^mZ???B Z(??QE ?Q#Q#Q#Q#Q#??endstream endobj 6 0 obj <</Filter/FlateDecode/Length 846>>stream x??V???+?q??z???'U./h???{???d?U??xf??PQ?O???unA???x1?0 50]#?\?T?y?B?s?9B? ? 2|????C???2t????k?U??]??]{? ?????s?AH??????h?"w????? f?????i??? ??>?9?8?#??"??G?$???<??0???S?2??sn?n??^?5?\FN?o1?4?~4~??Qe=&?T[???????Z??x??????k?????0z'#?;?'a??a?f~?q?~8ZH~?m???????Mm?p?#hh{????W7??????
?8?Olk'?A|?[???P?5?????uGxRr#?pw<$y?n??kD ???0??ih??9?5v??0?_}iG?Dq?8??_U??5a?????k????d???M??2???C(??;t2uA]z ??6A??o?t?}d????[?<;??R?iO8n??f???40???S?aVX????Y?p2N?eq]N?VeE?>??/V0?]MV?&???.aZ-???z2???????????8o??3?S????????gf??B?'6??]?J endstream endobj 8 0 obj<</Contents 6 0 R/Type/Page/Resources<</ProcSet [/PDF /Text /ImageB /ImageC /ImageI]/Font<</F1 3 0 R/F2 4 0 R>>/XObject<</Xf1 1 0 R/Xf2 5 0 R/img0 2 0 R>>>>/Parent 7 0 R/MediaBox[0 0 595 842]>>endobj 3 0 obj<</Subtype/Type1/Type/Font/BaseFont/Helvetica-Bold/Encoding/WinAnsiEncoding>>endobj 4 0 obj<</Subtype/Type1/Type/Font/BaseFont/Helvetica/Encoding/WinAnsiEncoding>>endobj 5 0 obj <</Subtype/Form/Filter/FlateDecode/Type/XObject/Matrix [1 0 0 1 0 0]/FormType 1/Resources<</ProcSet [/PDF /Text /ImageB /ImageC /ImageI]>>/BBox[0 0 292.41 39]/Length 474>>stream x?m??e1C?#???1Ly}??Ua??>????r?R?????r?7gr??a???\??PTj??p???s????~m"???:K??T???1????Gw({c???? !???p?rB g M?QG*?PC ?o??v?????'n[!n2??}*?g}r?G??J?R"aI?S??q ???d;??-??m?????y?lCp??[B(=?L??G[]2??)???
?8???9L????]y)?B??t<??E??????I????????#1?]$? ??h??6?Q[A)?8????<???????z??8c??????s??R????%6? endstream endobj 1 0 obj <</Subtype/Form/Filter/FlateDecode/Type/XObject/Matrix [1 0 0 1 0 0]/FormType 1/Resources<</ProcSet[/PDF/Text]/ExtGState 9 0 R/Font 10 0 R>>/Length 1818/BBox[0 0 595.28 419.53]>>stream x???Ko???t??V|???4-$n??{Pm%]6????P????:?$E?6i ?4??????d??m.U?7L????E??"???e?^r??c????????S#'?????????X?bz?k.J?3!?)??{?V ??'VS1?????8??L???? fU"&Fx?v?Q?9G??EL]?iLIN}?C?i~??4???J<??P?4Ec??F??P%c=!
?=?!U??P?T?b]???k>+¹&?5?9A5ai?"???G????H???J??J?#N??#?3dP??#O=A6%??&dO?eU&5;?Q?#M?'??.??8????P???z!
'??j??O?8??7?
?f????????u???^???:N#?q?Y?xN6Kjv B??Z?????<?? Dx^?J??;A1?3s /?S?k?8??'?9?n??.w?s????g????? M<0????????<?,p???xG!pv?v??O??,?!pv?v?P??l?O??3?M)[????????x??D?h????Z??&i)??,????k???k????j*???-?#?'?x9D)]?J:?=?G??1r? ???!???X?I???|n?q}?=?6?:ðl??????_T??[??_?AC???YI??????+??]??}f}S?P<{??EY??#??q?pah???,Pj?????v~??a?c???{R?7????? ?E~?mv??v?6??t ?? ??Y?????&???F?7P'?e?????R&??(?#????????)?2???P??j?.I??s4?|???s???$z????????E?P??x?{??tU?????????|??b?'?jH????f6 .?g? ?"?????iVR";;?P?'????F?????*??^?b?Nu6rO6? ?Xn[~>t???x2????n?[?D^????6C4O??vx??p?#???$?ru??Yj??55,?Z???u?&?yy????%????+????aMk?3 ???v?1M\A&?q???? '?Sf?,??ce)? ??x?????P?#?Ea&y????/n??~8j???????Co????????????%?? ????????5C???(?<??}???OA???a$?)J?`?!vd????T????D{,?}^?e?]]#?'#T?v??J??;??4?G?e???&b?Bl???K????.?t=s?i?;6.> ?????:?H??Z}:.V? ??) endstream endobj 9 0 obj<</R7 11 0 R/R9 12 0 R>>endobj 10 0 obj<</R8 13 0 R>>endobj 11 0 obj<</TK true/Type/ExtGState/BM/Normal/OPM 1>>endobj 12 0 obj<</Type/ExtGState/SA true>>endobj 13 0 obj<</Subtype/Type1/Type/Font/BaseFont/Helvetica/Encoding 14 0 R>>endobj 14 0 obj<</Type/Encoding/Differences[225/aacute/acircumflex/atilde 231/ccedilla 233/eacute/ecircumflex 243/oacute 245/otilde 250/uacute]>>endobj 7 0 obj<</Kids[8 0 R]/Type/Pages/Count 1>>endobj 15 0 obj<</Type/Catalog/Pages 7 0 R>>endobj 16 0 obj<<>>endobj xref 0 17 0000000000 65535 f 0000004765 00000 n 0000000015 00000 n 0000003908 00000 n 0000004000 00000 n 0000004087 00000 n 0000002787 00000 n 0000007190 00000 n 0000003700 00000 n 0000006794 00000 n 0000006833 00000 n 0000006863 00000 n 0000006922 00000 n 0000006965 00000 n 0000007044 00000 n 0000007240 00000 n 0000007285 00000 n trailer<</Info 16 0 R/ID []/Root 15 0 R/Size 17>>startxref 7305 %%EOF
Since a PDF is a binary file and not a text file, it is quite normal you would see ? and all sorts of other non-printable characters. Instead save it to a file on disk and open with something like ShellExecute. ie:
oHTTP = CreateObject("MSXML2.XMLHTTP.6.0")
oHTTP.Open("GET", 'https://homologacao.plugboleto.com.br/api/v1/boletos/impressa /lote/NIKLfYBWz',.F.)
oHTTP.setRequestHeader("content-type", "application/pdf")
oHTTP.Send()
Local lcFileName
lcFileName = Forcepath(Sys(2015)+'.pdf', Sys(2023))
Strtofile(oHttp.responseText, m.lcFileName)
Declare Long ShellExecute In "shell32.dll" ;
long HWnd, String lpszOp, ;
string lpszFile, String lpszParams, ;
string lpszDir, Long nShowCmd
ShellExecute(_vfp.HWnd,'',m.lcFileName,'','',1)
EDIT: It was not a job MSXML2.XmlHttp. You simply download the file as a PDF and open it:
Local lcFileName, lcRemote
lcRemote = 'https://homologacao.plugboleto.com.br/api/v1/boletos/impressao/lote/NIKLfYBWz'
lcFileName = Forcepath(Sys(2015)+'.pdf', Sys(2023))
If (getFileFromURL(m.lcRemote, m.lcFileName) = 0)
Declare Long ShellExecute In "shell32.dll" ;
long HWnd, String lpszOp, ;
string lpszFile, String lpszParams, ;
string lpszDir, Long nShowCmd
ShellExecute(_vfp.HWnd,'',m.lcFileName,'','',1)
Endif
Procedure getFileFromURL
Lparameters tcRemoteFile,tcLocalFile
Declare Integer URLDownloadToFile In urlmon.Dll;
INTEGER pCaller, String szURL, String szFileName,;
INTEGER dwReserved, Integer lpfnCB
Return URLDownloadToFile(0, m.tcRemoteFile, m.tcLocalFile, 0, 0)
endproc

Delete set of lines between two strings in Excel VBA

I have a task to read a bunch of notepad files and in each of the file, I need to delete the set of lines which are present in between the strings "dynamics" and "end dynamics" and also delete those two strings as well.
The attached image shows the part which needs to be removed, wherever it has occurred (dynamics to end dynamics – any content can be present with these two boundaries) in that text file.
L3.3
resizeText 1
zoomLines 0
zoomArrows 0
doLasso 1
opaqueMove 1
selectDistance 30
adjustFonts 1
doubleBuffer 1
clipping 1
nCopyAreas 0
drawTextLimit 0.5
saveObjects 1
canvasBackground #050048007800
defaultForeground #000000000000
layers 1
layerName 0 0
layerName 1 1
layerName 2 2
layerName 3 3
layerName 4 4
layerName 5 5
layerName 6 6
layerName 7 7
layerName 8 8
layerName 9 9
layerName 10 10
layerName 11 11
layerName 12 12
layerName 13 13
layerName 14 14
layerName 15 15
layerName 16 16
layerName 17 17
layerName 18 18
layerName 19 19
layerName 20 20
layerName 21 21
layerName 22 22
layerName 23 23
layerName 24 24
layerName 25 25
layerName 26 26
layerName 27 27
layerName 28 28
layerName 29 29
layerName 30 30
layerName 31 31
gend
N 0
P 0 0
T -1
R 0 0
0
0 4 1 0
Name #WVP
0 1 1
!
27e
054878
-1-1-1
0
0
0
0 0
dynamics
script
//***GblSymDetails***
;DTLS; GSA_TEXT = "CIUXX"
//***ApplReplace***
//GEMTool = 1
// = ASPECTLINK
end script
end dynamics
0 0 1920 1080 0 0
N 2
P 34.7792 181.549
T 2 21071 1 0 0
0 0
R 0 0
0
0 0 3 0
Name STAT5495
0 1 1
!
27e
a5a5a5
a5a5a5
0
0
0
2 0
0 0 0
0 0 0 0 1
4
0 12.7627
11.5724 0
192.874 0.255264
185.159 12.3798
N 4
P 221.604 181.887
T 2 21071 1 0 0
0 0
R 0 0
0
0 0 5 0
Name STAT5496
0 1 1
!
27e
7c7c7c
7c7c7c
0
0
0
2 0
dynamics
script
func ip_FillColor() {
return FILLCOLOR;
}
func ip_LineColor() {
return LINECOLOR;
}
func ip_TEXT() {
return TEXT;
}
func BackColor() {
return RGB(124,124,124);
}
func ForeColor() {
return RGB(124,124,124);
}
// when ...
object.background = BackColor();
object.foreground = ForeColor();
end script
end dynamics
0 0 0
0 0 0 0 1
4
0 11.6149
6.04944 0
6.04944 54.445
0 72.5933
N 6
P 81.7124 181.888
T 2 21071 1 0 0
0 0
R 0 0
0
0 0 7 0
Name STAT5497
0 1 1
!
27e
616161
616161
0
0
0
2 0
dynamics
script
func ip_FillColor() {
return FILLCOLOR;
}
func ip_LineColor() {
return LINECOLOR;
}
func ip_TEXT() {
return TEXT;
}
func BackColor() {
return RGB(97,97,97);
}
func ForeColor() {
return RGB(97,97,97);
}
// when ...
object.background = BackColor();
object.foreground = ForeColor();
end script
end dynamics
0 1 0
0 0 0 0 1
72
93.8536 5.86588
Something like the code below. You should be able to add code to dump all the files into a folder and DIR through them reading, stripping text and saving the output files automatically. Depending on the number of files it may or may not be time-efficient to do.
Sub cleantext()
Dim lineOfText As String
Dim skipLines As Boolean
'Open files for writing
Open "D:\inputfile.txt" For Input As #1
Open "D:\outputfile.txt" For Output As #2
skipLine = False
Do Until EOF(1)
Line Input #1, lineOfText
If lineOfText = "dynamics" Then skipLines = True
If lineOfText = "end dynamics" Then skipLines = False
If Not skipLines And Not lineOfText = "end dynamics" Then Print #2, lineOfText
Loop
Close #1
Close #2
End Sub

Converting byte[] to object in vb.net

I have a byte array, for example:
Dim byteArray(10) as Byte
byteArray(0) = 1
byteArray(1) = 2
byteArray(2) = 3
...
byteArray(9) = 10
and I am trying to convert it into an object but without success. I have read a lot of posts here about on how to do it so I have below function:
Public Shared Function ByteArrayToObject(ByVal arrBytes As Byte()) As Object
Using ms As New MemoryStream()
Dim binForm As New BinaryFormatter()
ms.Write(arrBytes, 0, arrBytes.Length)
ms.Seek(0, SeekOrigin.Begin)
Dim obj As Object = DirectCast(binForm.Deserialize(ms), Object)
Return obj
End Using
End Function
but when performing the DirectCast I am getting an exception saying, more or less (translated from spanish):
"SerializationException was unhandled: End of sequence reached before terminating analysis".
Any idea why is it happening?
You have an array of bytes:
Dim byteArray(10) as Byte
byteArray(0) = 1
byteArray(1) = 2
byteArray(2) = 3
...
byteArray(9) = 10
Which is this byte stream:
1 2 3 4 5 6 7 8 9 10
But you don't have a serialized object. Which is what your code assumes:
Dim obj As Object = DirectCast(binForm.Deserialize(ms), Object)
That stream can't be de-serialized into an instance of Object because, well, it isn't a serialized instance of Object. But this is (or at least is on my machine in my test):
0 1 0 0 0 255 255 255 255 1 0 0 0 0 0 0 0 4 1 0 0 0 13 83 121 115 116 101 109 46 79 98 106 101 99 116 0 0 0 0 11
Basically, you can't just de-serialize anything into an instance of an object. It has to be an actual serialized version of that object.

copy sets of numbers into a listbox from a textbox

I have a textbox that the user puts in sets of numbers(e.g. 32 45 98 56 52 1 23) and I need to copy these numbers into a listbox so that each number is its own item. So far I have this
For Each ch As Char In TextBox20.Text
If Char.IsDigit(ch) Then
ListBox1.Items.Add(ch)
End If
Next
but the problem is that it will copy each digit as an item so we will end up with
3
2
4
5
9
8
5
6
I need it to copy them like this
32
45
98
56
here is a sample of how to do that
Sub addToListBox()
Dim sample As String
Dim v As Variant
Dim i As Integer
sample = "32 45 98 56 52 1 23"
v = Split(sample, " ")
For i = 0 To UBound(v)
If IsNumeric(v(i)) Then
ListBox1.Items.Add(v(i))
End If
Next i
End Sub