Goal is to run this subroutine in an Access application, which embeds a PDF document in a new Excel workbook. The sub works properly with a text file, but will give error "Run-time error '1004': Document not saved." with a PDF file. The code breaks on the SaveAs method. Why does one file-type embed, and the PDF file-type not work? Any help will be appreciated.
Sub SavePDFInWorkBook()
Dim xlObj As New Excel.Application ' Excel application
Dim wbkObj As Excel.workbook ' workbook object
Dim wstObj As Excel.Worksheet ' worksheet object
Dim oleObj As Excel.OLEObject ' embeded object
Dim EmbedFile, MyWbk As String
MyWbk = "M:\SS\DD\Projects\cash_dep2000\TEST.xlsx"
EmbedFile = "M:\SS\DD\Projects\cash_dep2000\DaytonFreight.pdf"
Set wbkObj = xlObj.Workbooks.Add ' workbook is added in open condition in memory
Set wstObj = wbkObj.Worksheets(1) ' workbook is created with one worksheet
Set oleObj = wstObj.OLEObjects.Add(, EmbedFile, False, True)
wbkObj.SaveAs (MyWbk) ' saves workbook to folder
wbkObj.Close ' closes workbook in folder
Set oleObj = Nothing
Set wstObj = Nothing
Set wbkObj = Nothing
Set xlObj = Nothing
MsgBox ("Test Complete")
End Sub
This works to embed pdf icon/link but it also opens the pdf. Aggravating.
ActiveSheet.OLEObjects.Add(Filename:="C:\Users\June\MyStuff\DMV.pdf", Link:=False, _
DisplayAsIcon:=True, IconFileName:= _
"C:\WINDOWS\Installer\{AC76BA86-7AD7-1033-7B44-AB0000000001}\PDFFile_8.ico", _
IconIndex:=0, IconLabel:="whatever you want").Activate
I can manually save the file but SaveAs code for macro-enabled workbook errors. More aggravating. Maybe modifying to your object manipulation code will work.
Related
I currently have a string in Microsoft Access that is delegating functions to an external Application.
I have gotten so far as to copying a set of data from the external application.
I want to paste this into an excel workbook that is not open but exists. C:\Users\abcdef\Desktop KDNR.xlsx
how can I integrate this function into my sub procedure?
Thank you in advance
I attempted simply writing
Dim x as Workbook
set x = Workbooks.Open (" workbook name ")
Howoever, i got the compile error "user defined type not defined"
when i just write
Workbooks.Open (" workbook name ")
i get the compile error
"variable not defined"
Use Excel From Access
This is a basic example of how to work with Excel from Access.
It opens a new instance of Excel (whether Excel is open or not), opens the workbook, writes the string Test to cell A1 of worksheet Sheet1, saves and closes the workbook, and finally quits the Excel instance.
' If you haven't already, create a reference to
' "Tools->References->Microsoft Excel 16.0 Object Library"!
Sub Test()
Dim DestinationFilePath As String
DestinationFilePath = Environ("USERPROFILE") & "\DeskTop\KDNR.xlsx"
Dim xlApp As Excel.Application: Set xlApp = New Excel.Application
xlApp.Visible = True ' out-comment when done developing; default is 'False'
Dim wb As Workbook: Set wb = xlApp.Workbooks.Open(DestinationFilePath)
Dim ws As Worksheet: Set ws = wb.Worksheets("Sheet1")
ws.Range("A1").Value = "Test"
wb.Close SaveChanges:=True
xlApp.Quit
End Sub
I am able to open .xlsx file by CreateObject("Excel.Application").Workbooks.Open("path")
Something like this is not allowing me to create a new Excel Workbook via a PowerPoint Macro.
Set ExcelFile = CreateObject("Excel.Application")
ExcelFile.Workbooks.Add
ExcelFile.ActiveWorkbook.SaveAs "path"
sample code, just checked on PP 2016:
(remember to close xlsApp, set obj to nothing etc.)
Public Sub StackOverflow()
Dim xlsApp As Object
Dim wkbWorking As Object
Set xlsApp = CreateObject("Excel.Application") 'basically it opens excel application
Set wkbWorking = xlsApp.Workbooks.Add 'it creates new workbook in just opened excel
xlsApp.Visible = True 'makes excel visible
wkbWorking.SaveAs "C:\Temp\PesentationExcel.xlsx"
wkbWorking.Close 'closes workbook
xlsApp.Quit 'closes excel application
'sets variables to nothing
Set wkbWorking = Nothing
Set xlsApp = Nothing
End Sub
I am trying to have a vba macro in PowerPoint to open and retrieve information from an Excel file.
The Excel file is saved in a SharedPoint and I am able to open it. However, when I want to refer to the Workbook as to retrieve information from it's cells, it gives me the following error: Automation error invalid syntax -2147221020 (800401e4)
Here is the code I have so far:
Sub test()
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet
Application.DisplayAlerts = False
'open excel from sharedpoint
ActivePresentation.FollowHyperlink _
Address:="http://teams.blabla.com/Shared%20Documents/Overview_NPD_Projects.xlsm", _
NewWindow:=True, AddHistory:=True
Set wb = GetObject("http://teams.blabla.com/Shared Documents/Overview_NPD_Projects.xlsm")
' this does not work even when I change it to "Shared%20Documents"
Set ws = wb.Worksheets("Project List")
auxvariable = ws.Range("B2").Value
End Sub
The funny thing is that it works if I try to save the excel file manually to my desktop before running the macro and change the the path to:
Set wb = GetObject("C:\Users\Filipe.freitas\Desktop\Overview_NPD_Projects.xlsm")
I would save the file to the desktop and use the correct path, but I would need to refer to the excel file before doing it.
Any help would be greatly appreciated!
Have you tried http://www.cpearson.com/Excel/DownloadFile.aspx and then
Dim xlapp as object
xlapp = createobject("Excel.Application")
set wb = xlapp.workbooks.open("thefilenamewhereyousavethedownload")
I am trying to have Access 2007 open an Excel spreadsheet, add data to the cells, and then save the spreadsheet. I have a button in an Access 2007 form which activates the following code. Here is what I have so far, but every "save command" doesn't work. Does any one have any idea what to use?
Private Sub buttonExcel_Click()
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
' Create a New Excel WorkBook
Set oExcel = CreateObject("Excel.Application")
' Optional, Open a current Workbook from a file directory
oExcel.Workbooks.Open ("Z:\08_Volume Management\ACCESS\EMAILTEMPLATES\test.xlsx")
' Make the Excel Workbook visible to the users
oExcel.Visible = True
' Define the Workbook from Excel
Set oBook = oExcel.ActiveWorkbook
' Define the Workskeet from the Workbook (1="Sheet1", 2="Sheet2", etc...)
Set oSheet = oBook.Worksheets(1)
' Write Data to the Worksheet (Block "A1" is the first row, first column)
oSheet.Range("A1").Value = "Hello World"
' Save the Excel Workbook
oExcel.SaveAs ("Z:\08_Volume Management\ACCESS\EMAILTEMPLATES\test.xlsx") ' DOESNT WORK
oExcel("Z:\08_Volume Management\ACCESS\EMAILTEMPLATES\test.xlsx").Save ' DOESNT WORK
oBook.SaveAs ("Z:\08_Volume Management\ACCESS\EMAILTEMPLATES\test.xlsx") ' DOESNT WORK
oBook("Z:\08_Volume Management\ACCESS\EMAILTEMPLATES\test.xlsx").Save ' DOESNT WORK
' Closes Excel as asks to save the Workbook
oExcel.Workbooks.Close
' Quit Excel
'oExcel.Quit
End Sub
Thanx!
Private Sub buttonExcel_Click()
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
Set oExcel = CreateObject("Excel.Application")
Set oBook = oExcel.Workbooks.Open ("Z:\08_Volume Management\" & _
"ACCESS\EMAILTEMPLATES\test.xlsx")
oExcel.Visible = True
Set oSheet = oBook.Worksheets(1)
oSheet.Range("A1").Value = "Hello World"
oBook.Save
oBook.Close
'or...
'oBook.Close True 'True = save changes
oExcel.Quit
End Sub
I have an Excel 2007 workbook that contains an ODBC data connection (to FoxPro, if that matters). The connection is set to "refresh data when opening the file."
When I go into File Explorer and open the workbook, the data populates into the spreadsheet as it should. However, when I execute a function in Access VBA that opens the workbook, the data from the ODBC connection does not populate.
Why would it make a difference which way the workbook is opened? And more importantly, how can I get the data to populate when the workbook is opened via Access VBA?
Here is the Access VBA code that opens the workbook:
Public Sub Subform_cmdOpenFile_Click(frm As Form)
Dim rs As Recordset
Dim ftiSuperclass As FilingTemplateInterface
Set rs = frm.RecordsetClone
If (rs.BOF Or rs.EOF) Then GoTo PROC_EXIT
Set ftiSuperclass = New FilingTemplateInterface
ftiSuperclass.ShowWorkbook rs!Directory & frm!Filename
PROC_EXIT:
On Error Resume Next
rs.Close
Set rs = Nothing
ftiSuperclass.QuitExcel
Set ftiSuperclass = Nothing
Exit Sub
PROC_ERROR:
Resume PROC_EXIT
End Sub
Friend Sub ShowWorkbook(strFilename As String)
Dim fso As New Scripting.FileSystemObject
Dim appExcel As New Excel.Application
appExcel.Workbooks.Open Filename:=strFilename, AddToMRU:=True
appExcel.visible = True
Set appExcel = Nothing
End Sub
Resolved by adding the line of code noted below, to force connection refresh on open:
Friend Sub ShowWorkbook(strFilename As String)
Dim fso As New Scripting.FileSystemObject
Dim appExcel As New Excel.Application
appExcel.Workbooks.Open Filename:=strFilename, AddToMRU:=True
appExcel.ActiveWorkbook.Connections("ConnectionName").Refresh 'added this line
appExcel.visible = True
Set appExcel = Nothing
End Sub