VBA conversion Office 2007 to Office 2010 - ms-access

I have a function in an Access 2007 database, which has been working fine until my PC was upgraded to Office 2010. The procedure is below and the offending line is that where 'originalFolder' is set:
Function ExportToSharePoint()
Dim oFs As New FileSystemObject
Dim originalFolder As Folder
Dim destinationPath As String
Dim ofile As file
Dim XLApp As Excel.Application
Dim xlwb As Excel.Workbook
Dim strFileName As String
Dim oFolder As String
oFolder = "//chs114file1/dovpasres/Public/Script/InfoCentre/Delays"
Set oFs = CreateObject("Scripting.FileSystemObject")
Set XLApp = New Excel.Application
Kill "K:\Public\Script\InfoCentre\Delays\*.xlk"
Set originalFolder = oFs.GetFolder(oFolder)
destinationPath = "https://companyname.sharepoint.com/PRR/Documents/"
For Each ofile In originalFolder.Files
strFileName = oFs.GetFileName(ofile)
Set xlwb = XLApp.Workbooks.Open(ofile)
xlwb.SaveAs (destinationPath + strFileName)
Next
xlwb.Close True
XLApp.Quit
Set xlwb = Nothing
Set XLApp = Nothing
End Function
The error I'm getting is:
Error 13: data type mismatch
I'm mystified as this is a string, as required?

The Microsoft documentation lists the GetFolder return type as being Folder, but I suspect your problem is being caused by mixing late binding (via CreateObject) with early binding (via the strongly-typed Folder variable).
Either import the reference to avoid the CreateObject call, or change the type to a variant, or perhaps object:
Dim originalFolder As variant

Related

Unable to pass Excel worksheet to a function in VBA Access

I am using VBA in an access application to create an Excel file. I am creating the excel application and worksheet just fine but when I try to pass that worksheet to another function, I get this error:
Run-time error '438':
Object doesn't support this property or method
Here is the code where I set up my excel application:
Public Sub setUpExcel()
Dim XLAPP As Excel.Application
Set XLAPP = New Excel.Application
XLAPP.Visible = True
Dim WKB As Excel.Workbook
Dim WKS As Excel.Worksheet, WKS2 As Excel.Worksheet
Set WKB = XLAPP.Workbooks.Open(excelFile)
Set WKS = WKB.ActiveSheet '.Worksheets(1)
WKS.Range("A1").Value = "Test Value"
WKS.Range("A1").Select
WKS.Range("A1").Value = ""
makeMasterPage (WKS) '<--WHERE I GET ERROR
End Sub
The call to makeMasterPage is where I get my error. Here is that function:
Private Sub makeMasterPage(ByRef WKS As Excel.Worksheet)
...
End Sub

Make VBA run from MS Access

I'm trying to use a macro from MS Access that #masoud wrote for me here - Reopening recently closed instances of Excel
What do I need to do to have it run from MS Access? Code has been converted to the following to suit me:
Public Sub CloseAllExcel(Close1 As Boolean)
On Error GoTo handler
Dim xl As Excel.Application
Dim wb As Excel.Workbook
Dim strPath As String
strPath = "C:\path.txt"
If Close1 Then
Dim fso as Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim oFile as Object
Set oFile = FSO.CreateTextFile(strPath)
Do While xl Is Nothing
Set xl = GetObject(, "Excel.Application")
For Each wb In xl.Workbooks
oFile.WriteLine Application.ActiveWorkbook.FullName
wb.Save
wb.Close
Next
oFile.Close
Set fso = Nothing
Set oFile = Nothing
xl.Quit
Set xl = Nothing
Loop
Exit Sub
Else
Dim FileNum As Integer
Dim DataLine As String
FileNum = FreeFile()
Open strPath For Input As #FileNum
While Not EOF(FileNum)
Line Input #FileNum, DataLine
Workbooks.Open DataLine
Wend
Exit Sub
End If
handler:
If Err <> 429 Then 'ActiveX component can't create object
MsgBox Err.Description, vbInformation
End If
End Sub
The issues I have is that oFile.WriteLine Application.ActiveWorkbook.FullName is having a method or data member not found for the ActiveWorkbook. I'm a bit of a novice with this but I would have thought the code was good enough as is to work straight from MS Access. It works if run from Excel though.
Thanks for the help.

Add new sheets to the exported excel using vba and do a lookup between two sheets using access VBA [duplicate]

