MS Access 2003 - Embedded Excel Spreadsheet on Access form - excel

Let's say I have an embedded Excel Spreadsheet on a Microsoft Access form. I call the object frame
ExcelFrame
and I add a text box on the form called
txtA1
and I add a button on the form called
cmdInsert
I want to type "Hello World" into the text box, click the button and have it appear in the A1 cell on that spreadsheet. What VBA do I use to accomplish this?
Thanks

You can automate Excel, write your value to the worksheet, then update the object frame.
Private Sub cmdInsert_Click()
Dim strPath As String
Dim oExcel As Object
Dim oSheet As Object
Set oExcel = CreateObject("Excel.Application")
oExcel.Visible = True
strPath = Me.ExcelFrame.SourceDoc
'Debug.Print strPath '
oExcel.Workbooks.Open strPath
Set oSheet = oExcel.ActiveSheet
'Debug.Print oSheet.Name '
oSheet.Range("A1").Value = Me.txtA1
oExcel.ActiveWorkbook.Save
oExcel.Quit
Set oSheet = Nothing
Set oExcel = Nothing
'acOLEUpdate action requires Enabled = True '
'and Locked = False '
Me.ExcelFrame.Enabled = True
Me.ExcelFrame.Locked = False
Me.ExcelFrame.Action = acOLEUpdate
Me.txtA1.SetFocus
Me.ExcelFrame.Enabled = False
Me.ExcelFrame.Locked = True
End Sub
Edit: The example was based on an external workbook file which is linked as the source for the form's object frame.
To link a worksheet, choose the "Create from File" radio button, check the "Link" check box, and browse to select the workbook. That's the way I did it with Access 2007. As I recall, it was similar with Access 2003.

It's never too late, right ?
Provide that you have already created the Excel object (as in OP):
Dim wb As Excel.Workbook, ws As Excel.Worksheet
Set wb = Me.ExcelFrame.Object
Set ws = wb.Worksheets(1)
ws.range("a1")= "Hello world"
Note that this code requires a reference to MS Excel in VBA.

Related

How to unlock Excel file after opening it trought VBA Word

I'm working on a VBA Word macro to create a personalized agenda. This is done trought the steps:
From VBA Word, open an Excel Spreadsheet
Copy a pre-defined range from the spreadsheet, into an array in Word
Create new Word document, inserting every row from the array on an individual page.
Thanks to the help I found here and on a few websites, I wrote a piece of code that executes these three steps.
After running the code and even closing Word, I get a message everytime I open the spreadsheet in Excel. The message is:
File.xlsm is blocked for edition by 'User' ; (my user)
I think it might be related to:
Calling Excel from VBA Word: I call Excel by creating an ApplicationObject (app_Excel) and using CreateObject method; to finish the application, I used Set app_Excel = Nothing, and then app_Excel.Quit, as seen in the code; Or
Opening a spreadsheet from VBA Word: In the first attempts I was using Excel.Workbooks.Open(,,,ReadOnly = False), then I changed it to True, with the same result.
Sub main()
'Main procedure
Word.Application.ScreenUpdating = False
Dim app_Excel As Excel.Application
Set app_Excel = CreateObject("Excel.Application")
Dim wbk_srce As Workbook
Set wbk_srce = app_Excel.Workbooks.Open("C:\0_portolon\Dias.xlsm", , True)
Dim wsh_srce As Worksheet
Set wsh_srce = wbk_srce.Worksheets(3)
wsh_srce.Activate
cell_1 = CStr("A1")
cell_2 = CStr("D216")
Dim header_range As Excel.Range
wsh_srce.Range(cell_1, cell_2).Select
Set header_range = Excel.Selection
Dim header_array() As Variant
header_array = header_range.Value
Set header_range = Nothing
Set wsh_srce = Nothing
Set sbk_srce = Nothing
app_Excel.Quit
'Creates Word document
Call create_agenda(header_array)
End Sub
How can I unlock the spreadsheet after copying its data into Word?
Thanks in advance,
Tiago
Following the suggestion given in the comments, I changed Set wbk = Nothing to wbk.Close; the code works correctly now.
Thanks.
Sub main()
'Main procedure
Word.Application.ScreenUpdating = False
Dim app_Excel As Excel.Application
Set app_Excel = CreateObject("Excel.Application")
Dim wbk_srce As Workbook
Set wbk_srce = app_Excel.Workbooks.Open("C:\0_portolon\Dias.xlsm", , True)
Dim wsh_srce As Worksheet
Set wsh_srce = wbk_srce.Worksheets(3)
wsh_srce.Activate
cell_1 = CStr("A1")
cell_2 = CStr("D216")
Dim header_range As Excel.Range
wsh_srce.Range(cell_1, cell_2).Select
Set header_range = Excel.Selection
Dim header_array() As Variant
header_array = header_range.Value
Set header_range = Nothing
Set wsh_srce = Nothing
wbk_srce.Close 'change
app_Excel.Quit
'Creates Word document
Call create_agenda(header_array)
End Sub

