Author Topic: Translation VBA to .NET  (Read 9523 times)

0 Members and 1 Guest are viewing this topic.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Translation VBA to .NET
« on: October 01, 2010, 05:42:50 PM »
I had planned translating a couple of VBA routines to .NET today.

First glitch ....
My box is win7 x64 using ACADM2010 ACADM2011

AND I CAN"T READ THE .DVB 'cause I don't have the VBA enabler downloaded.


Before I download the enabler.
Has anyone had functionality problems using VBA on an x64 machine ???
« Last Edit: October 01, 2010, 05:50:28 PM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Swift

  • Swamp Rat
  • Posts: 596
Re: Translation VBA to .NET
« Reply #1 on: October 01, 2010, 05:48:06 PM »
I've not under vanilla 2010 and Vista

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Translation VBA to .NET
« Reply #2 on: October 01, 2010, 06:06:47 PM »

Thanks David.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Translation VBA to .NET
« Reply #3 on: October 01, 2010, 06:20:25 PM »
This might give you some ideas

http://blogs.msdn.com/b/csharpfaq/archive/2010/09/28/converting-a-vba-macro-to-c-4-0.aspx

I like the alt + click trick in it

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Translation VBA to .NET
« Reply #4 on: October 01, 2010, 06:35:24 PM »

Thanks Jeff.


Now, where did I store my VB6 installation CD ??
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Translation VBA to .NET
« Reply #5 on: October 01, 2010, 06:59:31 PM »


Thinking about Migration ..

anyone heard from  'Mook' in Hawaii. ??
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Translation VBA to .NET
« Reply #6 on: October 02, 2010, 08:03:36 PM »
Not in a few years.  I had a play with 7 and vba, slow, but functional.  I printed all my stuff to pdf filed it away for conversion later.

edit: I should add that the 7 machine was a VM, so that might have caused more slowness than you will experience
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Translation VBA to .NET
« Reply #7 on: October 02, 2010, 09:42:50 PM »

Thanks David.
I only have a couple I want to Migrate, so no big issue ... and I've had some enquiries regarding translating some code, so I thought I'd better get my act together.

Quote
Not in a few years.
I assume that was regarding migration rather than having contact with Leslie. ?
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Bryco

  • Water Moccasin
  • Posts: 1883
Re: Translation VBA to .NET
« Reply #8 on: October 03, 2010, 12:19:41 PM »
Kerry I thought you had to download the vba from autodesk.

I had so much code to convert from vba to C#  I wrote this
Code: [Select]
        [CommandMethod("VtoC")]
        public static void VbaToCsharp()
        {
            MessageBox.Show("starting");
            // the file to read from.
            string path = @"C:\Documents and Settings\Yourname\Desktop\VtoC.txt";
            string readText = File.ReadAllText(path);

            readText = readText.Replace("Option Explicit", "");
            readText = readText.Replace("Exit Sub", "return;");
            readText = readText.Replace("End Sub", "} //end");
            readText = readText.Replace("End Function", "} //end");
            readText = readText.Replace("Private", "private");
            readText = readText.Replace("Public", "public");
            readText = readText.Replace("End If", "}");
            readText = readText.Replace("()", "()" + Environment.NewLine + "{");
            readText = readText.Replace(" Sub ", "static void ");
            readText = readText.Replace(" Function ", "static  ");
            readText = readText.Replace("If", "if(");
            readText = readText.Replace("Then", ")");
            readText = readText.Replace("Else", "else" + Environment.NewLine + "{");
            readText = readText.Replace("ElseIf", "else if(");
            readText = readText.Replace("Select Case", "switch(");
            readText = readText.Replace("End Select", "}");
            readText = readText.Replace("Case", "case");
            readText = readText.Replace("For Each", "foreach (");
            readText = readText.Replace(" In ", " in ");
            readText = readText.Replace("On Error Resume Next", "");
            readText = readText.Replace("Next", "} //next");
            readText = readText.Replace("Not", "!");
            readText = readText.Replace("While","while");
            readText = readText.Replace("Wend", "}");
           
            readText = readText.Replace("'", "//");
            readText = readText.Replace("(0)", ".X");
            readText = readText.Replace("(1)", ".Y");
            readText = readText.Replace("(2)", ".Z");
            readText = readText.Replace("String", "string");
            readText = readText.Replace("Double", "double");
            readText = readText.Replace("AcadBlockReference", "BlockReference");
            readText = readText.Replace("False", "false");
            readText = readText.Replace("GoTo", "goto");

            readText = readText.Replace("On Error GoTo 0", "");
            readText = readText.Replace("On Error GoTo Err_Control", "");
            readText = readText.Replace("Set", "");
            readText = readText.Replace("ThisDrawing.ActiveUCS = CurrentUcs", "");
            readText = readText.Replace("Err_Control:", "");
            readText = readText.Replace("Err.Clear", "");
            readText = readText.Replace("Err.Number", "");


            readText = readText.Replace("Resume", "");
            readText = readText.Replace("Dim util As AcadUtility", "");
            readText = readText.Replace("Set", "");

            readText = readText.Replace("AcadLWPolyline", "Polyline");
            readText = readText.Replace("AcadAttribute", "Attribute");
            readText = readText.Replace("AcadCircle", "Circle");
            readText = readText.Replace("AcadLWPolyline", "Polyline");
            readText = readText.Replace("AcadLWPolyline", "Polyline");

            readText = readText.Replace("Pi ", "Math.PI ");
            readText = readText.Replace("Color", "ColorIndex");
         
            readText = readText.Replace("True", "true");
            readText = readText.Replace("MsgBox", "MessageBox.Show(");
            File.WriteAllText(path, readText);
        }


    }    //end test ////////////////////////////////
