Expected end of line error in middle of codeline - vbscript

I have this simple vbscript, but windows gives me an error when I'm trying to run it. I've never worked with vbscript before and i tried to google, but could figure out why. I'm getting expected end of line error at line 1, character 16. Does anyone see anything?
Also, I'm coding this in notepad and running it by double clicking the file. Is there any other way i should do it?
Dim WithEvents Button As CommandButton
Private Sub Application_StartUp(Cancel As Boolean)
Set Button = CommandButtons.Add("Click me")
End SubPrivate Sub Button_Click()
Msgbox "Clicked", vbInformation
End Sub

Since it was mentioned that notepad is being used for this and looking at the code this certainly appears to be code to add a button to a Form Based application using VBScript in Visual Studio.
VBScript via notepad is not really good at being used for creating GUI dialog boxes and such. A sample code of what would work using Notepad is as such:
toRepeat = InputBox("What should I repeat?", "Echo", "Hello World")
MsgBox toRepeat
Whatever is typed in to the input box will be repeated in this example.
If Visual Studio is not available and there is an interest in building a form based application then it may be possible to create User Forms in VBA via Word or Excel. The following site gives a good tutorial for creating a User Form application in Excel: Excel VBA Userform - Easy Excel Macros
In case there is interest, I recently wrote in to my blog an explanation of the different flavors of VB that are available: VBA vs VBSCRIPT vs Visual Basic
Thanks,
Sean W.

Related

Debugging Excel macro through Microsoft Visual Basic 6.0

I have VB6 code that executes various Excel macros. I set my breakpoints in VB6 and in the the Excel macro, then begin debugging in the VB6 application. When it reaches (a breakpoint) the statement to execute the macro, it skips over the command and continue traversing through my VB6 code. I was expecting it to jump to Excel and began debugging the Excel macro. Is there some configuration or trick, to debug an Excel macro that is being called from a VB6 application?
When VB6 executes the its own code, it allows the programmer using the IDE to set breakpoints and give control to user when the execution has reached those said breakpoints. However, setting breakpoints in Excel will only benefit you if you (the programmer) have executed the macro from Excel interface. Which is not the case when VB6 is in charge of firing up Excel and telling it to execute the macros within the workbook.
Here is what you can do:
migrate all of the macro code to VB6. It is fairly easy, you just need to add few words here and there like this
'In Excel macro:
Activecell.Offset(0,1).Value = "my value"
so if we want to migrate this line of macro to VB6:
'In VB6 you must declare an instance of EXCEL application:
Dim EX as Excel.Application
EX.Workbooks.Open('c:\my_excel_file.xls')
EX.Sheets(0).Activate 'Go to the first sheet
'Then you are back to business:
EX.ActiveCell.Offset(0,1).Value = "my vlaue"
'But don't forget to close and dispose of EX instance afterwards.
EX.Activeworkbook.Close 'Check save options
EX.Quit
Set EX = Nothing
After you have migrated all of the macros functionality to VB6, you can set breakpoints in VB6 and you will have all the control over the execution and will be able to debug the code as you intended to do.

Automatically Confirming a Msgbox in Excel VBA code