I am running a few modules of code in access and am writing data into
Excel. When I write the first time, data gets written properly. But again
when I try, the new data is written on top of the old data. What should I do to
insert a new sheet?
My existing code is
Dim objexcel As Excel.Application
Dim wbexcel As Excel.Workbook
Dim wbExists As Boolean
Dim objSht As Excel.Worksheet
Dim objRange As Excel.Range
Set objexcel = CreateObject("excel.Application")
On Error GoTo Openwb
wbExists = False
Set wbexcel = objexcel.Workbooks.Open("C:\REPORT1.xls")
Set objSht = wbexcel.Worksheets("Sheet1")
objSht.Activate
wbExists = True
Openwb:
On Error GoTo 0
If Not wbExists Then
objexcel.Workbooks.Add
Set wbexcel = objexcel.ActiveWorkbook
Set objSht = wbexcel.Worksheets("Sheet1")
End If
I think that the following code should do what you want. It's very similar to yours, except it uses the return values from the .Add methods to get the objects you want.
Public Sub YourSub()
Dim objexcel As Excel.Application
Dim wbexcel As Excel.Workbook
Dim wbExists As Boolean
Set objexcel = CreateObject("excel.Application")
'This is a bad way of handling errors. We should'
'instead check for the file existing, having correct'
'permissions, and so on, and actually stop the process'
'if an unexpected error occurs.'
On Error GoTo Openwb
wbExists = False
Set wbexcel = objexcel.Workbooks.Open("C:\REPORT1.xls")
wbExists = True
Openwb:
On Error GoTo 0
If Not wbExists Then
Set wbexcel = objexcel.Workbooks.Add()
End If
CopyToWorkbook wbexcel
EndSub
Private Sub CopyToWorkbook(objWorkbook As Excel.Workbook)
Dim newWorksheet As Excel.Worksheet
set newWorksheet = objWorkbook.Worksheets.Add()
'Copy stuff to the worksheet here'
End Sub

Run-time error '91' vba-access

Hey my program checks for a zip file and copies it to another directory. However I stumbled upon "Run time error '91' object variable or With block variable not set" on oApp when I compiled it.
Sub UnZip(Fname As Variant)
Dim oApp As Object
Dim FileNameFolder As Variant
FileNameFolder = "P:\"
Set oApp = CreateObject("Shell.Application")
oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(Fname).items
End Sub
What's the problem?
I'm using MS access 2010
.Copyhere works on folder objects.
Sub UnZip(Fname As Variant)
dim objShell
dim objFolder
set objShell = CreateObject("shell.application")
set objFolder = objShell.NameSpace("P:")
If not objFolder is nothing then
objFolder.CopyHere(Fname)
End If
End Sub
Fname must include both path and filename with extension.

Access 2010 ADO on Windows XP Mode (in ADDE format)

I have a problem with ADO in My application. I have Access installed on my computer with Win7Pro and there I can use both version (ACCDB and ACCDE). But only ACCDB works under runtime (with SP1) in WinXPMode environment.
There is the code
Dim strSQL As String, Cnxn As ADODB.Connection, Rsxn As ADODB.Recordset
Dim lngDummy As Long
lngCount = DCount("[Sklad]", "cisSklad", "[Zobrazit]")
CountData = lngCount
If CountData = 0 Then Exit Sub
ReDim ItemValues(lngCount - 1)
Set Cnxn = CurrentProject.AccessConnection
Set Rsxn = New ADODB.Recordset
...
In ACCDE (under RunTime on XPMode):
The line Set Cnxn = CurrentProject.AccessConnection return the message Error 13: Type mismatch.
I have the reference to ADO 2.8.
Debug.Print CurrentProject.AccessConnection: Provider=Microsoft.Access.OLEDB.10.0;Persist Security Info=False;Data Source=C:\Work\SkladII\Sklad.accde;User=Admin;Data Provider=Microsoft.ACE.OLEDB.12.0
All tables in sklad.accde are linked
Do you have any idea, where is the problem?
Investigate whether this issue is confined to early binding for ADO in WinXPMode. Remove the reference for ADO and revise your code to use late binding.
Dim strSQL As String, Cnxn As Object, Rsxn As Object
Set Cnxn = CurrentProject.Connection
'Set Rsxn = New ADODB.Recordset
Set Rsxn = CreateObject("ADODB.Recordset")