Migrating a string table (binary .res file) from VB6 to VB.NET - vb.net

I have to port an old VB6 program to VB.NET and stumbled across an old ".res" (Resource) file, which is stored in binary format. Using VS 2013, I can embed that file into my .NET project, and VS shows me that it contains simply a string table.
The problem is, I cannnot figure out how to bring those res file into a more modern text format, or how to load the strings directly from the res file. I linked file to my application es an embedded resource, but all my atttempts to use VB6.LoadResString from the "Visual Basic Compatibility library" lead to an exception, showing the key was not found.
Furthermore, it seems VS does not allow me to copy/paste the string table into a text file, at least, not at a whole. Actually, it allows me to copy/paste one string after another, but as you can imagine, that is extremely cumbersome and error-prone. That is why I am looking for a better solution. Any ideas?

There is a functional VB6 Class for doing this at:
ResDecomp Class Decompiles RES Files
Sample programs are included, one of them a sort of "viewer" and the other just extracts RT_STRING resource strings to an XML document. You could easily change the latter to dump the string values to a text file, database, etc. instead.

Embed the .res file to a simple VB6 program which loops from min to the max ID and write out the strings to a text file using LoadResString (error trap for missing IDs).

Related

Pass a bitmap object to Interop Word function that is expecting a filename string for a bitmap file

The title sounds insane but bear with me. This is a problem that could exist with any object.
I am generating a bitmap object in memory and I would like to pass it directly to another function that wants to open a bitmap file. The simple solution is to write the file to disk, call the function against the file, and then delete the file. I don't want to do that. If I am pushing a high volume of image objects in to a Word document with a VSTO add-in it doesn't make sense to thrash my disk for no reason when the whole thing could be done in memory.
I guess I am looking for a different function to insert a picture in to a Word document that accepts a bitmap object. Or a way to pass a filesystem object that actually points to memory (Not a RAMDisk, but a RAMFile?). Or a way to wire the "Image.Save" directly to the reader of the "AddPicture" function without actually making a file on disk.
Hopefully, there is a better way of doing this.
Here is the code example:
Dim newImage = GenerateImage(InputString, SelectedFormat)
Dim imagePath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName())
newImage.Save(imagePath, ImageFormat.Png)
With Globals.ThisAddIn.Application
.Selection.InlineShapes.AddPicture(imagePath)
End With
File.Delete(imagePath)
Word can't "stream" (see "Background", below) content, so your choices are 1) The Clipboard or 2) wrapping the bitmap in valid, Word Open XML OPC flat-file format, which means first converting the bitmap to base64.
For the first, you can use standard .NET methods to place the information on the Clipboard in the format you want Word to use. In the Word "interop", the Paste or PasteSpecial methods will insert it. The argument against this approach is, as ever, "interfering" with the user's Clipboard.
Using Word Open XML is as close as you can get to "streaming" content into Word, using the Range.InsertXML method.
Word documents (and other Office files) are essentially "zip packages" of XML and binary files that together make up the document. It's possible to create and edit these files without opening them in the Word (Office) application, which makes the format suitable for server-side work. Any tool that can work with zip files and xml can be used for this; standard is the Microsoft Open XML SDK which offers a complete API of the Office content.
Word, alone, of all the Office applications enables the developer to read and write content in the opened Word document using the OPC flat-file standard. This "concatenates" the entire content of the zip package into an XML String. The Word object model's Range.InsertXML method is used to write content in this format to a Word document open in the Word application.
Information on how to convert a zip package into OPC flat file can be found in this blog article. Information concerning minimal Word Open XML to have a valid OPC version is described in this article; there is a section in there specifically about working with graphics.
Background
Word is based on very old technology - late 1980's. By the mid-1990's it reached a very high standard as a professional word processor and what has happened with it since has mostly been "sugar coating" - adding a bit of this and a bit of that to bring it closer to HTML / page layouting. But the core of the application remains the same... and part of that means Word isn't able to do many of the things the modern developer expects - such as "streaming" data in and out.

How to save matlab result and use it in VB.net

I was wondering how can i save the result from a matlab function and use it in VB.net (if not possible then save the result in a .txt file and then use it in VB.net). thanks in advance.
MATLAB can be linked by many software such HSPICE, ANSYS , ...
There are special toolbox for these software , But I do not know ant toolbox for VB.
A simple solution is the save your result in text file.
Text file can be read in any programming language.
A important note in saving, pay attention to encoding. Source and Destination encoding must be same.

Import .docx contents into MS Access

