473,395 Members | 1,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Controlling Excel via Access: Runtime 1004 Method 'Intersect of object' Global failed

Hi all,

I am trying to export query to Excel from Access, manipulate it into a pivot table, format the pivot table and then export into a word document...

I have some code that I have made/taken from other places to do so but I come across a runtime error on one line of code (line 66 in the below code section). This error only happens every other time the code is run which I find odd - e.g. first time works fine, second time gets error. The error relates to me trying to change the number format for all of the value cells that are not subtotals/totals.

All help is greatly appreciated!

Expand|Select|Wrap|Line Numbers
  1. Sub ExportToWord()
  2.  
  3. Dim xlApp As Excel.Application
  4. Dim xlWB As Excel.Workbook
  5. Dim xlWS As Excel.Worksheet
  6. Dim pt As PivotTable
  7. Dim i As Integer
  8. Dim rng1 As Range
  9. Dim rng2 As Range
  10. Dim ptItem As PivotItem
  11. Dim fileName As String
  12. Dim dataSheet As Worksheet
  13. Dim pivotSheet As Worksheet
  14. Dim pc As PivotCache
  15. Dim x As Integer
  16. Dim WordApp As Word.Application
  17. Dim myDoc As Word.Document
  18.  
  19. 'in my sub i have the actual path here but removed just in case ;)
  20. fileName = "\test.xlsx"
  21.  
  22. 'export query to excel file
  23. DoCmd.TransferSpreadsheet acExport, , "qryCountThisCross", fileName, True
  24.  
  25. 'open excel file
  26. Set xlApp = New Excel.Application
  27.  
  28. With xlApp
  29.     .Visible = True
  30.     Set xlWB = .Workbooks.Open(fileName, , False)
  31. End With
  32.  
  33. Set dataSheet = xlWB.Worksheets(1)
  34.  
  35. 'set up pivot table
  36. xlWB.Worksheets.Add(After:=dataSheet).Name = "PivotSheet"
  37. Set pivotSheet = xlWB.Worksheets("PivotSheet")
  38.  
  39. Set pc = xlWB.PivotCaches.Create(xlDatabase, dataSheet.Range("A1:P40"))
  40. Set pt = pc.CreatePivotTable(pivotSheet.Range("A1"), "PivotTable")
  41.  
  42. 'edit pivot table layout
  43. Set pt = pivotSheet.PivotTables("PivotTable")
  44. With pt
  45.     With .PivotFields("Service Line")
  46.         .Orientation = xlRowField
  47.         .Position = 1
  48.         .PivotItems("Younger Adult").Position = 1
  49.         .PivotItems("OPMH").Position = 2
  50.         .PivotItems("Rehab").Position = 3
  51.         .PivotItems("Forensic & Specialist").Position = 4
  52.     End With
  53.     With .PivotFields("Location")
  54.         .Orientation = xlRowField
  55.         .Position = 2
  56.     End With
  57.     With .PivotFields("Ward Name")
  58.         .Orientation = xlRowField
  59.         .Position = 3
  60.     End With
  61.     For x = 5 To .PivotFields.Count
  62.         .AddDataField pt.PivotFields(x), .PivotFields(x).Name & " ", xlSum
  63.     Next x
  64.     .AddDataField pt.PivotFields(4), "Total ", xlSum
  65.  
  66.     'the problem section
  67.     'i'm trying to make it so that if a 0 appears in the data section that is not in the subtotals
  68.     'or totals then it should appear in light grey
  69.     For i = 1 To (pt.DataFields.Count - 1)
  70.         Set rng1 = pt.DataFields(i).DataRange
  71.         For Each ptItem In pt.PivotFields("Ward Name").PivotItems
  72.             Set rng2 = ptItem.DataRange.EntireRow
  73.             'Runtime error 1004: Method 'Intersect' of object' _ Global failed
  74.             Intersect(rng1, rng2).NumberFormat = "#,##0;#,##0;[Color15]#,##0"
  75.         Next ptItem
  76.     Next i
  77.  
  78.     .CompactLayoutRowHeader = "Ward by Service Line"
  79.     .DataPivotField.Caption = "Month"
  80.     .PivotFields(1).LayoutBlankLine = True
  81.     .SubtotalLocation xlAtBottom
  82. End With
  83.  
  84. pt.TableRange1.Copy
  85.  
  86. 'Create an Instance of MS Word
  87.   On Error Resume Next
  88.  
  89.     'Is MS Word already opened?
  90.       Set WordApp = GetObject(Class:="Word.Application")
  91.  
  92.     'Clear the error between errors
  93.       Err.Clear
  94.  
  95.     'If MS Word is not already open then open MS Word
  96.       If WordApp Is Nothing Then Set WordApp = CreateObject(Class:="Word.Application")
  97.  
  98.     'Handle if the Word Application is not found
  99.       If Err.Number = 429 Then
  100.         MsgBox "Microsoft Word could not be found, aborting."
  101.         GoTo CLEAR_UP
  102.       End If
  103.  
  104.   On Error GoTo 0
  105.  
  106. 'Make MS Word Visible and Active
  107.     WordApp.Visible = True
  108.     WordApp.Activate
  109.  
  110. 'Create a New Document
  111.     Set myDoc = WordApp.Documents.Add
  112.  
  113. 'Copy Excel Table Range
  114.     WordApp.Selection.PasteSpecial , , , , wdPasteOLEObject
  115.  
  116. xlWB.Close False
  117. xlApp.Quit
  118.  
  119. CLEAR_UP:
  120. Set xlWS = Nothing
  121. Set xlWB = Nothing
  122. Set xlApp = Nothing
  123. Set myDoc = Nothing
  124. Set WordApp = Nothing
  125.  
  126. Set pt = Nothing
  127. Set pc = Nothing
  128. Set pivotSheet = Nothing
  129. Set dataSheet = Nothing
  130. Set xlWB = Nothing
  131.  
  132. Set rng2 = Nothing
  133. Set rng1 = Nothing
  134. Set ptItem = Nothing
  135.  
  136. End Sub
