Results 1 to 12 of 12
  1. #1
    allenjasonbrown@gmail.com is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Jun 2010
    Posts
    44

    Transferspreadsheet Method

    I have a query that I use Transferspreadsheet method to simply export it to a excel document. (row one is column headers and then row 2 is data).


    WORKS GREAT.

    NOW...I would like to be able to insert a TITLE "THIS IS MY NEW DOC" in row 1 of the excel document and shift everything down one row.

    Is there an easy way to do this? I have 20+ queries I need to do this to so am looking for basic code.

    Any help / Examples would be GREAT.

    Thank you in advance!

  2. #2
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    After you have exported it, you can reopen the workbook and do it using this. Save this function into a standard module (not form, report or class module):

    Code:
    Function InsertTitle(strTitle As String, strFileAndPath As String)
       Dim objXL As Object
       Dim xlWB As Object
     
       Set objXL = CreateObject("Excel.Application")
       objXL.Visible = True ' you can comment this out later when it is working
     
       Set xlWB = objXL.Workbooks.Open(strFileAndPath)
       xlWB.Worksheets(1).Range("A1").EntireRow.Insert
       xlWB.Worksheets(1).Range("A1").Value strTitle
     
    xlWB.Save
    xlWB.Close
     
    objXL.UserControl = True  ' comment this out too if you aren't leaving it visible and then
    ' objXL.Quit   ' uncomment this line if you comment out the visible and such 
    Set objXL = Nothing
     
    End Function
    But the workbook would need to not be open from the TransferSpreadsheet for this.

  3. #3
    allenjasonbrown@gmail.com is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Jun 2010
    Posts
    44
    Thank you, I think this is exactly what I need.

    I am getting an error on " xlWB.Worksheets(1).Range("A1").EntireRow.Insert"
    error = "Object doesn't support this property or method"

    Do I need a specific reference?

  4. #4
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    Hmm, I put it into my Access database and got the error on the next line, not that one. I forgot to put the = sign between .Value and strTitle.

    So what works for me is:
    Code:
    Function InsertTitle(strTitle As String, strFileAndPath As String)
    Dim objXL As Object
    Dim xlWB As Object
     
    Set objXL = CreateObject("Excel.Application")
    objXL.Visible = True ' you can comment this out later when it is working
     
    Set xlWB = objXL.Workbooks.Open(strFileAndPath)
    xlWB.worksheets(1).range("A1").EntireRow.Insert
    xlWB.worksheets(1).range("A1").select
    xlWB.worksheets(1).range("A1").Value = strTitle
     
    xlWB.Save
    xlWB.Close
     
    objXL.UserControl = True ' comment this out too if you aren't leaving it visible and then
    ' objXL.Quit ' uncomment this line if you comment out the visible and such
    Set objXL = Nothing
     
    End Function

  5. #5
    allenjasonbrown@gmail.com is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Jun 2010
    Posts
    44
    Thank you....that worked great (sorry I had pasted the wrong line into my post...it was the "=" sign"
    I really appreciate your help.

    Are there other things I can do within this code? (i.e. make the title line bold, underline, etc). Is there a place I can look up different options?

    Thank you again!!!

  6. #6
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    Basically, do what I do. Record an Excel macro while doing what you want and then go to the VBA window in Excel and then use that code (with slight modifications).

  7. #7
    allenjasonbrown@gmail.com is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Jun 2010
    Posts
    44
    Thank you again. This seems to be working great and I was able to record some macros and incorporate them into the VBA code.

    I did run into a couple that I had trouble with. Do you have any idea how to put these in the proper format for the above VBA? (I can do the selection part but the "= xl****" does not seem to work.

    1.Range("A5:W5").Select
    With Selection.Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .ThemeColor = xlThemeColorDark1
    .TintAndShade = -0.14996795556505
    .PatternTintAndShade = 0
    End With

  8. #8
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    If you have the EXCEL code using Late Binding, then you will have to provide the values at the top of your code (create your own constants):

    Code:
    Const xlSolid As Integer = 1
    Const xlAutomatic As Integer = -4105
    And then with this one:
    Const xlThemeColorDark1 As Integer = ??? ' I don't know what it is because I don't have Access 2007/2010 handy to check. But you just open up Excel and then to TOOLS > MACROS > VISUAL BASIC (or in 2007/2010 using the Design tab) you then can type

    ?xlThemeColorDark1

    into the Immediate Window and then hit enter and it will give you the value.

  9. #9
    allenjasonbrown@gmail.com is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Jun 2010
    Posts
    44
    I am using this code:

    xlWB.worksheets(1).Range("A5:U5").Select
    With Selection.Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .ThemeColor = xlThemeColorDark2
    .TintAndShade = -9.99481185338908E-02
    .PatternTintAndShade = 0
    End With

    and have placed this at the top:
    Const xlSolid As Integer = 1
    Const xlAutomatic As Integer = -4105
    Const xlThemeColorDark2 As Integer = 3

    I am still getting a object required error.

  10. #10
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    This code here isn't tied to an instantiated object:

    With Selection.Interior

    It should be connected to your application object. Also, you might double check to see if you now have extra EXEL.EXE process running in your task manager. There is a good possibility that there is as they will appear if you have code not tied to an instantiated object, and then only close when the database itself is closed.

    Read this for more about that:
    http://www.btabdevelopment.com/ts/excelinstance

  11. #11
    allenjasonbrown@gmail.com is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Jun 2010
    Posts
    44
    Thank you .... and yes I did have a EXCEL.EXE process running. Can you show me an example of how to use this code within the example above?
    xlWB.worksheets(1).Range("A5:U5").Select
    With Selection.Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .ThemeColor = xlThemeColorDark2
    .TintAndShade = -9.99481185338908E-02
    .PatternTintAndShade = 0
    End With

    I cannot figure out how to tie it to an object and get it to work correctly.

    Thanks.

  12. #12
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    If objXL is the application object, then it would be:

    Code:
    With objXL.Selection.Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .ThemeColor = xlThemeColorDark2
    .TintAndShade = -9.99481185338908E-02
    .PatternTintAndShade = 0
    End With

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 02-27-2010, 06:53 AM
  2. Transferspreadsheet Help Please
    By graviz in forum Programming
    Replies: 0
    Last Post: 11-30-2009, 02:56 PM
  3. Can't get TransferSpreadsheet to work
    By Valeda in forum Import/Export Data
    Replies: 0
    Last Post: 07-19-2007, 08:04 AM
  4. transferSpreadsheet
    By rabbit in forum Import/Export Data
    Replies: 1
    Last Post: 06-09-2006, 04:01 PM
  5. transferspreadsheet function
    By jeffj in forum Import/Export Data
    Replies: 3
    Last Post: 03-13-2006, 11:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums