Results 1 to 19 of 19

Thread: How do VB for Word, Excel and/or VBScript work together? [Resolved]

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2005
    Posts
    16

    Resolved 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

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2005
    Posts
    16

    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.

  4. #4
    New Member
    Join Date
    Feb 2005
    Location
    Wilmington DE
    Posts
    5

    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.

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Feb 2005
    Posts
    16

    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:
    1. wsheet.Range("A1").Value = "a, b, c"
    2. 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!

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Feb 2005
    Posts
    16

    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.

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Feb 2005
    Posts
    16

    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:
    1. Option Explicit
    2. Dim objExcel
    3. Set objExcel = CreateObject("Excel.Application")
    4. ' Make Excel visible'
    5. objExcel.Visible = True
    6.  
    7. 'Open template'
    8. Dim wb
    9. Set wb = objExcel.Workbooks.Open("C:\dummy.xls")
    10.                  
    11. 'Import data'
    12. wb.Sheets(2).Range("A8").Value = "a, b, c"
    13. [B]wb.Sheets(2).Range("A8").TextToColumns DataType:=1, Comma:=True[/B]
    14.  
    15. ' Close to prompt save and then quit.'
    16. wb.close
    17. If objExcel.Workbooks.Count = 0 Then
    18.     objExcel.Quit
    19. 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.

  11. #11
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How do VB for Word, Excel and/or VBScript work together?

    This worked for me on a blank sheet/book.
    VB Code:
    1. Private Sub Worksheet_Activate()
    2.     Sheets(2).Range("A8").Value = "a, b, c"
    3.     Sheets(2).Range("A8").TextToColumns DataType:=1, Comma:=1
    4. End Sub
    You should also dim your vars as Object instead of Variant
    VB Code:
    1. Option Explicit
    2. Dim objExcel As Object
    3. Set objExcel = CreateObject("Excel.Application")
    4. ' Make Excel visible'
    5. objExcel.Visible = True
    6.  
    7. 'Open template'
    8. Dim wb As Object
    9. Set wb = objExcel.Workbooks.Open("C:\dummy.xls")
    10.                  
    11. 'Import data'
    12. wb.Sheets(2).Range("A8").Value = "a, b, c"
    13. wb.Sheets(2).Range("A8").TextToColumns DataType:=1, Comma:=1
    14.  
    15. ' Close to prompt save and then quit.'
    16. wb.close
    17. If objExcel.Workbooks.Count = 0 Then
    18.     objExcel.Quit
    19. 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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Feb 2005
    Posts
    16

    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.

  13. #13
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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:
    1. 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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Feb 2005
    Posts
    16

    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...

  15. #15
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Feb 2005
    Posts
    16

    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.

  17. #17
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How do VB for Word, Excel and/or VBScript work together?

    Quote 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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  18. #18
    Fanatic Member ZeBula8's Avatar
    Join Date
    Oct 2002
    Posts
    548

    Re: How do VB for Word, Excel and/or VBScript work together?

    Quote 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.

  19. #19

    Thread Starter
    Junior Member
    Join Date
    Feb 2005
    Posts
    16

    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
  •  



Click Here to Expand Forum to Full Width