I am using ruby a WIN32OLE to automate execution of Excel macros with windows scheduler.
I have all of this working properly. My problem comes with having to run a macro embedded in an excel file of an external party (ie I don't have control of the macro). I can call the macro with no problem, however the code contains user prompts (in the form of Msgbox statements)
In short how do I tell the msgbox "yes" or "ok" via WIN32OLE?
What I have tried
Event handling: The Msgbox does not throw a worksheet event. I would have to think that windows throws an event somewhere for the Msgbox (but I am not skilled enough to capture it with WIN32OLE)
-Threading and Sendkeys: I was also unable to get the msgbox to respond to sendkeys. More problematic was that I was unable to get the threading to work with the WIN32OLE object (it worked fine when I was using puts and sleeps to simulate the macro running.)
It appears I am stuck with copying the code into another workbook and removing the msgbox statments, I don't really want to do that because then it will add maintenance work everytime the business logic of the 3rd party code changes.
Here's one way,
install Autoit
and then do:
au3 = WIN32OLE.new("AutoItX3.Control")
au3.ControlClick("MessageBox Title",'', 'OK')

Word 2003 template with VBA macros that does not run properly in Word 2011 for MAC

As the title says, I've got a word template with macros that does not run properly in the new Word version from Office 2011 for MAC.
The thing which seems to not work properly is the following code:
Sub Document_New()
myForm.Show
End Sub
The same is with Document_Open()
It doesn't seem to run this code on the Mac version.
Does anyone know why this won't work on the Mac, or if there's another way around to emulate the document_open/document_new function?
EDIT: The document is in the .dot format. And I tried to save it to .doc, then the Document_open() worked just fine, so it seems to not be working in the .dot format.. And Document_New() is not running in .doc since its not a new templatefile based on a document..
EDIT 2: Seems like it was a once only with the Document_open on .doc files. I cant make it work again. So weird! The only event I get working, and this is only when using the .doc file format, is Document_Close() - this works everytime...
EDIT 3: This is just getting weirder. I made a new .doc document with the following code:
Private Sub Document_Open()
MsgBox ("BlaBlaBla")
End Sub
The code only runs if the Visual Basic Editor is open BEFORE I close the word file and try to open it again. If I close the Visual Basic Editor and then the word file, and then open the word file; The code is not run.
??
All VB application events are suppressed if you have the VB-editor active, and the current project is not running. It is an intentional behavior, to prevent unwanted code execution, hence not debuggable.
I have used Workbook_Open() (in Excel), and I can only see it working on newly open Excel Xls (xlsm on 2010), from a non-open VB-editor Excel application.
It will work if you have other doc/xls already open, but not if vb-editor is up.
Have you checked whether Macros are allowed? Do you have generated a certificate and setup your application as a trusted source?
I'm having similar issues. It seems that MS removed support for the Document_New and Document_Open functions in the Word object model for Word 2011. See http://mac2.microsoft.com/vb/1033/Word/html/womscChangesBetweenWord2010and2011.htm

Create a user in Active Directory from an Infopath form

I've been searching the web for a while now and still can't seem to find anything useful on this topic.
What I am trying to create is a button in my Infopath form that will create a user account in Active Directory. The code will need to pull through information stored in the fields of the Infopath form.
I have chosen to use Visual Basic, as I believe that is a good language scripting with AD.
I have successfully run this code:
Sub CTRL1_12_OnClick(eventObj)
MsgBox "Output: "
End Sub
which returns: 'Output: ' in a VB dialogue box when I click on my infopath button. However, when I try:
Set objOU=GetObject("LDAP://OU=People,OU=contoso,DC=contoso,DC=com")
Set objUser=objOU.Create("user","CN=Linda Mitchell")
objUser.Put "sAMAccountName","linda.mitchell"
objUser.SetInfo()
All I get is an error message in Infopath:
The following error occurred:
ActiveX component can't create object:
'GetObject' File:script.vbs Line:14
When I put the same code in to notepad, save as a vbs file and then execute is runs fine and create a user in active directory, what I don't understand is why when I run the same code it doesn't work.
I would recommend you check out Richard Mueller's web site - he has tons of VBScript code to do all sorts of AD stuff.
Here's a link to a page showing how to create users - is that of help?? I'm not much of a VBScript programmer, but I know Richard's stuff is usually very good and useful.
Here's his entire list of free VBScript code for you to go rummage about!
I have now found out what the problem was. I needed to set the form to 'full trust' Tools>form options> Security and Trust. I also had to sign the form with my certificate.
Now I am able to run the code from that button.

Referencing functions within VB6 User Controls

I'm having an issue referencing public procedures of User Controls that I've created within a VB6 project.
A simple example (exe), I have a form with a button and a user control:
Option Explicit
Private Sub Command1_Click()
UserControl1.updateMessage ("TIME NOW: " & DateTime.Time)
End Sub
The User Control code is as follows:
Option Explicit
Public Sub updateMessage(ByVal newMessage As String)
Label1.Caption = newMessage
End Sub
This exe compiles and works fine, and when I'm typing updateMessage in the Form, it appears in the intellisense list with the appropriate requirements. The issue I have is when I'm wanting to "go to the definition" of updateMessage, instead of going to the appropriate section of the code within the User Control, the message always returns with:
"Cannot jump to 'updateMessage' because it is in the library 'Unknown1' which is not currently referenced."
where the numbered suffix of "Unkown1" changes from time to time.
It seems that if there were no reference to this procedure, then it would not appear in the intellisense and the project shouldn't compile. When running this with MZTools (though the error appears regardless of this plug-in being installed), I can go into the updateMessage procedure, and use it to find all procedures calling this function, so the link between the two should exist (although I'm not sure if MZTools just finds using a text-matching pattern).
If anyone out there could shed some light on this matter, it would be very much appreciated, and save this poor VB6 developer a lot of hassle!
I have SP6 installed (build 9782) of VB6 and am running XP SP3 on an HP dx2400.
Yes, this is extremely annoying and I'm convinced it's a bug in VB6. I say this because, if you locate the updateMessage method in the object browser and double-click on it, you are taken to the definition. So, the compiler actually does know where the definition is, it just refuses to take you there with Shift+F2.

Resources