Nov 16 '15 #1

✓ answered by jforbes

I've not done what you are attempting, but I've done some Cross Office Application VBA development. I looked up the Intersect command on Microsoft's website and there was this example:
Expand|Select|Wrap|Line Numbers
  1. Worksheets("Sheet1").Activate 
  2. Set isect = Application.Intersect(Range("rg1"), Range("rg2")) 
  3. If isect Is Nothing Then 
  4.  MsgBox "Ranges do not intersect" 
  5. Else 
  6.  isect.Select 
  7. End If
From their code it looks like the Intersect Method doesn't always return an object. Which means you might want to want to test to see if there is an intersection before setting the Number Format. I didn't look at your code hard enough to see if there will always be an intersection, but this would be the place I would start.

Something like:
Expand|Select|Wrap|Line Numbers
  1. Dim oIntersect As Range
  2. ...
  3. Set oIntersect = xlApp.Intersect(rng1, rng2) 
  4. If not oIntersect Is Nothing Then 
  5.     oIntersect.NumberFormat = "#,##0;#,##0;[Color15]#,##0"
  6. End If

2 1834
jforbes
1,107 Expert 1GB
I've not done what you are attempting, but I've done some Cross Office Application VBA development. I looked up the Intersect command on Microsoft's website and there was this example:
Expand|Select|Wrap|Line Numbers
  1. Worksheets("Sheet1").Activate 
  2. Set isect = Application.Intersect(Range("rg1"), Range("rg2")) 
  3. If isect Is Nothing Then 
  4.  MsgBox "Ranges do not intersect" 
  5. Else 
  6.  isect.Select 
  7. End If
From their code it looks like the Intersect Method doesn't always return an object. Which means you might want to want to test to see if there is an intersection before setting the Number Format. I didn't look at your code hard enough to see if there will always be an intersection, but this would be the place I would start.

Something like:
Expand|Select|Wrap|Line Numbers
  1. Dim oIntersect As Range
  2. ...
  3. Set oIntersect = xlApp.Intersect(rng1, rng2) 
  4. If not oIntersect Is Nothing Then 
  5.     oIntersect.NumberFormat = "#,##0;#,##0;[Color15]#,##0"
  6. End If
Nov 16 '15 #2
Hi jforbes,

Thanks for having a look. I would have thought that there would always be an intersection being a pivot table with a format that doesn't change. Anyway, I gave the code a go and it works like a charm. Tested it 5 times in a row with no runtime errors and correct results everytime.

Many thanks!
Nov 16 '15 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: richilli | last post by:
Hi Any help on this would be appreciated cos its driving me insane. I have a function in VB.NET that takes in an excel range and tries to delete rows where the first column starts with a...
2
by: Lauren Wilson | last post by:
Hi folks, I have an Access 2003 app that works perfectly on my computer. However, when we install it on OTHER computers that ALSO have access 2003 we get an error on startup: Error:...
0
by: Chris | last post by:
Hi, I tried to install Access Runtime 2007 on 2 machines having Word, Excel, Power Point 2007 but unfortunately it did not succeed. I got the message "Installation failed" which was not helpful...
4
by: ielamrani | last post by:
Hi, I am getting this error when I try to export to an excel sheet. When I click on a button to export the first time it's fine, I rename the exported excel sheet and I try to export it again and I...
0
by: CoreyReynolds | last post by:
Hey all, I have a piece of code that dumps a bunch of data into a spreadsheet. Also rearranges it into a pivot table and then graphs the pivot table as well so my boss can get a clear view of the...
4
by: fnemo | last post by:
I'm getting the error - Method 'Item' of object 'Forms' failed . Earlier this error was not occuring. In the below code, first textboxes are created dynamically in the form "display_result"....
3
by: Hema Suresh | last post by:
Hi all I created a database via VB and saved it in excel sheet I have 10 command buttons and 10 text box controls on the vb form and i coded in the way to get the data from the user once the...
4
by: ChiomaJennifer | last post by:
hi... i have an access application in which i need to open an existing excel sheet, find a date (already in the sheet) and populate a row (with the date cell column) with either 1 or 0 this means...
3
by: bpremlatha | last post by:
Sub WorkBook_Open() RemoveEmptyRow ConcatenateColumn DeleteBlankColumns SaveFile End Sub Sub SaveFile() 'ActiveWorkbook.Save ThisWorkbook.Saved = True
4
by: hvsummer | last post by:
Hi guy, this is another core question. While I can't handle with OLE error on this link...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.