I'm trying to write a VBA macro from within Word to create an Excel spreadsheet and export data to it. I'm having trouble just creating the new spreadsheet. For the following macro, I get "Object doesn't support this property or method" on the line
Set myWb = myExcel.Workbooks.Add
in the code
Private Sub CreateExcel2()
Dim myExcel As Object
Dim myWb As Object
Set myExcel = CreateObject("Excel.Application")
Set myWb = myExcel.Workbooks.Add
Application.DisplayAlerts = False
myWb.SaveAs FileName:="D:\test\dump.xls"
Application.DisplayAlerts = True
myWb.Close False
Set myWb = Nothing
myExcel.Quit
Set myExcel = Nothing
End Sub
You can try:
On Error Resume Next
Set oExcel = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
Err.Clear
Set oExcel = CreateObject("Excel.Application")
End If
On Error GoTo 0
oExcel.visible = true 'to check of excel opens
'try Set myWb = myExcel.Workbooks.Add()
'try Set myWb = oExcel.Workbooks.Open(sPathFile)
Related
I am trying to convert a pdf file to an excel file (xlsx) using excel VBA.
The problem is the code seems to be perfectly fine as I have seen it working on other computers in action, but for some reason, I am getting a run time error and I am trying to solve this for a week.
Below is the code
Option Explicit
Function ClearCipboard()
'Early binding will requires a Reference to 'Microsoft Forms 2.0 Object Library'
Dim oData As Object 'New MSForms.DataObject
Set oData = CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
oData.SetText Text:=Empty
oData.PutInClipboard
Set oData = Nothing
End Function
Sub Automate()
Dim PathforPDFfiles As String
Dim PathforExcelfiles As String
PathforPDFfiles = "C:\Users\kvenkat2\Desktop\Trails 18.06.2021\Test File Excel\PDF-to-Excel-Converter\"
PathforExcelfiles = "C:\Users\kvenkat2\Desktop\Trails 18.06.2021\Test File Excel\PDF-to-Excel-Converter\"
Dim fso As New FileSystemObject
Dim myFolder As Folder
Dim myFile As File
Set myFolder = fso.GetFolder(PathforPDFfiles)
Dim WordApp As Object
Dim WordDoc As Object
Dim WordRange As Object
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Set WordApp = CreateObject("word.application")
'Set WordDoc = WordApp.documents.Add
'Set WordApp = New Word.Application
WordApp.Visible = True
Dim nwb As Workbook
Dim nsh As Worksheet
For Each myFile In myFolder.Files
Set WordDoc = WordApp.documents.Open(myFile.Path, False, Format:="PDF Files")
Set WordRange = WordDoc.Paragraphs(1).Range
WordRange.WholeStory
Set nwb = Workbooks.Add
Set nsh = nwb.Sheets(1)
WordRange.Copy
nsh.Paste
nwb.SaveAs (PathforExcelfiles & Replace(myFile.Name, ".pdf", ".xlsx"))
Application.CutCopyMode = False
Call ClearCipboard
WordDoc.Close True
nwb.Close True
Next
WordApp.Quit
Set WordDoc = Nothing
Set WordApp = Nothing
Application.displayAlters = True
Application.ScreenUpdating = False
MsgBox ("Done for real")
End Sub
Set WordDoc = WordApp.documents.Open(myFile.Path, False, Format:="PDF Files")
This is the part where my code stops running and I try to see the opened word and nothing happens from here. I am unable to get past this line.
It shows as a run time error as shown in the image
I am opening Excel from MsAccess and when I finished the task I close it, but Excel remain opened in backgroud until I close MsAccess
this is the code
Public Sub closeExcel()
Dim xlApp
Dim wkb As Object
Dim wks As Object
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If err <> 0 Then
Set xlApp = CreateObject("Excel.Application")
End If
With xlApp
Set wkb = .Workbooks.Add
Set wks = wkb.Worksheets(1)
wkb.Close True
Set wks = Nothing
Set wkb = Nothing
.Quit
End With
End Sub
I need to remain Access runing, but I want that Excel be closed.
How can I do this?
You are cutting off the worksheet. Try this:
With xlApp
Set wkb = .Workbooks.Add
Set wks = wkb.Worksheets(1)
' Do stuff.
Set wks = Nothing
wkb.Close True
Set wkb = Nothing
.Quit
End With
Set xlApp = Nothing
I occasionally get a run time error when trying to open/manipulate Excel files using Access VBA. The error is
"Run-Time error '462': The remote server machine does not exist or is
unavailable
What is frustrating is that the error occurs only for certain files and not others and in different instances. Here is my code, the error occurs at the workbooks.open(sPath) line:
DoCmd.SetWarnings False
Dim oExcel As New Excel.Application
Dim oWB As Workbook
Dim oWS As Worksheet
Set oExcel = Excel.Application
Set oWB = oExcel.Workbooks.Open(sPath)
Set oWS = oWB.Sheets(1)
oExcel.Visible = False
If fGetFileName(sPath) = "FILE_NAME1.xlsx" Then
'oExcel.Visible = False
oWS.Range("AW1").Value = "TEXT1"
oWS.Range("AX1").Value = "TEXT2"
oWS.Range("AY1").Value = "TEXT3"
End If
oWB.Save
Debug.Print "Amended " & sPath
oWB.Close False
Set oWB = Nothing
oExcel.Quit
Set oExcel = Nothing
DoCmd.SetWarnings True
After a bit of research online, I've found this document gives a good overview of the error: https://anictteacher.files.wordpress.com/2011/11/vba-error-462-explained-and-resolved.pdf
Using the logic from that document, the error is that:
object has not been fully qualified by reference to the Office object
in every case
However, I amended the row where the error occurs to specifically reference the Excel object (Set oWB = oExcel.Workbooks.Open(sPath)). Have tried declaring the dimensions as Objects and put reference to oExcel in every mention of a workbook/sheet. Any ideas? Does sPath need to better qualified?
You declare oExcel as a new Excel.Application:
Dim oExcel As New Excel.Application
That means later in your code, Set oExcel = Excel.Application is not really useful ... because oExcel is already a reference to Excel.Application. Add in a MsgBox to demonstrate what oExcel is at that point ...
MsgBox "TypeName(oExcel): " & TypeName(oExcel)
'Set oExcel = Excel.Application
Consider a different approach for your Excel object variable. Test this stripped down procedure (the purpose is only to see whether it eliminates your current error):
Dim oExcel As Excel.Application
Dim oWB As Excel.Workbook
DoCmd.SetWarnings True
MsgBox "TypeName(oExcel): " & TypeName(oExcel)
Set oExcel = New Excel.Application
MsgBox "TypeName(oExcel): " & TypeName(oExcel)
Set oWB = oExcel.Workbooks.Open(sPath)
oWB.Close False
Set oWB = Nothing
oExcel.Quit
Set oExcel = Nothing
Also, do use WorkSheet and close this:
Set oWS = oWB.WorkSheets(1)
' snip.
oWB.Save
Set oWs = Nothing
oWB.Close False
Set oWB = Nothing
I have written a VBA(Access) code to export data from Access database to Excel Worksheet. At the end of the code I have closed all the objects, Recordsets, worksheets and respectively 'nothing' is set.After the first run, I check the task Manager, I see an instance of Excel still exist in it. After the export, If I close the Access Database the instance in the Task Manager is ended. Is this normal ? or Do I have to edit my code?
Disdavantage: I am not able to run the code for second time when the database is still open.
Below is my code
Public Sub Expdata()
Dim rst As DAO.Recordset
Dim xlWBk As Object, Apxl As Object
Dim wsMetaData As Worksheet
Dim wsPlanning As Worksheet
Dim PathEx As String
Dim i As Long
Dim Tempsheetname As String
Dim blnEXCEL As Boolean
blnEXCEL = False
On Error Resume Next
Set Apxl = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
Set Apxl = CreateObject("Excel.Application")
blnEXCEL = True
End If
Err.Clear
On Error GoTo 0
PathEx = Forms("Export").Text14 'path comes from the directory given in form
'Set Apxl = CreateObject("Excel.Application")
Set xlWBk = Apxl.Workbooks.Open(PathEx)
Tempsheetname = "Metadatasheet"
Worksheets.Add.Name = Tempsheetname
Set wsMetaData = xlWBk.Worksheets("Metadatasheet")
Set wsPlanning = xlWBk.Worksheets("PlanningData")
Apxl.Visible = True 'uncomment for debug to see excel file
Set rst = CurrentDb.OpenRecordset("LatestSNR")
For i = 1 To rst.Fields.Count
wsMetaData.Cells(1, i).Value = rst.Fields(i - 1).Name
Next i
rst.MoveFirst
wsMetaData.Range("A2").CopyFromRecordset rst
'calls Exp_Refresh module
Call Exp_Refresh.RfData(xlWBk)
Set xlWBk = Apxl.Workbooks.Open(PathEx)
xlWBk.Sheets("Metadatasheet").Select
DoCmd.SetWarnings False
xlWBk.Application.DisplayAlerts = False
xlWBk.Sheets("Metadatasheet").Delete
DoCmd.SetWarnings True
xlWBk.Application.DisplayAlerts = True
'Close all the objects and recordsets
rst.Close
Set rst = Nothing
xlWBk.Close SaveChanges:=True
Set xlWBk = Nothing
Set wsMetaData = Nothing
Set wsPlanning = Nothing
If blnEXCEL = True Then Apxl.Quit
Set Apxl = Nothing
MsgBox "Export Successful !!!"
End Sub
It's Excel not closing? Then CreateObject on the workbook, not ExcelApp.
Also if excel is set to visible then the rules of COM says you do not close if the user can see it.
After a bit of research.. I found the error point
Tempsheetname = "Metadatasheet"
Worksheets.Add.Name = Tempsheetname
At this point it had lost its reference from the object. So if "Access app has ANY reference to an Excel resource, Excel won't close".
All I had to do was;
Tempsheetname = "Metadatasheet"
xlWBk.Worksheets.Add.Name = Tempsheetname
I need to have a vb code in ms word 2003 that copy a a specific cell in excel file and paste it in word (filed). Below is what I have done and it result in error.
Sub cmdGetNumber()
Dim XL As Object
Dim WBEx As Object
Dim ExelWS As Object
Dim appwd As Object
Dim wdApp As Word.Application
''''
'On Error GoTo OLE_ERROR
Set XL = CreateObject("Excel.Application")
Set wdApp = CreateObject("Word.Application")
'Open Excel document
Set WBEx = XL.Workbooks.Open("C:\Documents and Settings\121567\Desktop\tafket1.xls")
Set ExelWS = WBEx.Worksheets("Sheet1")
XL.Visible = True
'appwd.Visible = True
ExelWS.Range("c2").Select
'Selection.Copy
'wdApp.Selection.PasteSpecial Placement:=wdInLine, DataType:=wdPasteMetafilePicture
'wdApp.Documents.Save
Set wdApp = Nothing
Set ExelWS = Nothing
Set WBEx = Nothing
End Sub
Since this macro is in Word, you don't need to explicitly open a word instance. You can just do Documents.Add to add a new document, or Documents.Open to open an existing one.
Try this:
Sub cmdGetNumber()
Dim XL As Object
Dim WBEx As Object
Dim ExelWS As Object
Dim wdDoc As Word.Document
'On Error GoTo OLE_ERROR
Set XL = CreateObject("Excel.Application")
'Open Excel document
Set WBEx = XL.Workbooks.Open("C:\Documents and Settings\121567\Desktop\tafket1.xls")
Set ExelWS = WBEx.Worksheets("Sheet1")
'XL.Visible = True
ExelWS.Range("C2").Copy
Set wdDoc = Documents.Add
wdDoc.Activate
wdDoc.Select
Selection.Paste
WBEx.Close
XL.Quit
Set WBEx = Nothing
Set ExelWS = Nothing
Set XL = Nothing
End Sub
The above code will open your excel file, copy the cell C2, then open a new word document, and paste it there.
I see you have mentioned a (filed) in your question. Did you mean a Field or a File? If it is a Field then you may want to replace Selection.Paste with the relevant field name