I began writing a docx document to do a project of mine.
Recently, I realized that it would be easier to manage that data if it was in a database.
So, I wanted to import that data into MS Access automatically, to avoid copying and pasting the data manually.
Is there anyway to do it? I have only encontered ways of opening Word application via Access. I also know that docx has a XML structure, so I imagine if I can open that structure, it would be easy to do a parser in VBA
There are two basic ways information can be taken out of a Word document and put into an Access database: automating the Word object model using VBA code running in either Word or Access OR extracting the WordOpenXML that makes up the Word document. You indicate you lean towards the second option.
Here, again, there are a number of approaches available:
Use VBA in Word or Access to extract the WordOpenXML of the document open in the Word application user interface.
Use VBA in Access together with non-VBA tools to "crack open" the Zip file and extract the XML.
Use the tools available in the .NET Framework to extract the content of the ZIP file and write it to Access using an OLE DB connection.
I understand your goal is to be able to recreate the document at a later point for printing, so you want to preserve all the formatting. In addition, you want to be able to read the content from within Access.
I believe this will require a minimum of four fields in the Access table:
ID
Title
Text of song
The complete WordOpenXML for re-creating the document
You don't mention (4) in the discussion and problem description, but if you want to store the formatting AND you want to be able to read the content I believe this is necessary. While WordOpenXML is "readable", there's a lot of mark-up in there which doesn't make reading comfortable.
All things being equal, I'd go for either VBA working on the open Word document or the .NET approach, using the Open XML SDK (free download .NET library you can reference in Visual Studio and distribute with solutions).
One important thing to keep in mind is storing the Word Open XML in the database. Unless something has changed in Access, you can't store the ZIP file - you need a "streamable" format. That would be the OOXML OPC flat-file format.
When you read the WordOpenXML from a document using VBA, that's what you get, which is why that would be an option for me. The Open XML SDK doesn't have that option, but there is code available from Eric White's blog for doing this.
When you later want to recreate and print the document it should be enough to stream the WordOpenXML to a file with the .xml extension. Or you could convert it back to a docx zip file (same blog).

opening and writing to files in visual basic 2010

Am writing a simple application which can write a to pdf,doc,xls and access files. so far it can write to word.i also want it to be able to navigate a hard disk and open these files using filters.
i was using this code to write to the files
My.Computer.FileSystem.WriteAllText(SaveFileDialog1.FileName, TextBox1.Text, False)
how can i write to pdf and access files and also navigate and open files using openFileDialog?
You should be using System.IO for writting to files. Read the documentation on StreamWriter. It is very straight forward. One of the constructors for streamwriter accepts a string representation of the path to the file, and overloads allow you to specify a FileMode enumeration value. Normally you will use FileMode.OpenOrCreate when writting to the file.
OpenFileDialog is also straightforward. Create an instance and access the selected file property to get a string representation of the path. Use the static File.Exists("path") to check that a valid path was returned, then use the give path to open the file with a StreamReader.
There is more than one way to skin a cat here because static oriented FileInfo and DirectoryInfo are provided in System.IO, and there are the corresponding File and Directory classes which must be instantiated.
The use of these classes is very straightforward so I'm not going to sit here and type you example code but that should get you started.
As far as creating PDF and XLS files, I am assuming that you already have raw bytes that are in the correct format for those file types? If not, I can't help you there off hand. There are no Formatters in the .NET Framework that will convert ASCII or Unicode strings to a format that is acceptable for PDF or XLS that I am aware of. You are going to either need to dig into the specifics of those file formats or find a third party utility that will format your raw bytes or text into something those specifications.
If you are recieving the PDF and XLS data in the raw already properly formatted just use BinaryWriter to create the new file and write the raw array of bytes.

Include sql scripts in a VB6 application

I am maintaining an old VB6 application, and would like to include SQL scripts directly in part of the project. The VB6 application should then extract the text of this script and execute it on the server.
The reasons for this approach are various - among others, we want to deliver only an updated executable rather than a complete update/installation package. Hence, the SQL scripts need to be compiled into the application just like a resource file. And, obviously, one has to be able to get at the content from code, in order to send it to the database server.
Does anyone have a good way to do this?
The simplest solution is to just create a VB module with the scripts as strings.
If you want to use a resource file instead, you can do that too. You can associate a resfile with a VB project (I don't remember how to do this directly in the VB IDE but the VBP file supports a ResFile32 parameter).
EDIT: It seems like the issue here is mostly about formatting -- you don't want to store SQL queries as one long string, but formatting the query nicely inside VB is tedious because you have to add quotes, add string concatenation operators to join the lines together, etc.
I would recommend placing the SQL in a text file and formatting it in whatever way you like. Write a script that will take the text and convert it into a VB module. The build process would be modified to always apply this script first before compiling the application.
For scripting, use your favorite scripting language; if you don't have a favorite scripting language, this is an easy enough task that you could do it in VB, C#, or any other language. If it were me, I'd probably use awk (gawk) or Python.
If you want to use a resource (.RES) to store your SQL, go to the menu:
Add-ins > Add-in Manager...
and select VB 6 Resource Editor. Configure the add-in to be loaded and to load at startup.
From the editor add-in, VB provides a simple interface to add resource strings. You will refer to these using the provided constant values. To load the strings at runtime, use the LoadResString function:
Public Const SQL_INSERT As Integer = 101
Dim strSQL As String
strSQL = LoadResString(SQL_INSERT)
(replace "101" with the constant value of the string you wish to load)
Just another thought on your approach. Because I find myself tweaking the program's behavior or UI for customers I might be in the middle of a change that either is not ready or has not yet been tested and approved. So if I have properties that change from time to time, but I want to maintain control of, for instance connection settings to our ftp server, I will create a resource only dll exposing my properties and use a resource file in the dll to supply the values. When my network manager changes something on the ftp server I change the strings in the resource maanger, recompile the dll and release just the updated dll. I'm sure there are many more solutions, but that is how I do it. If you don't think you might have to change your SQL scripts at the same time you are changing you exe this probably only complicates your work. It has worked well enough for me that now this is pretty much standard for me.