|
-
Feb 21st, 2005, 06:10 PM
#1
Thread Starter
Junior Member
How do VB for Word, Excel and/or VBScript work together? [Resolved]
I need help understanding how object models interact with each other. If I open Excel using VBScript or VB in Word, how do I know what objects and methods are available to me?
Should I be able to use any and all of the Excel methods in VBScript/Word once I create an Excel object (under appropriate Excel objects, of course)?
P.S. Posted elsewhere but realized this is the place to ask.
Last edited by daiei27; Feb 22nd, 2005 at 05:25 PM.
Reason: resolved by RobDog888
-
Feb 21st, 2005, 06:40 PM
#2
Re: How do VB for Word, Excel and/or VBScript work together?
Probably the best solution is to check the help file for each office app in question. They usually have all
methods/functions/properties available when opened either with VB or VB Script. One of the main issues
is to use the VB constants value instead of the name of the constant. For ex. Const xlCellTypeLastCell = 11
so you would use 11 in your vbscript when referencing the method that takes it as a parameter.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Feb 22nd, 2005, 10:20 AM
#3
Thread Starter
Junior Member
Re: How do VB for Word, Excel and/or VBScript work together?
If all the objects/methods should be available when I create an Excel object, why doesn't VBScript or VB for Word seem to recognize the TextToColumns method from Excel?
I wish there was an exists() function or something to check if a object/method is available.
-
Feb 22nd, 2005, 11:42 AM
#4
New Member
Re: How do VB for Word, Excel and/or VBScript work together?
One thing you can do is poke around in the Object Browser that's available to VB for Word. It's under the view menu in the VB editor, but you can also access it by hitting F2. That's one way to find a lot of the information you are looking for. However, if you don't have the Excel reference added, you're not going to find what you are looking for. Check your references and if you have the Excel one, check out the Object Browser to find what you need.
-
Feb 22nd, 2005, 12:09 PM
#5
Re: How do VB for Word, Excel and/or VBScript work together?
I think your talking about Intellisense? Like when you start typing an object and you press
the dot and a popup menu comes up with the available methods and properties? If so, that
wil only be available if you early bind and add a reference to Excel in your project. After your
done you can change it back to later binding and remove the reference(s).
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Feb 22nd, 2005, 01:48 PM
#6
Thread Starter
Junior Member
Re: How do VB for Word, Excel and/or VBScript work together?
I guess Intellisense does sort of do that for you, but not if you're developing from another app.
My real problem is I already know how to use the TextToColumns method in the VB editor for Excel, but when I try to use it on an Excel application opened from VB for Word or VBScript , the method isn't recognized.
In other words, the following code will work in Excel.
VB Code:
wsheet.Range("A1").Value = "a, b, c"
wsheet.Range("A1").TextToColumns DataType:=xlDelimited, Comma:=True
However, running these statements in VBScript or VB for Word gives me errors like it doesn't recognize TextToColumns as a method. That's why I asked if creating an Excel object imports Excel's object model or if there was a way to test if the method was available.
I'd appreciate any help I can get. Thanks for the replies!
-
Feb 22nd, 2005, 01:52 PM
#7
Re: How do VB for Word, Excel and/or VBScript work together?
Oh your opening Excel from Word. Word does not have a reference for Excel. You need to add one.
Then it should be recognized.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Feb 22nd, 2005, 02:04 PM
#8
Thread Starter
Junior Member
Re: How do VB for Word, Excel and/or VBScript work together?
Actually, I was just reading another thread involving a constant reference and what you said earlier hit me. Maybe it's not the method but the constant in the call that is the problem. I just realized the 'xlDelimited' is a constant and probably can't be resolved like you said. I'm gonna go try to subsitute the value and let you know how it goes.
-
Feb 22nd, 2005, 02:25 PM
#9
Re: How do VB for Word, Excel and/or VBScript work together?
With VBScript you can not use the named constants. You need to use the actual numeric values.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Feb 22nd, 2005, 03:11 PM
#10
Thread Starter
Junior Member
Re: How do VB for Word, Excel and/or VBScript work together?
What can I say, you were right. 
However, the fix works in Word, but I still can't get it to work in VBScript, which is where I need it! The following bold line gives me an 'Expected Statement' error.
VB Code:
Option Explicit
Dim objExcel
Set objExcel = CreateObject("Excel.Application")
' Make Excel visible'
objExcel.Visible = True
'Open template'
Dim wb
Set wb = objExcel.Workbooks.Open("C:\dummy.xls")
'Import data'
wb.Sheets(2).Range("A8").Value = "a, b, c"
[B]wb.Sheets(2).Range("A8").TextToColumns DataType:=1, Comma:=True[/B]
' Close to prompt save and then quit.'
wb.close
If objExcel.Workbooks.Count = 0 Then
objExcel.Quit
End If
So unless something else is wrong, doesn't it look like VBScript does not have access to the TextToColumns method? I saw someone mention you can bind Excel earlier on the dim statement to make the application's methods available in other versions of VB, but I don't think you can do this in VBScript.
-
Feb 22nd, 2005, 03:27 PM
#11
Re: How do VB for Word, Excel and/or VBScript work together?
This worked for me on a blank sheet/book.
VB Code:
Private Sub Worksheet_Activate()
Sheets(2).Range("A8").Value = "a, b, c"
Sheets(2).Range("A8").TextToColumns DataType:=1, Comma:=1
End Sub
You should also dim your vars as Object instead of Variant
VB Code:
Option Explicit
Dim objExcel As Object
Set objExcel = CreateObject("Excel.Application")
' Make Excel visible'
objExcel.Visible = True
'Open template'
Dim wb As Object
Set wb = objExcel.Workbooks.Open("C:\dummy.xls")
'Import data'
wb.Sheets(2).Range("A8").Value = "a, b, c"
wb.Sheets(2).Range("A8").TextToColumns DataType:=1, Comma:=1
' Close to prompt save and then quit.'
wb.close
If objExcel.Workbooks.Count = 0 Then
objExcel.Quit
End If
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Feb 22nd, 2005, 03:45 PM
#12
Thread Starter
Junior Member
Re: How do VB for Word, Excel and/or VBScript work together?
I can get it to work in Excel and Word just fine. It's VBScript run in Internet Explorer that I can't do it in. And from what I've seen so far the 'As [datatype]' declaration doesn't seem to be an option in VBScript. My experience is mainly in VB for word so I could be wrong.
-
Feb 22nd, 2005, 04:26 PM
#13
Re: How do VB for Word, Excel and/or VBScript work together?
Maybe its the parameter labels that are messing it up. Try it in the correct order with the correct parameter types.
expression.TextToColumns(Destination, DataType, TextQualifier, ConsecutiveDelimiter, Tab, Semicolon, Comma, Space, Other, OtherChar, FieldInfo, DecimalSeparator, ThousandsSeparator, TrailingMinusNumbers)
VB Code:
Sheets(2).Range("A8").TextToColumns , 1, , , , , 1
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Feb 22nd, 2005, 05:24 PM
#14
Thread Starter
Junior Member
Re: How do VB for Word, Excel and/or VBScript work together?
It worked! I seriously couldn't have done it without your help. I never would have guessed the crappy errors I was getting was related to the values and not the method. Thanks a million, Rob.
I noticed you answer just about everybody that posts here. Do you work for vbforums.com? I mean, you certainly went out of your way for me...
-
Feb 22nd, 2005, 06:02 PM
#15
Re: How do VB for Word, Excel and/or VBScript work together? [Resolved]
Its not a problem! Thanks.
No I dont work for them, but I do volunteer my time. I Moderate this (VBA) Forum and Co-Moderate the Application Deployment Forum.
When you have been involved with the Forums for a lengthy time and have a certain level of expertise in a particular area
you can ask to become a Moderator of one of the Forums that is your speciality (depending on availabiity).
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Feb 23rd, 2005, 12:26 PM
#16
Thread Starter
Junior Member
Re: How do VB for Word, Excel and/or VBScript work together? [Resolved]
If you ever need help with XML-related stuff like XSLT2.0 or web stuff like Perl/Javascript/CSS, send me an email. I'd be glad to return the favor.
FYI, I researched some and apparently VBScript doesn't allow for named arguments like in regular VB. That's why using just the values worked.
-
Feb 23rd, 2005, 12:42 PM
#17
Re: How do VB for Word, Excel and/or VBScript work together?
 Originally Posted by RobDog888
