Consulting

Results 1 to 8 of 8

Thread: Solved: Share Variable from Word to Excel

  1. #1
    VBAX Newbie
    Joined
    Oct 2006
    Posts
    4
    Location

    Solved: Share Variable from Word to Excel

    I want to use a Word Form as a front end for populating a few cells in Excel. How can I pass the word field value to excel? I can retrieve the word field value and assign it into a variable but how do I have it availabe to use in Excel?

    Thank You,
    DJR28

  2. #2
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    That depends, will you have a macro that opens the Excel file and places the variable somewhere?

  3. #3
    VBAX Newbie
    Joined
    Oct 2006
    Posts
    4
    Location
    Thanks,
    I prefer to use a macro. I do not want to link or embed.

  4. #4
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Here is an example:

    [vba]
    Option Explicit

    Sub ExcelExample()

    Dim MyVar As String
    Dim AppExcel As Object
    Dim Wkb As Object

    MyVar = "Test"

    Set AppExcel = CreateObject("Excel.Application")
    Set Wkb = AppExcel.Workbooks.Add
    Wkb.sheets(1).Range("A1").Value = MyVar

    AppExcel.Visible = True

    ExitSub:

    Set Wkb = Nothing
    Set AppExcel = Nothing

    End Sub
    [/vba]

    In this case I am just assigning a value to MyVar, but you can replace that with the value from Word. Also I am creating a new workbook, but you can open an existing one as well.

    Just replace:

    [VBA] Set Wkb = AppExcel.Workbooks.Add
    [/VBA]
    With:

    [VBA] Set Wkb = AppExcel.Workbooks.Open(Filename:="C:\MyPath\MyExcelFile.xls")[/VBA]

  5. #5
    VBAX Newbie
    Joined
    Oct 2006
    Posts
    4
    Location
    This works for me. Thanks loads for your help.

  6. #6
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    You're Welcome

    Take Care

  7. #7
    VBAX Newbie
    Joined
    Oct 2006
    Posts
    4
    Location
    Got everything working great... but... the excel workbook opens as read-only. What is up with that? Can it be opened regular? Thanks again for the help.

    djr28

  8. #8
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    836
    Location
    [vba]AppExcel.Visible = True [/vba]
    Is this a Word macro or XL macro? If your using an XL macro then I think you need to save the wb then quit this application then re-open the wb/file rather than make it visible. You would have 2 XL applications (not just 2 open wbs) which I don't think is too good? HTH. Dave

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •