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

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.

Related

Disable excel save changes prompt

I have tried all the ways I normally do this and what I can find searching. oxl.DisplayAlerts = False is not working. I still get asked if I want to save changes.
I am essentially trying to use the excel sheet as a template. The full script exports to pdf, but this is enough to re-create the problem. BTW I tried saving it as a xltx file (template) and still get the save promt.
Dim oxl As New Excel.Application
Dim apppath2 As String = My.Application.Info.DirectoryPath.ToString
Dim mywb As Excel.Workbook = oxl.Workbooks.Open(Filename:=apppath2 & "\fuse template.xlsx", [ReadOnly]:=True)
oxl.Visible = False
Dim mysheet As Excel.Worksheet = mywb.Sheets(1)
mysheet.Cells(10, 5) = l_region.Text
mysheet.Cells(11, 5) = comb_emc_name.Text
oxl.DisplayAlerts = False
mywb.Close(False)
mysheet = Nothing
mywb = Nothing
oxl = Nothing
GC.Collect()
I was missing mywb.Saved = True. I have never had to do that before.

How to retrieve data from Excel and add to Word

I have a Word template file that retrieves data from an Excel file to populate a form.
The code looks something like this:
Dim myXL As Object
Set myXL = Getobject("myfile.xls")
myXL.Application.Visible = True
myXL.Parent.Windows(1).Visible = True
This code works fine in Office 2010 and 2007, but when I try it in 2013, it gives run time error 9 which is an array subscript error. When I check the Windows array it has zero elements, so error is correct.
How do I achieve the same result in 2013?
The next bit of code attempts to access the Worksheets("mysheet") and if I skip the Visible = True line accessing the worksheet gives runtime error 1004.
Any help with fixing this would be greatly appreciated.
To make the code work on Office 2013 I added the line myXL.Activate before trying to make the Window visible. So the code becomes:
Dim myXL As Object
Set myXL = Getobject("myfile.xls")
myXL.Application.Visible = True
myXL.Activate
myXL.Parent.Windows(1).Visible = True
This fixed the run-time error, and the code went back to working well.
To retrieve data from an Excel
An Example would be...
Option Explicit
Sub ExcelData()
Dim xlApp As Object ' Application
Dim xlBook As Object ' Workbook
Dim xlSht As Object ' Worksheet
Dim FilePath As String
FilePath = "C:\Temp\Book1.xlsx"
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open(FilePath)
Set xlSht = xlBook.Sheets("Sheet1")
With ActiveDocument
.Content = xlSht.Range("A1").Value
End With
xlApp.Visible = True
Set xlApp = Nothing
Set xlBook = Nothing
End Sub

Excel 2010 - 'The image part with relationship ID rId1 was not found in the file.' When copying sheets of a workbook into another workbook

I have a source excel workbook (XLSM format) that has sheets containing images. These sheets are programatically copied into another(target) workbook (XLSM) using VB.NET late binding. After all the copy is done i save the workbook and launch it. In the opened excel workbook I see the error message 'The image part with relationship ID rId1 was not found in the file' at all places where the images are placed.
All the operations are done in the client machine no server side code available.
Interestingly this issue doesn't occur in Excel 2013 and it displays the images properly issue is observed only in 2010 and 2007. Is this a know bug in Excel 2010 and 2007 if yes can any one provide me the official link to the ticket so that i can track the issue and get the Hot fix once it is available.
Dim SourceExcelWorkbook As Excel.Workbook = Nothing
Dim TargetExcelWorkbook As Excel.Workbook = Nothing
Dim TargetExcelSheets As Excel.Sheets = Nothing
Dim SourceExcelSheets As Excel.Sheets = Nothing
Dim CopyWorkSheet As Excel.Worksheet = Nothing
Dim XLApp As New Excel.Application
XLApp.Visible = False
XLApp.DisplayAlerts = False
XLApp.ScreenUpdating = False
Dim pobjExcelWorkbooks As Excel.Workbooks = XLApp.Workbooks
SourceExcelWorkbook = pobjExcelWorkbooks.Open("source file path")
TargetExcelWorkbook = pobjExcelWorkbooks.Open("target file path")
TargetExcelSheets = TargetExcelWorkbook.Worksheets
SourceExcelSheets = SourceExcelWorkbook.Worksheets
Dim OriginalSheetCount As Integer = TargetExcelSheets.Count
Dim SheetCount As Integer = OriginalSheetCount
Dim SheetsToBeCopiedCount As Integer = SourceExcelSheets.Count
While SheetsToBeCopiedCount > 0
Dim lobjAfterSheet As Object = TargetExcelSheets.Item(SheetCount)
CopyWorkSheet = SourceExcelSheets.Item(1)
CopyWorkSheet.Move(After:=lobjAfterSheet)
SheetCount = SheetCount + 1
TargetExcelWorkbook.Save()
SheetsToBeCopiedCount = SheetsToBeCopiedCount - 1
End While
TargetExcelWorkbook.Save()

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.

MS Access 2003 - Embedded Excel Spreadsheet on Access form

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.

Resources