With VBScript you can not use the named constants. You need to use the actual numeric values. 
I thought you saw my post?
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Feb 24th, 2005, 10:44 AM
#18
Fanatic Member
Re: How do VB for Word, Excel and/or VBScript work together?
 Originally Posted by RobDog888
One of the main issues
is to use the VB constants value instead of the name of the constant. For ex. Const xlCellTypeLastCell = 11
so you would use 11 in your vbscript when referencing the method that takes it as a parameter.
FYI:
What I do with this particular issue is to create my own constants for easy readibility - such as:
Code:
Public Const xlCellTypeLastCell As Long = 11
Public Const xlWhatever As Long = 12
etc..
This has been a great time saver when doing information transfer.
-
Feb 24th, 2005, 11:08 AM
#19
Thread Starter
Junior Member
Re: How do VB for Word, Excel and/or VBScript work together? [Resolved]
I did, but I think named constants are different from named arguments. It's argument=value, where value can be a constant. For example, I think all of these are valid in VB but only the last one works in VBScript:
named arguments and named constants:
xxx.method argument:=xlConstant
named arguments WITHOUT named constants (which is what I thought you meant):
xxx.method argument:=1
unnamed arguments with named constants:
xxx.method xlConstant
unnamed arguments without named constants:
xxx.method 1
Overkill, I know. Just wanted to make sure everything was straight.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|