Forum Home
Press F1
 
Thread ID: 38283 2003-10-02 10:53:00 Help with vbscript Mike (15) Press F1
Post ID Timestamp Content User
179840 2003-10-02 10:53:00 I'm trying to modify a vbscript in an application.

What the script does is when I type " Name: " into a text box it populates the text box with the current logged in users name, so for me it'd say " Name: Mike " . When I type " Date: " it includes the date, " Date: 2 October 2003 " . When I type " File: " it displays the location of the current file like this " File: c:\windows\win.com " .

Now what I want it to do is do all three at once when I type something like " Info: " or similar.

Here's the script

Option Explicit
Private WithEvents m_pActiveViewEvents As Map

Private Declare Function GetUserName Lib " advapi32.dll " Alias " GetUserNameA " _
(ByVal lpBuffer As String, nSize As Long) As Long

Private Function MxDocument_BeforeCloseDocument() As Boolean
UnSetEvents
End Function

Private Function MxDocument_OpenDocument() As Boolean
SetEvents
End Function

Public Sub SetEvents()
Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument
Set m_pActiveViewEvents = pMxDoc.PageLayout
End Sub

Public Sub UnSetEvents()
Set m_pActiveViewEvents = Nothing
End Sub


Private Sub m_pActiveViewEvents_ViewRefreshed(ByVal pView As IActiveView, _
ByVal phase As esriViewDrawPhase, _
ByVal data As Variant, _
ByVal Envelope As IEnvelope)

Dim pGC As IGraphicsContainer
Set pGC = pView
pGC.Reset
Dim pElement As IElement
Set pElement = pGC.Next
Do While Not pElement Is Nothing
If TypeOf pElement Is ITextElement Then
Dim pTextElement As ITextElement
Set pTextElement = pElement
If UCase(Mid(pTextElement.Text, 1, 5)) = " DATE: " Then
pTextElement.Text = " DATE: " & Date
End If
If UCase(Mid(pTextElement.Text, 1, 5)) = " NAME: " Then
pTextElement.Text = " NAME: " & UserName
End If
If UCase(Mid(pTextElement.Text, 1, 5)) = " FILE: " Then
pTextElement.Text = " FILE: " & GetMXDName
End If
End If
Set pElement = pGC.Next
Loop

End Sub

Function UserName() As String
Dim UN As String * 260
GetUserName UN, Len(UN)
UserName = UN
End Function


Function GetMXDName() As String
' each project in the VBE has a pathname
Dim pVBProj As VBProject
Set pVBProj = ThisDocument.VBProject
' loop through all the projects
Dim l As Long, strPath As String
For l = 1 To pVBProj.VBE.VBProjects.Count ' 1 based
On Error Resume Next
strPath = pVBProj.VBE.VBProjects.Item(l).FileName
If Err <> 0 Then
strPath = " "
Err.Clear
End If
If InStr(1, LCase(strPath), " .mxd " ) > 0 Then
GetMXDName = strPath
Exit Function
End If
Next l
GetMXDName = " Untitled.mxd "
End Function


I tried adding a bit to the part that tells it what to display, like this

If UCase(Mid(pTextElement.Text, 1, 5)) = " INFO: " Then
pTextElement.Text = " NAME: " & UserName & " DATE: " & Date & " FILE: " & GetMXDName
End If

However, that didn't work - it just displayed the first part (Name).

Anyone know what I've done wrong? I know very little (read " none " ) VB :)

Thanks

Mike.
Mike (15)
179841 2003-10-02 14:50:00 hiya,
Although VB isn't my forté, I'll give it a try . . .
I'm having a bit of trouble coming to terms of where you are getting some of the variables from (maybe i'm just stupid and tired), but for

pTextElement . Text = " DATE: " & Date

what is the Date variable, is it some global i havn't noticed or has it been passed down by function call?

Private Sub m_pActiveViewEvents_ViewRefreshed(ByVal pView As IActiveView, _ ByVal phase As esriViewDrawPhase, _ ByVal data As Variant, _ ByVal Envelope As IEnvelope)

Has all the veriables etc been passed down somewhere in here? It's just I can't see Date accessed anywhere else in the code . Thinking about it, maybe the 'data' Variant is what it is contained in, possibly it would be better to access by reference? i . e . instead of ByVal, ByRef . so it passes a reference to the memory address of the variable, rather than the variable data so the actual data can be editied from within the function (ala global variables) .

