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.
Related
I am working on an application that stores it's files in the Documents folder to easily sync with One Drive. I need to make them available offline, in case of an outage. I have changed the settings so that I have a local copy. When I click directly on the Excel file in File Explorer, the program opens it with no problem. However, when I try to open the file in my application, I get an error stating "No network found..." etc. Is there another way to do this? Thanks in advance.
Public Sub LoadMemberList()
lstMemberList.Clear()
Dim mMember As Members
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim range As Excel.Range
xlApp = New Excel.Application
xlBook = xlApp.Workbooks.Open(CurrentYearPath) '***This is where the exception is thrown.
xlSheet = xlBook.Worksheets("sheet1")
range = xlSheet.UsedRange
Dim rs As Object(,) = CType(range.Value, Object(,))
Dim records As Long = rs.GetUpperBound(0)
If records > 1 Then
For x = 2 To records
mMember.MemberNumber = FormatNumber(rs(x, 1))
mMember.MemberName = rs(x, 4) + " " + rs(x, 3)
lstMemberList.Add(mMember)
Next
End If
xlBook.Close()
xlApp.Quit()
KillExcel()
xlApp = Nothing
xlBook = Nothing
xlSheet = Nothing
range = Nothing
End Sub
Public Function CurrentYearPath() As String
Dim CurrentYear As Integer
CurrentYear = CInt(Format(Now, "yyyy"))
Return My.Computer.FileSystem.SpecialDirectories.MyDocuments + "\DailyBook\Rosters\Membership" + CurrentYear.ToString + ".xlsx"
End Function
Scroll down here
https://learn.microsoft.com/en-us/onedrive/plan-onedrive-enterprise#key-onedrive-features
and you'll find this,
However, if you plan to access a file while disconnected from the internet, you can make the file available offline by right-clicking it, and then selecting Always keep on this device.
This question already has an answer here:
Excel VBA: Answer gets "stuck"
(1 answer)
Closed 4 years ago.
I'm trying to open a file using a function. The same code is working if I call it by a button. The file will open, I can modify it and I can close it without saving. But if I use the same code called by the function the file will not be open. Why? Following the code used with the button and with the function. Might anyone help me? This is only the beginning of what I would need. Detailing, with this function I need to open a CSV file, fix the CSV file, extract a data with a Dlookup and write this data on the cell where I called the function. This works if I call my code by button but not if I call it with a function in a cell
Sub Button1_Click()
Dim path As String
Dim xl As Excel.Application
Dim wk As Excel.Workbook
path = ActiveWorkbook.path & "\Book1.xlsm"
Set xl = Excel.Application
Set wk = xl.Workbooks.Open(path)
xl.Visible = True
wk.Activate
xl.DisplayAlerts = False
Range("B2").Select
Range("B2").Value = "hello world"
wk.Saved = False
wk.Close
Set wk = Nothing
End sub
Public Function aprifile()
Dim path As String
Dim xl As Excel.Application
Dim wk As Excel.Workbook
path = ActiveWorkbook.path & "\Book1.xlsm"
Set xl = Excel.Application
Set wk = xl.Workbooks.Open(path)
xl.Visible = True
wk.Activate
xl.DisplayAlerts = False
Range("B2").Select
Range("B2").Value = "hello world"
wk.Saved = False
wk.Close
Set wk = Nothing
End Function
There's a couple of things here.
First I have made the thing a Sub rather than a Function as you're not returning anything.
Option Explicit
Public Sub aprifile()
Dim path As String
Dim xl As Application
Dim wk As Workbook
path = ActiveWorkbook.path & "\Book1.xlsm"
If Len(Dir$(path)) > 0 Then
Set xl = Excel.Application
Set wk = xl.Workbooks.Open(path)
xl.Visible = True
wk.Activate
xl.DisplayAlerts = False
wk.ActiveSheet.Range("B2").Select
wk.ActiveSheet.Range("B2").Value = "hello world"
wk.Saved = False
wk.Close
Set wk = Nothing
End If
End Sub
The second thing is that I placed 'wk.ActiveSheet' to make sure that I am writing to the correct sheet. Without that I found that I was writing to the wrong workbook.
The other thing I did, as you can see, I checked to see if the file existed. I found that for my first run it didn't so that may give you a clue there.
But, I can assure you that this code works because I've just done it here.
Hope that this helps
Malc
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
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()
I am having a very strange problem with this code. The general purpose is to save user data from a form in Access to a spreadsheet in Excel, and then use an email client to send an email containing the spreadsheet attachment. The code is as follows
Private Sub Send_Email_Click()
Dim MySheetPath As String
Dim Xl As Excel.Application
Dim XlBook As Excel.Workbook
Dim XlSheet As Excel.Worksheet
' Tell it location of actual Excel file
MySheetPath = "\\SERVER\Users\Public\Documents\WORK ORDERS\Blank Work Order.xlsx"
'Open Excel and the workbook
Set Xl = CreateObject("Excel.Application")
Set XlBook = GetObject(MySheetPath)
'Make sure excel is visible on the screen
Xl.Visible = True
XlBook.Windows(1).Visible = True
'Define the sheet in the Workbook as XlSheet
Set XlSheet = XlBook.Worksheets(1)
'Insert values in the excel sheet starting at specified cell
XlSheet.Range("B6") = Jobnameonform.Value
XlSheet.Range("C7") = Companynameonform.Value
XlSheet.Range("C8") = Employeename.Value
XlSheet.Range("H7") = Jobnumberonform.Value
Xl.ActiveWorkbook.Save
Xl.ActiveWorkbook.Close
Xl.Quit
'in case something goes wrong
Set Xl = Nothing
Set XlBook = Nothing
Set XlSheet = Nothing
Dim cdomsg
Set cdomsg = CreateObject("CDO.message")
With cdomsg.Configuration.Fields
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'NTLM method
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/smptserverport") = 587
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "matthewfeeney6#gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "REDACTED"
.Update
End With
' build email parts
With cdomsg
.To = "matthewfeeney6#gmail.com"
.From = "matthewfeeney6#gmail.com"
.Subject = "Test email"
.TextBody = "Did you get the attachment?"
.AddAttachment "\\SERVER\Users\Public\Documents\WORK ORDERS\Blank Work Order.xlsx"
.Send
End With
Set cdomsg = Nothing
MsgBox "Completed"
End Sub
Without the line ".AddAttachment..." The code works exactly as intended, minus sending the attachment of course. However, with that line, I get a runtime error 91, with the debugger citing the line "Xl.ActiveWorkbook.Save" as the problematic code. Also, without the code to modify the excel spreadsheet, the simple email portion does work, attachments included. If anyone can provide insight as to why I am getting this error, that would be very helpful. Thanks in advance!
EDIT: Retesting the code, it seems to consistently crash at Xl.ActiveWorkbook.Save I thought it worked before, but I must have been mistaken
You (think you) are saving and closing your workbook with:
Xl.ActiveWorkbook.Save
Xl.ActiveWorkbook.Close
but that's not the workbook you're using and manipulating, which is XlBook:
Set XlBook = GetObject(MySheetPath)
If you save and close the "real" workbook, XlBook:
XlBook.Save
XlBook.Close
then it should work.
The reason you're getting the error at the Save call probably means that the Xl.ActiveWorkbook object doesn't exist/is null or something.