Author Topic: Translation VBA to .NET  (Read 9522 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