To use it you copy some code from vba to notepad and save it on your desktop.
Then run the code. It could do with a lot of work but it does save a little time.
If you copy code directly from vba you can crash.
I can definately see the point made, that is just as easy to rewrite it than convert it.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Translation VBA to .NET
« Reply #9 on: October 04, 2010, 02:06:01 PM »
Actually, it has been a few years since I last spoke to Mook
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

mohnston

  • Bull Frog
  • Posts: 305
  • CAD Programmer
Re: Translation VBA to .NET
« Reply #10 on: October 04, 2010, 07:55:44 PM »
First glitch ....
My box is win7 x64 using ACADM2010 ACADM2011

AND I CAN"T READ THE .DVB 'cause I don't have the VBA enabler downloaded.

Before I download the enabler.
Has anyone had functionality problems using VBA on an x64 machine ???

I had many problems with x64. To be fair I support 32bit XP systems and was trying to make the same code run on my 64bit system.
Between Win 7 (x64), VS 2010 (x64) and Acad 2011 (x64) I found it to be too much trouble and did a reinstall to 32.
If I didn't have to support 32bit I suppose I would just put my head down and work out all the issues.
Otherwise I don't see much ROI for the pain of 64.
Our drawings aren't very large, and we do have a great deal of legacy VBA.

It's amazing what you can do when you don't know what you can't do.
CAD Programming Solutions

vegbruiser

  • Guest
Re: Translation VBA to .NET
« Reply #11 on: November 19, 2010, 12:02:14 PM »
I'm running ACADM2011 on Win 7 x64 with Visual Studio 2010 (admittedly without vba support) and the only issue I've had so far was debugging with C# - you have to add /p to the Command Line Arguments box on the debug page otherwise acad.exe automatically starts with the AutoCAD Mechanical profile and will cause all kinds of BS - at least, it caused 5 days+ of head scratching - I even sent my code to Kean Walmsley only to be told that it ran just fine on his vanilla 2011 installation.  :pissed:

EDIT: Actually, just putting /p isn't enough. (Yet it was yesterday!?)

It seems you need to put "/p <<VANILLA>>" to force regular AutoCAD if you have AutoCAD Mechanical installed. As per the below:



:)
« Last Edit: November 19, 2010, 12:34:40 PM by vegbruiser »

Bryco

  • Water Moccasin
  • Posts: 1883
Re: Translation VBA to .NET
« Reply #12 on: November 19, 2010, 08:21:41 PM »
Thanks I needed to do that too. Funny I dont use the quotes
Quote
/p Lex2010 /nologo /b GetThangs10.scr

jjs

  • Guest
Re: Translation VBA to .NET
« Reply #13 on: January 20, 2011, 09:41:26 AM »
If you run autocad with a certain profile, then launch autocad again it will use the last profile that you ran (assuming the shortcut you used to launch autocad does not force the profile with the /p switch). So if you run the autocad shortcut that comes with MEP. Close autocad. THen open vs project and launch autocad without any /p it will load up the 2D Drafting & Annotation profile.
But that still has not resolved my issues with Win7 64Bit and Autocad MEP 2011

jjs

  • Guest
