PDA

View Full Version : Solved: New to VBA - help with first script



douga
10-18-2006, 06:17 AM
Help!! I'm a programmer, but I'm new to VBA. I'm trying to write a VBScript that is NOT going to run as a macro that will create and save a Word document. When I try to run the following code I get a "variable is undefined" for LinesToPoints and wdAlignParagraphCenter. What am I doing wrong?? :banghead:

Option Explicit

Dim WordObj, letterText, wordTemplate

Set WordObj = CreateObject("Word.Document")
WordObj.Application.Visible = False
WordObj.Activate

'Add Word Document Text Here
With WordObj.Application
.Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
.Selection.ParagraphFormat.LineSpacing = LinesToPoints(1.5)
.Selection.TypeParagraph
.Selection.TypeText _
"Your momma's got a glass eye with a fish in it!!" & vbCrLf _
& "Your momma's got a glass eye with a fish in it!! Line 2"
.Selection.TypeParagraph
.Selection.TypeParagraph
.Selection.TypeParagraph
.Selection.TypeText _
"Your momma's got a glass eye with a fish in it!!" & vbCrLf _
& "Your momma's got a glass eye with a fish in it!! Line 2"

End With

WordObj.SaveAs "c:\filename2.doc"
WordObj.Application.Quit
Set WordObj = Nothing

lucas
10-18-2006, 07:24 AM
I'm trying to write a VBScript that is NOT going to run as a macro that will create and save a Word document.

This runs ok if you enclose it in sub and end sub tags, I don't understand your statement above. I think it needs to be a sub or function to run under vba.

fumei
10-18-2006, 07:54 AM
You will get that error if you do not have a reference to the Word object library.

Either make a reference, or use actual values of the Word properties.

wdAlignParagraphCenter = 1, so

.Selection.ParagraphFormat.Alignment = 1

douga
10-18-2006, 08:04 AM
In other words - this isn't running as a macro inside of Word. This is just a .vbs file on my desktop and I want to be able to double click it and have it create and save the document.

Even if I don't use Subs or Functions this will work if I remove the alignment and linespacing parameters, but when I include them it says that the wdAlignParagraphCenter and LinesToPoints variables haven't been defined (although they aren't variables). But if I take those 2 lines out it works fine.
Option Explicit

Sub Test()

Dim WordObj, letterText, wordTemplate

Set WordObj = CreateObject("Word.Document")
WordObj.Application.Visible = False
WordObj.Activate

'Add Word Document Text Here
With WordObj.Application
' .Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
'.Selection.ParagraphFormat.LineSpacing = LinesToPoints(1.5)
.Selection.TypeParagraph
.Selection.TypeText _
"Your momma's got a glass eye with a fish in it!!" & vbCrLf _
& "Your momma's got a glass eye with a fish in it!! Line 2"
.Selection.TypeParagraph
.Selection.TypeParagraph
.Selection.TypeParagraph
.Selection.TypeText _
"Your momma's got a glass eye with a fish in it!!" & vbCrLf _
& "Your momma's got a glass eye with a fish in it!! Line 2"

End With

WordObj.SaveAs "c:\filename2.doc"
WordObj.Application.Quit
Set WordObj = Nothing

End Sub

Test

douga
10-18-2006, 08:06 AM
You will get that error if you do not have a reference to the Word object library.

Either make a reference, or use actual values of the Word properties.

wdAlignParagraphCenter = 1, so

.Selection.ParagraphFormat.Alignment = 1
How do I add a reference to the library? - I know how to add in Visual Basic Editor (for Macros), but not in notepad using Vbscript - if anything where could I find a reference to values for these library variables?

lucas
10-18-2006, 08:31 AM
see ken puls kb entry on finding out which reference you are looking for. Maybe it will give you some insights:
http://vbaexpress.com/kb/getarticle.php?kb_id=278

fumei
10-18-2006, 11:04 AM
Steve, that is not going to help him with writing code in Notepad for VBScript.

Doug:
Even if I don't use Subs or Functions this will workReally? I am quite surprised to hear that. Seems to me that you ARE using a Sub. Could you post some code that will work without being a Sub or Function? I don't know VBScript, so maybe it can do that - but....

As for references, I am not sure, but there may be an article/thread here regarding adding a reference dynamically. You could do that. Or, search Microsoft for their document listing Word constants, and use those.

Oh....what the heck. It will probably take you a while to find that document (wdConstants.doc). I know it took me a while. So....here you go. ALL the constants for Word. Sorry...only up to XP (2002).

I hope it helps.

douga
10-18-2006, 11:45 AM
Woo Hoo! Thanks for posting that - and yes, it wouldn't taken me all day to find. I was just thinking as I was reading your post "Microsoft Documentation Sucks!!"

Basically if you copy and paste my first example into notepad and save it as somthing VBS it will run if you comment out the following lines.Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
.Selection.ParagraphFormat.LineSpacing = LinesToPoints(1.5)

I have been searching all day to see if I can make a reference, but it doesn't seem to be happening.

I think the best route for me will be to create a new doc form a template and then find/replace
values - if I can get the find/replace to work.

fumei
10-18-2006, 12:39 PM
I think the best route for me will be to create a new doc form a template and then find/replace
values I have no idea what you mean by that.

douga
10-18-2006, 01:58 PM
I have no idea what you mean by that.

This is what I meant and this is my solution I guess. Create a .dot file that will be my template on a network share. Create a new document from it and replace some place holder text (i.e. <<Last Name>>) with real values.

Option Explicit
'Declare some vars
Dim objWord, objDoc

Set objWord = CreateObject("Word.Application")
objWord.Application.Visible = True
Set objDoc = objWord.Documents.Add("C:\Test.dot")

'Add Word Document Text Here
'objWord.Selection.TypeParagraph
'objWord.Selection.TypeText _
'"Your momma's got a glass eye with a fish in it!!" & vbCrLf _
'& "Your momma's got a glass eye with a fish in it!! Line 2"

With objWord.Application.Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
'First instance is text to be replaced
'Second instance is new text
.Execute "glass", False, True, False, False, False, True, 1, True, "steel",2
End With

With objWord.Application.Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
'First instance is text to be replaced
'Second instance is new text
.Execute "eye", False, True, False, False, False, True, 1, True, "leg",2
End With

objDoc.SaveAs("c:\filename2.doc")
objWord.Quit

Set objDoc = Nothing
Set objWord = Nothing

fumei
10-18-2006, 02:23 PM
Well...whatever. Since I still don't have a really good understanding of exactly what you are doing....whatever works for you.