-
Re: Changing a macro prompt from a message box to a userform
JOHN GEORGE May 15, 2015 11:15 AM (in response to David Dewey)David,
Can you post the macro here?
Someone can have a look and identify the problem.
-
Re: Changing a macro prompt from a message box to a userform
David Dewey May 15, 2015 11:56 AM (in response to JOHN GEORGE)John,
The portion of the macro that contains the message boxes is posted above the image. I can post the entire macro here, but I'm not sure it's necessary. There is a lot of other stuff going on that isn't pertinent to the form problem I've got going on.
-
-
Re: Changing a macro prompt from a message box to a userform
Deepak Gupta May 15, 2015 1:09 PM (in response to David Dewey)You can use swCustProp.Get4 to get the values and populate the form.
-
Re: Changing a macro prompt from a message box to a userform
David Dewey May 18, 2015 7:52 AM (in response to Deepak Gupta)Where in the form layout do I put this though? I'm already pulling in this as a variable earlier in my code and using it for other things so I could pass this directly to the userform, but I am unsure where or how to accomplish this.
-
Re: Changing a macro prompt from a message box to a userform
Deepak Gupta May 18, 2015 7:39 AM (in response to David Dewey)It would hard to tel without looking at the macro codes. Can you post the complete macro along with a sample file.
-
Re: Changing a macro prompt from a message box to a userform
David Dewey May 18, 2015 7:51 AM (in response to Deepak Gupta)-
Re: Changing a macro prompt from a message box to a userform
Deepak Gupta May 18, 2015 7:53 AM (in response to David Dewey)You can also attach them to this forum. Check #18 here: Forum Posting
Could you also attach a sample file have the properties you want to import values of.
-
Re: Changing a macro prompt from a message box to a userform
David Dewey May 18, 2015 8:12 AM (in response to Deepak Gupta)Sorry Deepak. I missed the forum rules for attaching. Hopefully this method meets the forum rules.
-
sample.zip 274.9 KB
-
Re: Changing a macro prompt from a message box to a userform
Paul Marsman May 19, 2015 8:35 AM (in response to David Dewey)David,
I know this isn't exactly what you are asking for here but I thought I'd mention it anyway. I'm not sure of the current version of your macro but I downloaded the one above and stepped through it and noticed that you have a call to the UserName function 4 times for each "UserName" you have (32X currently). This really should be changed AND you don't currently have any setup incase the UserName is not in your list. You set the UserName to "-" in the function, but you never use it in that setup either. I would do as the snip below if it was mine:
(I changed the function name so it isn't the same as what you are looking for, much more clear as to when you are looking to call a function and when you are evaluating a value)
Dim strUserName As String
Function getUserName()
Dim myShell
Dim content
Dim value
Set myShell = CreateObject("WScript.Shell")
For Each value In myShell.Environment("PROCESS")
content = Split(value, "=")
If content(0) = "USERNAME" Then strUserName = content(1)
'Debug.Print value
Next
Set myShell = Nothing
End FunctionThen in your Sub main():
getUserName
'Debug.Print strUserNameSelect Case UCase(strUserName)
Case "BAI"
Engineer = "B.INGALLS"
Initials = "BAI"
Case "SEA"
Engineer = "S.ALLAN"
Initials = "SEA"
Case "DLD"
Engineer = "D.DEWEY"
Initials = "DLD"
Case "JJG"
Engineer = "J. GODZIK"
Initials = "JJG"
Case "GS"
Engineer = "G.SMITH"
Initials = "GS"
Case "DEJ"
Engineer = "D.JOHNSON"
Initials = "DEJ"
Case "JJH"
Engineer = "J.HABEL"
Initials = "JJH"
Case "DAJ"
Engineer = "D.ALLORE"
Initials = "DAJ"
Case Else
' strUserName = "-" 'need something for this strUserName, and something if no case is matched
End Selectthis way you are getting the value ONCE, and using the select case to run all possible options. This will be much faster and easier to debug/update in the future. Hope this helps.
-
-
-
-
Re: Changing a macro prompt from a message box to a userform
Paul Marsman May 18, 2015 7:51 AM (in response to David Dewey)David,
You will just need to take the values you are getting from the active document property(ies) and set the value(s) of your textbox(es). Are you just saying that you are unsure how to set the value of existing items on a userform?
Paul
-
Re: Changing a macro prompt from a message box to a userform
David Dewey May 18, 2015 7:52 AM (in response to Paul Marsman)Paul,
This is what I'm saying. Unsure how to set them on the userform.
-
Re: Changing a macro prompt from a message box to a userform
Paul Marsman May 18, 2015 8:03 AM (in response to David Dewey)I'm sure Deepak can show you exactly where and how to put this but the call is something like this
UserForm1.TextBox1.Value = "This value was set at runtime"
Hope that helps,
Paul
-
Re: Changing a macro prompt from a message box to a userform
David Dewey May 18, 2015 8:10 AM (in response to Paul Marsman)Paul,
That does work, but it doesn't show up in that textbox until after I click OK. Should I be refreshing the userform somehow or doing something else to get it to show up?
-
Re: Changing a macro prompt from a message box to a userform
Deepak Gupta May 18, 2015 8:30 AM (in response to David Dewey)I could get the macro to run properly as I think it is still under WIP.
Edit you Userform and add these codes to populate the two boxes. Add additional lines as required.:
Private Sub UserForm_Initialize()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swCustPropMgr = swModel.GetActiveConfiguration.CustomPropertyManager
Dim val1 As String
Dim val2 As String
swCustPropMgr.Get3 "DESCRIPTION", False, val1, val2
txtDescription.Text = val1
Dim SWGrade As String
swCustPropMgr.Get3 "GRADE", False, val1, val2
txtGrade.value = val1
End Sub
-
Re: Changing a macro prompt from a message box to a userform
David Dewey May 18, 2015 9:30 AM (in response to Deepak Gupta)Sorry Deepak, I did have it open previously when I sent it to you.
I've added these to the user form code prior to the previous code but I don't see anything.
-
Re: Changing a macro prompt from a message box to a userform
Deepak Gupta May 18, 2015 10:25 AM (in response to David Dewey)I've commented out few lines which were not required (not sure) and also some others which were not working for me.
-
Re: Changing a macro prompt from a message box to a userform
David Dewey May 18, 2015 11:17 AM (in response to Deepak Gupta)I appreciate you trying, but I am still not able to complete the script.
The lines that you commented out shouldn't be necessary. My original thought was to pass those variables from the main to the form and use those to prepopulate the text fields.
Regardless, I'm getting an error on the line where I am showing my userform. "Object variable or With block variable not set".
Any thoughts? I do appreciate your help with this!
-
Re: Changing a macro prompt from a message box to a userform
Deepak Gupta May 18, 2015 3:53 PM (in response to David Dewey)Macro seems to work fine for me in SW 2014 and 2015 (check attached video)
In case you're running a different version then update/correct the macro reference as suggested here (don't worry on the fact that video is for fixing missing library error but check the process to update your macro library): Fix-Update SOLIDWORKS Macro References
-
Populate Form.zip 1.4 MB
-
Re: Changing a macro prompt from a message box to a userform
David Dewey May 19, 2015 7:06 AM (in response to Deepak Gupta)ahhh... maybe that is where I wasn't clear. I am running this macro on a drawing, not a part. Unsure if this makes a large difference or not? Running it on the part seems to work. I'm running SWX 2014 SP2.0.
Edit: I should note that this does also work for me at the part level.
-
Re: Changing a macro prompt from a message box to a userform
JOHN GEORGE May 19, 2015 6:53 AM (in response to David Dewey)David,
Still it is not very clear what exactly the macro is doing on the part and the drawing.
From Deepak's video, it reads the property values from the file and showing it in the macro form.
In either case, reading a value or writing to the file properties, I would suggest to use property tab builder. (Have you tried this before?)
It has more flexibility and you can read, write or modify the file properties. You can even populate the values from an excel file or access data base
-
Re: Changing a macro prompt from a message box to a userform
David Dewey May 19, 2015 6:57 AM (in response to JOHN GEORGE)Thank you John,
I have explored property builder. Unfortunately this does not provide everything that my macro is doing. I'm doing many other maintenance items with this macro. The direction was one button that gets run on a new document that handles A,B,C,D,E... what you are seeing is only A of these steps.
-
Re: Changing a macro prompt from a message box to a userform
Deepak Gupta May 19, 2015 12:30 PM (in response to David Dewey)Try this one.
-
Re: Changing a macro prompt from a message box to a userform
David Dewey May 19, 2015 2:45 PM (in response to Deepak Gupta)Thank you Deepak... this does indeed pull in the properties to my form, however when it jumps back to the main script, it fails when trying to set the appropriate property. I've tried many changes but I'm not sure of what I'm doing because of my weak VBA programming skills. I do appreciate the help you've given me up to this point but I understand if you would rather not assist any further as I've already used up quite a bit of your time!
-
Re: Changing a macro prompt from a message box to a userform
Deepak Gupta May 19, 2015 2:50 PM (in response to David Dewey)David if you can give clean up the codes (removing all unwanted stuff) plus add what Paul has suggested then it would be easier to debug and help further.
-
Re: Changing a macro prompt from a message box to a userform
David Dewey May 19, 2015 3:40 PM (in response to Deepak Gupta)Deepak,
Interestingly enough, once I went through all of my code, cleaned it up and better documented it, I figured I would move some pieces of it around to better group what was happening, and now it all is working properly.
I just need to get my option buttons to update the one field and I'll be all set! Going to poke around on my own for a bit and see if I can solve it myself.
I greatly appreciate your help!
-
-
-
-
-
-
Re: Changing a macro prompt from a message box to a userform
Deepak Gupta May 19, 2015 7:09 AM (in response to David Dewey)OK, will check with your drawing later.
-
-
-
-
-
-
-
-
-
-
-
-