Re: Translation VBA to .NET
« Reply #14 on: January 20, 2011, 09:44:47 AM »
Here is the error i get
Code: [Select]
Cannot load assembly. Error details: System.BadImageFormatException: Could not
load file or assembly 'file:///C:\Users\JJS\Documents\Visual Studio
2010\Projects\VbMgdAcadJJS\VbMgdAcadJJS\bin\Debug\Testing.dll' or one of its
dependencies. This assembly is built by a runtime newer than the currently
loaded runtime and cannot be loaded.
File name: 'file:///C:\Users\JJS\Documents\Visual Studio
2010\Projects\VbMgdAcadJJS\VbMgdAcadJJS\bin\Debug\Testing.dll'
   at System.Reflection.Assembly._nLoad(AssemblyName fileName, String codeBase,
Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark,
Boolean throwOnFileNotFound, Boolean forIntrospection)
   at System.Reflection.Assembly.InternalLoad(AssemblyName assemblyRef,
Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
   at System.Reflection.Assembly.LoadFrom(String assemblyFile)
   at Autodesk.AutoCAD.Runtime.ExtensionLoader.Load(String fileName)
   at loadmgd()

WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value
[HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure
logging.
To turn this feature off, remove the registry value
[HKLM\Software\Microsoft\Fusion!EnableLog].

It shows up when in Autocad when I netload the debug.dll

kaefer

  • Guest
Re: Translation VBA to .NET
« Reply #15 on: January 20, 2011, 09:55:15 AM »
Here is the error i get
Code: [Select]
Cannot load assembly. Error details: System.BadImageFormatException

Add this to your 2011 acad.exe.config.
Code: [Select]
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0"/>
  </startup>

jjs

  • Guest
Re: Translation VBA to .NET
« Reply #16 on: January 20, 2011, 10:03:56 AM »
you are awesome.

jjs

  • Guest
Re: Translation VBA to .NET
« Reply #17 on: January 20, 2011, 10:39:38 AM »
Kerry,
Are you looking for Mook specifically or for the VBA to VB translator he had made? If you remember what the name of it was, I can look and see if i might still have it.
JJS

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Translation VBA to .NET
« Reply #18 on: January 20, 2011, 03:11:13 PM »

Hi jj
Just wondering what became of 'mook'.
I think I have his translator in one of my archives, thanks.

Stay well
Kerry
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

jjs

  • Guest
Re: Translation VBA to .NET
« Reply #19 on: January 20, 2011, 03:44:54 PM »
hopefully i will be around more. I finally got a 64bit machine with enough oomph to run 2011. The problem is that none of my vba routines work very well because fo the 64 bit. SO i am going to have to start porting. I am also starting to use Autocad MEP for the electrical. It needs quite a bit of customization to make it fast. THings that should be a simple command line prompt are 6 mouse clicks all over the screen.

vegbruiser

  • Guest
Re: Translation VBA to .NET
« Reply #20 on: January 26, 2011, 11:56:01 AM »
Hi all,

I realise this is slightly off-topic, so it might benefit from its' own thread, but it might save a whole heap of work for someone:

I was looking around for an Excel VBA Protection remover (I bought one a couple of years back but couldn't find the installer for it anywhere on my system) and stumbled across the following:
Removing an Excel Workbook VBA Password
 
A VBA project password can be removed with a hex editor. Close the workbook and open the workbook file in the hex editor. Find the string "DPB" and change it to "DPx". Save the file. Open the workbook and click OK until the workbook is open (one or more dialogs are displayed describing various problems with the VBA project). Press ALT+F11, choose the menu command Tools->VBAProject Properties, navigate to the Protection tab, and change the password but do not remove it (note the new password). Save, close, and re-open the workbook. Press ALT+F11 and enter the new password. Choose Tools->VBAProject Properties, navigate to the Protection tab, and remove the password. Save the workbook.

I couldn't quite believe it until I tried it on a new workbook myself. I've no idea if it will work on any other vba-created application, but feel free to give it a whirl.

:)

bikelink

  • Guest
Re: Translation VBA to .NET
« Reply #21 on: February 04, 2012, 03:17:26 PM »
perfect! thank You

poncelet

  • Guest
Re: Translation VBA to .NET
« Reply #22 on: February 07, 2012, 06:18:09 AM »
VBA is very slow in acad2011

Arizona

  • Guest
Re: Translation VBA to .NET
« Reply #23 on: February 07, 2012, 09:37:48 AM »
I have been testing some of our VBA programs under Windows 7 with Autocad 2012. The first thing I found is that every one of them that ran using Randall's batch processing failed. Many of the VBA programs (not requiring batch processing) that strictly deal with changing properties of existing (common) entities, passed on the Win 7 computer with the VBA enabler installed. Although these will still need to be re-written in the future, they have moved down my priority list.
On the optimistic side of re-writing these programs is that many of the wish list items from my users can now be easily incorporated in the newer versions. As well as some of those "Oh man, I wish I had thought of that sooner".
Practice, practice, practice... Probably what I need the most of  :-)