Maybe you havn't given Date any data for them to access or maybe i'm just being stuipid, because i havn't seen half of this sort of code before ;) I'm just strugling to come to grips with what a lot of it means :D Please don't laugh, i'm just trying to help :)

- David
DangerousDave (697)
179842 2003-10-02 21:01:00 Dave,

I didn't write the script, I'm just trying to modify it to work better for me. But when I read through it I noticed the same about the date, so I'm guessing it's some global variable that's just called by "Date", but really I have no idea, I don't know VB at all :)

Mike.
Mike (15)
179843 2003-10-02 21:52:00 Hi Mike this is doing an API call which is a bit out of my league at the moment. However, the following part of the code seems to loop through the different user elements and put the result in pTextElement.Text. So you could either have the Name and File parts going to separate text boxes otr put them in the same text box and line feed down a line.


Do While Not pElement Is Nothing
If TypeOf pElement Is ITextElement Then
Dim pTextElement As ITextElement
Set pTextElement = pElement
If UCase(Mid(pTextElement.Text, 1, 5)) = " DATE: " Then
pTextElement.Text = " DATE: " & Date
End If
If UCase(Mid(pTextElement.Text, 1, 5)) = " NAME: " Then
pTextElement.Text = " NAME: " & UserName
End If
If UCase(Mid(pTextElement.Text, 1, 5)) = " FILE: " Then
pTextElement.Text = " FILE: " & GetMXDName
End If
End If
Set pElement = pGC.Next
Loop


Try this to put data all in the same text box. Not sure if it will work though because of the If then structure. I think your change didn’t work because it needs to loop through to get the elements as Name etc must mean something in the dll it's calling. btw Chr(10) is a line feed.


Do While Not pElement Is Nothing
If TypeOf pElement Is ITextElement Then
Dim pTextElement As ITextElement
Set pTextElement = pElement
If UCase(Mid(pTextElement.Text, 1, 5)) = " DATE: " Then
pTextElement.Text = " DATE: " & Date & chr(10)
End If
If UCase(Mid(pTextElement.Text, 1, 5)) = " NAME: " Then
pTextElement.Text = pTextElement.Text & " NAME: " & UserName & chr(10)
End If
If UCase(Mid(pTextElement.Text, 1, 5)) = " FILE: " Then
pTextElement.Text = pTextElement.Text & " FILE: " & GetMXDName
End If
End If
Set pElement = pGC.Next
Loop
parry (27)
179844 2003-10-02 22:14:00 Heres what I would do Mike.
Delcare a new variable, something like " Dim InfoString "
Then in your if statement create the string that you want to return. For example

If UCase(Mid(pTextElement.Text, 1, 5)) = " INFO: " Then
InfoString = " NAME: " & UserName & " DATE: " & Date & " FILE: " & GetMXDName

Then just return the string that you have already created or concatenated

eg

pTextElement.Text = InfoString
end if

This way you've already made a single string to return.
Let me know how you go.

B.
Barnabas (4562)
179845 2003-10-03 01:42:00 Parry, it didn't work :) it just looped the same thing over and over, so I'd end up with a box full of "Name: Mike Name: Mike Name Mike" etc. :D

I have a feeling my text box might only be one line, so a line feed wouldn't be practical. Its more a title box rather than a text box I guess.

I don't mind whether the results all end up in one box or in three (they're currently in three as I have to create three boxes for the script to work properly).

Thanks for your suggestion though!

Mike.
Mike (15)
179846 2003-10-03 01:43:00 Hi Barnabas,

thanks for your reply, but would you be able to show me how I'd do it, as I don't konw VB at all, so I wouldn't know how to do what you said (let alone understand it)! :D

Cheers,

Mike.
Mike (15)
179847 2003-10-03 02:27:00 Not much into vbscript...

but is & the correct operator to use? not +?
Kame (312)
179848 2003-10-03 02:29:00 Should explain more...

If UCase(Mid(pTextElement.Text, 1, 5)) = " INFO: " Then pTextElement.Text = " NAME: " & UserName + " DATE: " & Date... etc
Kame (312)
179849 2003-10-03 06:10:00 > Should explain more...
>
> If UCase(Mid(pTextElement.Text, 1, 5)) = " INFO: "
> Then pTextElement.Text = " NAME: " &
> UserName + " DATE: " & Date... etc

Kame, I tried what you suggested, but it didn't like it (asked me to debug when I tried it out) - I have a feeling though that the & is correct.
Thanks though :) I really do need to do a VB course.

Mike.
Mike (15)
1 2 3 4