none
can I write word macro with visual basic 6?

    Question

  • I have a GUI application written in Visual basic 6.0, is there any to import it into word so that it will be a macro?
    Wednesday, June 13, 2007 9:54 AM

Answers

  • If you want to get some values from the GUI you can use the following

     

    Code Snippet

    Sub Run_VB6App_FromWord()

    sCmd = "C:\Program Files\MyFile.exe"
    vntResult = OpenProcess(PROCESS_QUERY_INFORMATION, False, Shell(sCmd, 1))
    GetExitCodeProcess vntResult, lngExitCode
      
    Do
        GetExitCodeProcess vntResult, lngExitCode
        DoEvents
    Loop While lngExitCode = STILL_ACTIVE


    End Sub

     

    Do not forget to add the following in declaration

     

    Code Snippet

    'API declarations
    Public Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
    Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long

     

    GOOD LUCK

    Shasur 

     

    Wednesday, June 13, 2007 10:20 AM

All replies

  • If you want to get some values from the GUI you can use the following

     

    Code Snippet

    Sub Run_VB6App_FromWord()

    sCmd = "C:\Program Files\MyFile.exe"
    vntResult = OpenProcess(PROCESS_QUERY_INFORMATION, False, Shell(sCmd, 1))
    GetExitCodeProcess vntResult, lngExitCode
      
    Do
        GetExitCodeProcess vntResult, lngExitCode
        DoEvents
    Loop While lngExitCode = STILL_ACTIVE


    End Sub

     

    Do not forget to add the following in declaration

     

    Code Snippet

    'API declarations
    Public Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
    Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long

     

    GOOD LUCK

    Shasur 

     

    Wednesday, June 13, 2007 10:20 AM
  • Thanks, but unless i misunderstood you, that's not what I wanted... 

    I wanted the macro itself to be written in vb6 (I read somewhere that vba is compatible with vb6 so I was hoping that this is possible, but I guess it's not...)

    Thanks anyway,

    Neta.

    Wednesday, June 13, 2007 11:36 AM
  • OK I got it

     

    Code Snippet

    Function CreateProposal()

        Dim oWA As Word.Application     '* Word Application Object
        Dim oWD As Word.Document        '* Word Document Object
       
        Set oWA = New Word.Application
        Set oWD = oWA.Documents.Open("c:\document.doc")


    End Function

     

    You can use the above to get the Word instance You can use all Word objects . ActiveDocument, Bookmarks etc

     

    You should include the Microsoft Word 9.0 Object Library in References

     

     Regards

    Shasur

    Wednesday, June 13, 2007 12:11 PM
  • let me see if I got this right:

    you offer to add the above code to my vb6 application, so that I can access the word objects from my vb6 app? 

    but how can i activate my vb6 application from inside Word (as a macro, for example) apart from by sending my application to the command prompt?

    Thanks again,

    Neta.

    Wednesday, June 13, 2007 12:52 PM
  • Yes, you can. 

     

    Depending on how the vb6 code was developed will determine how easy this will be.  The Modules and Class files created in vb6 should be able to be imported directly.   If you're very lucky no further edits to that code will be necessary.

     

    Forms are another matter as the user forms in vba are different from vb6 forms.  Any forms will have to be redrawn in the vba environment.  To make your life easier use the same control names used in vb6, then you can copy the form code from vb6 and paste it in vba.  Be aware there are other significant differences in the way forms operate in vba so you will probably have more edits to make.

     

    Vba does not allow using a form for startup.  If your program operates that way you will have to create a sub as the entry point load/show the form in the sub.  You'll also need to check the References and Components used in the vb6 environment and connect the same in the vba environment.

     

    That should get you started.

     

    HTH.

    Rod Wing

    Thursday, June 14, 2007 1:46 AM