Results 1 to 4 of 4

Thread: Compile error: User-defined type not defined [SOLVED - Using Bing Chat]

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2017
    Posts
    17

    Compile error: User-defined type not defined [SOLVED - Using Bing Chat]

    I used the macro wizard to make this, and don't really know the code. I can figure it out sometimes.

    I'm trying to tack on this first part, to my VB that works (below), to see if Firefox is open or not: If open, stop and continue. If not, open it.

    But it gives me this error for "Dim p() As Process" . . . Compile error: User-defined type not defined

    Any idea why?

    ********************************************
    ********************************************
    ********************************************

    Sub Open_a_Bookmark(control As IRibbonControl)

    ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


    ' See if Firefox is open. If not, open it, then proceeed

    Dim p() As Process

    Private Sub CheckIfRunning()
    p = Process.GetProcessesByName("Firefox")
    If p.Count > 0 Then

    ' Process is running

    End If

    Else

    ' Process is not running

    ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    ' I KNOW THAT THIS PART WORKS:

    ' Set the Path variable equal to the Path of the program

    Dim x As Variant
    Dim Path As String

    Path = "C:\Program Files (x86)\Mozilla Firefox\firefox.exe -chrome chrome://browser/content/places/places.xul"
    x = Shell(Path, vbNormalFocus)

    ' If I have the TimeValue at 1 second, sometimes that's too quick, and it pages down one screen within the spreadsheet.
    ' The tilde means "Enter"
    ' https://msdn.microsoft.com/en-us/lib...ffice.10).aspx
    ' This must be the slowest way to get there.
    ' 10/18/19 If I use WScript, it looks like I can make it 1 second

    Application.Wait (Now + TimeValue("0:00:01"))

    ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    End If

    End Sub

    ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


    ' Open a Bookmark in Firefox . . . Firefox has to be open for this to work

    ' Go to the Row of the bookmark you want, then click this button.
    ' It automatically goes to the URL column, and copies it.
    Cells(ActiveCell.Row, "U").Activate
    ActiveCell.copy

    ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    ' Open a new tab in Firefox with Ctrl+T
    ' AppActivate "Mozilla Firefox"
    ' SendKeys ("^t"), True

    ' Sometimes you have to click this macro again, to get it to work, because the "paste" doesn't get to Firefox.
    ' Give it a second before pasting
    ' Application.Wait (Now + TimeValue("00:00:01"))

    ' The focus defaults to the Address Bar. Paste the URL / Enter
    ' SendKeys ("^v~"), True

    ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    AppActivate "Mozilla Firefox"
    Application.Wait (Now + TimeValue("00:00:01"))

    Set WshShell = CreateObject("WScript.Shell")
    WshShell.SendKeys "^t", True

    Application.Wait (Now + TimeValue("00:00:01"))

    Set WshShell = CreateObject("WScript.Shell")
    WshShell.SendKeys "^v~", True

    ' If getting rid of the marching ants, you have to give it a second,
    ' or it doesn't work
    ' or Open_Bookmark / Paste - does not complete
    Application.Wait (Now + TimeValue("00:00:01"))
    Selection.Borders(xlEdgeBottom).LineStyle = xlNone

    ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    ' See the bottom of "Process_Bookmarks" for details. Used at the end of macros, when necessary.

    ' SendKeys "{NUMLOCK}", True

    ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    End Sub
    Last edited by sludge7051-x; May 21st, 2023 at 12:31 PM. Reason: Solved

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,538

    Re: Compile error: User-defined type not defined

    Bottom line... it doesn't know what hte type "Process" is ... you need to add a reference to it.
    Or import it ...


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2017
    Posts
    17

    Process.GetProcessesByName()

    It looks like I got it from this page:

    VB.NET: Check To See If A Process Is Running . . . https://social.msdn.microsoft.com/Fo...ess-is-running

    Per this page: What Process.GetProcessesByName() does is indeed to see if there are any processes that run with a specified name, how many etc.

    If I run this macro, it says: "Compile error: User-defined type not defined", and this is highlighted: p() As Process

    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

    Sub Open_Firefox()

    ' Open Firefox

    Dim p() As Process

    Private Sub CheckIfRunning()
    p = Process.GetProcessesByName("Firefox")
    If p.Count > 0 Then
    ' Process is running
    Else
    ' Process is not running
    End If

    End Sub

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Oct 2017
    Posts
    17

    Re: Compile error: User-defined type not defined

    This is exciting! I asked Bing Chat, and with some slight modifications, I got it to work:

    Q: Using VBA 7.0, what is the code for a macro that 1.) Checks to see if Firefox is open, 2.) If Firefox is open, then stop, 3.) If Firefox is not open, open Firefox, and then stop

    A: This code uses Windows Management Instrumentation (WMI) to query for processes running on the computer. It sets the process name to check for as “firefox.exe” and the computer name to check on as “.” (local computer). It then queries for processes with that name and checks if any are running. If Firefox is not running, it starts it up using the objShell.Run method1.

    Attachment 187684
    Attachment 187685
    Attachment 187686

    Not sure what it's doing with my screenshots. Here they are on imgur . . . https://imgur.com/a/wt6uiKu
    Last edited by sludge7051-x; May 21st, 2023 at 12:24 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width