Use Word Content Control Values for chart object in same Word doc

Using MS Word (in my case 2010 version), I have constructed a form with Content Control elements to be filled out by the user. Now I want certain entries (that I already gave titles to) be shown in a chart inside the same Word document (not in a separate Excel document).
This should be an automated process, so that if the user changes one of the Content Control entries, the chart updates itself automatically; I would also be OK if the user had to press a button in order to update the chart (but the user shouldn't have to click around a lot, since I must assume the user to have little skills.)
So I inserted an Excel chart object in my Word form document. I also wrote some VBA code inside this Excel object to read the Content Control values from the Word document as source for the chart. But I think what I really need is the VBA code to be in my Word document itself (for example to be executed upon click on a button by the user), yet I don't know how to address the Excel chart object and the cells within.
My VBA code inside the Excel object is:
Sub ChartDataAcquirer()
Dim wdApp As Object
Dim wdDoc As Object
Dim DocName As String
Dim ccX As String
Dim ccY As String
Dim datapairs As Integer
'''''''''' Variables '''''''''
DocName = "wordform.docm"
ccX = "titleX"
ccY = "titleY"
datapairs = 5
''''''''''''''''''''''''''''''
Set wdApp = GetObject(, "Word.Application")
Set wdDoc = wdApp.Documents(DocName)
Dim i As Integer
For i = 1 To datapairs
With ActiveSheet.Cells(i + 1, 1) ' The first row contains headline, therefore i+1
.Value = wdDoc.SelectContentControlsByTitle(ccX & i).Item(1).Range.Text ' The CC objects containing the x values have titles "titleX1", "titleX2" ..., therefore "ccX & i"
On Error Resume Next
.Value = CSng(wdDoc.SelectContentControlsByTitle(ccX & i).Item(1).Range.Text) ' To transform text into numbers, if user filled the CC object with numbers (which he should do)
End With
With ActiveSheet.Cells(i + 1, 2)
.Value = wdDoc.SelectContentControlsByTitle(ccY & i).Item(1).Range.Text
On Error Resume Next
.Value = CSng(wdDoc.SelectContentControlsByTitle(ccY & i).Item(1).Range.Text)
End With
Next
End Sub
I guess I need a similar code that is placed in and operates from the Word form document itself, but that is where I am stuck...
The following is demo code that shows how to access an embedded Excel chart.
Note that the Name (Shapes([indexValue])) of your chart Shape is probably different than in this code. You'll need to check and change that assignment. Also, your chart may be an InlineShape rather than a Shape, so you may need to adjust that bit, as well.
This code checks whether the Shape is actually a chart. If it is, the Chart object is accessed as well as its data sheet. Via that, it's possible to get the actual workbook, the worksheets, even the Excel application if you should need it.
Sub EditChartData()
Dim doc As Word.Document
Dim shp As Word.Shape
Dim cht As Word.Chart
Dim wb As Excel.Workbook, ws As Excel.Worksheet, xlApp As Excel.Application
Set doc = ActiveDocument
Set shp = doc.Shapes("MyChart")
If shp.HasChart Then
Set cht = shp.Chart
cht.ChartData.Activate
Set wb = cht.ChartData.Workbook
Set xlApp = wb.Application
Set ws = wb.ActiveSheet
Debug.Print ws.Cells(1, 2).Value2
End If
Set ws = Nothing
Set wb = Nothing
Set cht = Nothing
Set xlApp = Nothing
End Sub

Extract Outlook body to Excel VBA

after searching multiple things, and getting errors
How do I upon pressing "f5" in a vba script copy the body of an email into an excel sheet /csv
where every line = a new cell below.
Thanks
Sorry, this is causing me nothing but trouble.
What I have tried so far
http://smallbusiness.chron.com/export-outlook-emails-excel-spreadsheets-41441.html
How to copy Outlook mail message into excel using VBA or Macros
http://www.vbforums.com/showthread.php?415518-RESOLVED-outlook-the-macros-in-this-project-are-disabled
http://www.ozgrid.com/forum/showthread.php?t=181512
and a few more, last year.
This will work for you. we are basically splitting the email body into an array based on a new line. Notice that this will yield blank cells if you had a blank line in the email body.
Public Sub SplitEmail() ' Ensure reference to Word and Excel Object model is set
Dim rpl As Outlook.MailItem
Dim itm As Object
Set itm = GetCurrentItem()
If Not itm Is Nothing Then
Set rpl = itm.Reply
rpl.BodyFormat = olFormatHTML
'rpl.Display
End If
Dim objDoc As Word.Document
Set objDoc = rpl.GetInspector.WordEditor
Dim txt As String
txt = objDoc.Content.text
Dim xlApp As Excel.Application
Set xlApp = CreateObject("Excel.application")
xlApp.Visible = True
Dim wb As Excel.Workbook
Set wb = xlApp.Workbooks.Add
Dim i As Long
For i = LBound(Split(txt, Chr(13)), 1) To UBound(Split(txt, Chr(13)), 1)
wb.Worksheets(1).Range("A" & i + 1).Value = Split(txt, Chr(13))(i)
Next i
End Sub
Function GetCurrentItem() As Object
Dim objApp As Outlook.Application
Set objApp = Application
On Error Resume Next
Select Case TypeName(objApp.ActiveWindow)
Case "Explorer"
Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
Case "Inspector"
Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
End Select
GetCurrentItem.UnRead = False
Set objApp = Nothing
End Function
The Outlook object model doesn't recognize lines in the body. You can try to resize any inspector window in Outlook and see how the body lines are changed.
Anyway, you may try to use the Word object model to get the exact lines. Outlook uses Word as an email editor. The WordEditor property of the Inspector class returns an instance of the Document class which represents the message body. You can read more about all possible ways in the Chapter 17: Working with Item Bodies article.
The How to automate Microsoft Excel from Visual Basic article explains how to automate Excel from any external application.

Use Access VBA to check-out an Excel doc from Sharepoint

I have a rather odd situation, the below code can successfully checkout a file from sharepoint to Excel...
Private sub checkoutfromSP()
Dim loc as String
loc = "Location"
if Workbooks.CanCheckOut(loc) = true then
Workbooks.CheckOut loc
end if
However how does this translate into Access? I always receive the error "This document cannot be checked out" with the following code?
Dim objXL as Excel.Application
Dim loc as String
loc = "Location"
objXL = new Excel.Application
if objXL.Workbooks.CanCheckOut(loc) = True then
objXL.Workbooks.CheckOut loc
end if
Reason for the checkout via Access is there are a few pieces of data that need dropped into Excel from Access, however as the Excel file is on sharepoint I need to checkout/checkin to submit the changes.
Open the document with your Excel instance before checking it out and it should work for you:
Dim objXL As Excel.Application
Dim objWB As Excel.Workbook 'NEW
Dim loc As String
loc = "Location"
Set objXL = New Excel.Application 'Make sure you use Set here
If objXL.Workbooks.CanCheckOut(loc) = True Then
Set objWB = objXL.Workbooks.Open(loc) 'NEW
objXL.Workbooks.CheckOut loc
End If
When you check the workbook back in with the line objWB.CheckIn, Excel automatically closes the Workbook object.

ms-access vba - read from excel and also update that excel

Created a simple access DB with only 1 form and 1 one button to run code that opens an existing empty excel (with 1 worksheet) and writes "X" in its 1st cell. It does the job but the workbook is hidden and I have to manually unhide it. That is, after the VBA code is executed I open the excel file and it is all grayed out. I have to click the "view" tab and then select the "Unhide" option and all is fine and I can see that the cell was updated as needed. If I take out the VBA line that writes "X" in the excel file, it doesn't hide the workbook. How do I solve the problem of the workbook being hidden?
Windows 7 and Office2013.
Thank you!!!
Here is the code:
Private Sub Command0_Click()
Dim my_xl_app As Object
Dim my_xl_worksheet As Object
Dim my_xl_workbook As Object
Set my_xl_app = CreateObject("Excel.Application")
my_xl_app.UserControl = True
my_xl_app.Visible = False ' yes. I know it's the default
Set my_xl_workbook = GetObject("D:\Dropbox\MASAV\HIYUVIM\AAA.xlsx")
Set my_xl_worksheet = my_xl_workbook.Worksheets(1)
my_xl_worksheet.Cells(1, "A") = "V"
my_xl_workbook.Close SaveChanges:=True
Set my_xl_app = Nothing
Set my_xl_workbook = Nothing
Set my_xl_worksheet = Nothing
End Sub
S o l v e d !!!
Here is the code that works without hiding my entire workbook :
Private Sub Command0_Click()
Dim my_xl_app As Object
Dim my_xl_worksheet As Object
Dim my_xl_workbook As Object
Set my_xl_app = CreateObject("Excel.Application")
Set my_xl_workbook = my_xl_app.Workbooks.Open("D:\Dropbox\MASAV\HIYUVIM\AAA.xlsx")
Set my_xl_worksheet = my_xl_workbook.Worksheets(1)
my_xl_workbook.Sheets(1).Range("A1").Value = "V"
my_xl_workbook.Close SaveChanges:=True
Set my_xl_app = Nothing
End Sub
Got the answer right here in this this forum, in another thread which escaped my eyes...
Thanks a lot to all in this wonderful forum!!!!
Use this:
Workbooks(1).Windows(1).Visible = True

Resources