TheSwamp

Code Red => VB(A) => Topic started by: krampaul82 on February 18, 2011, 01:43:22 PM

Title: Vb to VBA
Post by: krampaul82 on February 18, 2011, 01:43:22 PM
Option Explicit
The following code works in visual Basic but I cannot seem to get it to work in vba 2010


Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpszOp As String, ByVal lpszFile As String, ByVal lpszParams As String, ByVal lpszDir As String, ByVal FsShowCmd As Long) As Long
Public Function OpenFile(ByVal FileName As String) As Long
   OpenFile = ShellExecute   [This is where it hangs]  (0, "Open", FileName, "", "C:\ALL_pdf_Cut_Sheets\Actuators\", 1)
End Function

Sub file_get()
   OpenFile "C:\ALL_pdf_Cut_Sheets\Actuators\ms8105a1008.pdf"
End Sub

Any ideas appreciated...

Mark.....
Title: Re: Vb to VBA
Post by: Matt__W on February 18, 2011, 01:48:28 PM
For what it's worth, it works just fine in 2008.   :|
Title: Re: Vb to VBA
Post by: krampaul82 on February 18, 2011, 01:54:53 PM
I Always seem to get results that are not normal  :x Do I need to have certian references checked?
Title: Re: Vb to VBA
Post by: Matt__W on February 18, 2011, 02:06:04 PM
The only references I have checked are the defaults for 2008.
Title: Re: Vb to VBA
Post by: Keith™ on February 20, 2011, 01:17:49 AM
What OS are you using? Is it the same one when you were using this previously?
Title: Re: Vb to VBA
Post by: jgr on February 20, 2011, 01:47:14 PM
Checks the return value of ShellExecute.

ShellExecute returns a value greater than 32 if successful, or an error value that is less than or equal to 32 otherwise.

Error codes:
http://msdn.microsoft.com/en-us/library/bb762153%28v=vs.85%29.aspx

Private Const ERROR_BAD_FORMAT As Long = 11
Private Const SE_ERR_ACCESSDENIED As Long = 5
Private Const SE_ERR_ASSOCINCOMPLETE As Long = 27
Private Const SE_ERR_DDEBUSY As Long = 30
Private Const SE_ERR_DDEFAIL As Long = 29
Private Const SE_ERR_DDETIMEOUT As Long = 28
Private Const SE_ERR_DLLNOTFOUND As Long = 32
Private Const SE_ERR_FNF As Long = 2
Private Const SE_ERR_NOASSOC As Long = 31
Private Const SE_ERR_OOM As Long = 8
Private Const SE_ERR_PNF As Long = 3
Private Const SE_ERR_SHARE As Long = 26
Title: Re: Vb to VBA
Post by: krampaul82 on February 21, 2011, 09:36:18 AM
What OS are you using? Is it the same one when you were using this previously?
windows xp32 bit. The original code was written in visual basic 6.0 and i tried to get it to work in acad2010 vba (the add on.) and it hangs at the openfile = ShellExecute(0, "Open", FileName, "", "C:\gtc_proj\2008_Blocks\Valves\Honeywell\Product Link\", 1).  The Run-time error says 49 Bad DLL calling convection.
any help appreciated.... at your convienence.... Mark
Title: Re: Vb to VBA
Post by: jgr on February 21, 2011, 10:32:44 AM
 windows xp32 bit. The original code was written in visual basic 6.0 and i tried to get it to work in acad2010 vba (the add on.) and it hangs at the openfile = ShellExecute(0, "Open", FileName, "", "C:\gtc_proj\2008_Blocks\Valves\Honeywell\Product Link\", 1).  The Run-time error says 49 Bad DLL calling convection.
any help appreciated.... at your convienence.... Mark
[/quote]

It's strange.
it works fine in my pc: XP SP3 32-bit + AutoCAD 2010
Title: Re: Vb to VBA
Post by: krampaul82 on February 21, 2011, 11:34:36 AM
windows xp32 bit. The original code was written in visual basic 6.0 and i tried to get it to work in acad2010 vba (the add on.) and it hangs at the openfile = ShellExecute(0, "Open", FileName, "", "C:\gtc_proj\2008_Blocks\Valves\Honeywell\Product Link\", 1).  The Run-time error says 49 Bad DLL calling convection.
any help appreciated.... at your convienence.... Mark

It's strange.
it works fine in my pc: XP SP3 32-bit + AutoCAD 2010
[/quote] I know! i have had many other quirks with other things in vba (things like this that work on other peoples machines but not mine!) very frustrating :realmad: I really should dump vba altogether but I have and extensive library of blocks that was set up in vb(a) and i know the boss will not shell out the bucks for vb.net program.  If it was not for me they would still be on ACAD R13!
thank you for your input though.

Mark...
Title: Re: Vb to VBA
Post by: Keith™ on February 21, 2011, 11:57:01 AM
VB.Net is a free download for the express version - but at least there is a clue in your latest comments,

The bad calling convention sounds like the variable types are getting mangled in the API call. In .NET the behavior has changed and instead of Long, many times you have to use IntPtr.
Title: Re: Vb to VBA
Post by: krampaul82 on February 22, 2011, 09:09:24 AM
VB.Net is a free download for the express version - but at least there is a clue in your latest comments,

The bad calling convention sounds like the variable types are getting mangled in the API call. In .NET the behavior has changed and instead of Long, many times you have to use IntPtr.
Does vb.net run like vb or vb(a)? I am not very good at vb(a) i usually hack what other people have done and taylor it to my needs. why do they always take a good thing and complicate it? (sorry for the rant)  :| Mark...
Title: Re: Vb to VBA
Post by: Keith™ on February 22, 2011, 10:15:28 AM
The only thing similar to VB and VBA in VB.NET is the language, and there are some key differences. If you understand that, then porting your code to .NET is a pretty straightforward process, however, that being said, when using a .NET assembly in AutoCAD, you have to create an interface for your application because AutoCAD doesn't do that for you.

There are tons of examples showing how to build a .NET assembly and make it work in AutoCAD - add a few libraries and then build the command interface and you are done.  (Yeah, I know .. if it were only that easy)
Title: Re: Vb to VBA
Post by: krampaul82 on February 22, 2011, 12:07:41 PM
The only thing similar to VB and VBA in VB.NET is the language, and there are some key differences. If you understand that, then porting your code to .NET is a pretty straightforward process, however, that being said, when using a .NET assembly in AutoCAD, you have to create an interface for your application because AutoCAD doesn't do that for you.

There are tons of examples showing how to build a .NET assembly and make it work in AutoCAD - add a few libraries and then build the command interface and you are done.  (Yeah, I know .. if it were only that easy)
does the express version handle that? i do not mind digging into it if it does...
Title: Re: Vb to VBA
Post by: Keith™ on February 22, 2011, 01:56:21 PM
It does, in fact, if you take your VBA code and paste it into the VB6 upgrade window, it will attempt to convert it for you. The only issue I had was most of the interfaces aren't available until you actually add the required libraries so you will probably have some types that don't convert properly.

Oh, and you have to make sure to install the .NET libraries for each version being used.
VS '10 Express comes with .NET 4.0 but I'm not sure if AutoCAD is fully compatable with this version - since I haven't built an AutoCAD assembly in well over a year ... and the last version I worked on was A2k8

Oh, if you get the entire VS package, you can also forray into C# ...  it isn't hugely different from VB except in some of the code delimiters, and the fact that case is important (i.e. thisObject is different from thisobject) ... C# seems to be in-vogue right now ... although I never quite understood why, since VB.NET and C#.NET all get compiled to CLI anyway ... the reflector will convert the code between as needed with few modifications.

Good luck!
Title: Re: Vb to VBA
Post by: dgorsman on February 22, 2011, 01:58:45 PM
And, the licensing allows you to use the Express editions for corporate and for-profit (i.e. non-hobby programming) work.
Title: Re: Vb to VBA
Post by: krampaul82 on February 23, 2011, 04:11:52 PM
It does, in fact, if you take your VBA code and paste it into the VB6 upgrade window, it will attempt to convert it for you. The only issue I had was most of the interfaces aren't available until you actually add the required libraries so you will probably have some types that don't convert properly.

Oh, and you have to make sure to install the .NET libraries for each version being used.
VS '10 Express comes with .NET 4.0 but I'm not sure if AutoCAD is fully compatable with this version - since I haven't built an AutoCAD assembly in well over a year ... and the last version I worked on was A2k8

Oh, if you get the entire VS package, you can also forray into C# ...  it isn't hugely different from VB except in some of the code delimiters, and the fact that case is important (i.e. thisObject is different from thisobject) ... C# seems to be in-vogue right now ... although I never quite understood why, since VB.NET and C#.NET all get compiled to CLI anyway ... the reflector will convert the code between as needed with few modifications.

Good luck!
Could you provide a link for download?
Title: Re: Vb to VBA
Post by: Keith™ on February 23, 2011, 08:28:18 PM
To download the VB Express go to the Microsoft website:

http://www.microsoft.com/express/downloads/

click on the Visual Studios 2010 Express tab, then click the item "Visual Basic 2010 Express Edition". Make sure to select the language version of your choice and then select the Free Download icon to download the setup file to your computer.
Title: Re: Vb to VBA
Post by: krampaul82 on February 24, 2011, 10:54:54 AM
To download the VB Express go to the Microsoft website:

http://www.microsoft.com/express/downloads/

click on the Visual Studios 2010 Express tab, then click the item "Visual Basic 2010 Express Edition". Make sure to select the language version of your choice and then select the Free Download icon to download the setup file to your computer.
Thank You Keith! I really appreciate all of your help, both past and present,  It has made my life WAAAY easier... Mark