Referencing file from One Drive

jaihawk8

Board Regular
Joined
Mar 23, 2018
Messages
69
Office Version
  1. 2016
Platform
  1. Windows
I have written this script, which works perfectly. Unfortunately, when I change the path in B2 for the file path to try to reference my OneDrive path, it blows it up. This is actually going to be run on another person's computer, so ideally, I would like it to find the file, no matter who runs the report. Here's the scripting:

Sub Send_email_fromexcel()
Dim Edress As String
Dim Subject As String
Dim Message As String
Dim Filename As String
Dim outlookapp As Object
Dim myAttachments As Object
Dim path As String
Dim lastrow As Integer
Dim Attachment As String
Dim x As Integer


x = 6
y = 2
Z = 3


Do While Sheets("Austin").Cells(x, 1) <> ""


Set outlookapp = CreateObject("Outlook.Application")
Set outlookmailitem = outlookapp.createitem(0)
Set myAttachments = outlookmailitem.Attachments


path = Sheets("Austin").Range("B2")
Edress = Sheets("Austin").Cells(x, 1)
Subject = Sheets("Austin").Cells(x, 2)
Filename = Sheets("Austin").Cells(x, 3)
Attachment = path + Filename


outlookmailitem.To = Edress
outlookmailitem.cc = ""
outlookmailitem.bcc = ""
outlookmailitem.Subject = Subject
outlookmailitem.body = Sheets("Austin").Cells(Z, 2)


myAttachments.Add (Attachment)
outlookmailitem.display
outlookmailitem.send


lastrow = lastrow + 1
Edress = ""


x = x + 1


Loop


Set outlookapp = Nothing
Set outlookmailitem = Nothing




End Sub
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
When I change B2 (Path) to: C:\Users\jasonb\NEOPOST\Evans, Pamela - Central District Payroll\2018 Payroll Files\May 18 Earnings Statements\AU

It fails on:

myAttachments.Add (Attachment)

Run-time error '-2147024894(80070002)':
Cannot find this file. Verify the path and file name are correct.
 
Upvote 0
when you debug... what is the exact value this operation creates?

Code:
[COLOR=#333333]path + Filename[/COLOR]

that value appears to be invalid... you probably are missing a '\' if i had to guess

so your path in B2 doesnt have a trailing '\' and im guessing your filename does not have a leading '\' so that would create an invalid path
 
Upvote 0
when you debug... what is the exact value this operation creates?

Code:
[COLOR=#333333]path + Filename[/COLOR]

that value appears to be invalid... you probably are missing a '\' if i had to guess

so your path in B2 doesnt have a trailing '\' and im guessing your filename does not have a leading '\' so that would create an invalid path

I added a \ at the end of the path, but I still get the same error message when debugging.
 
Upvote 0
what was the value that got created and saved in the Attachment variable?

have a messagebox show it to you or breakpoint on the myAttachments.Add (Attachment) line and hover over attachment and what is the value?

i assume the directory the file is located is what you are calling 'path'
 
Last edited:
Upvote 0
what was the value that got created and saved in the Attachment variable?

have a messagebox show it to you or breakpoint on the myAttachments.Add (Attachment) line and hover over attachment and what is the value?

i assume the directory the file is located is what you are calling 'path'

I am not sure how to have a messagebox show it to me. I did hover over it and it says:

(Attachment) = "C:\Users\jasonb\NEOPOST\Evans,Pamela - Central District Pay... and then cuts off.
 
Upvote 0
replace this line...

Code:
[COLOR=#333333]Attachment = path + Filename[/COLOR]

with these lines...

Code:
MsgBox "path is... " & vbCrLf & vbCrLf & path
MsgBox "Filename is... " & vbCrLf & vbCrLf & Filename
[COLOR=#333333]Attachment = path & Filename
[/COLOR]MsgBox "Attachment after concatenation is... " & vbCrLf & vbCrLf & Attachment

so each message box will tell you what values you are working with if they are wrong then your code wont work... MsgBox lines can be deleted later once you figure out the bug

i just noticed the error... in vba you use & to concatenate... c languages use +
 
Last edited:
Upvote 0
i think your error is actually you need to use ampersand to concatenate... path & Filename
 
Upvote 0
i think your error is actually you need to use ampersand to concatenate... path & Filename

Here is what I get in the message boxes:

path is...

C:\Users\jasonb\NEOPOST\Evans, Pamela - Central District Payroll\2018 Payroll Files\May 18 Earnings Statements\AU\

Filename is...

Brandi Andel Commission Payout 05.04.18xlsx

Attachment after concatenation is...

C:\Users\jasonb\NEOPOST\Evans, Pamela - Central District Payroll\2018 Payroll Files\May 18 Earnings Statements\AU\Brandi Andel Commission Payout 05.04.18xlsx

Everything looks good there...
 
Upvote 0

Forum statistics

Threads
1,224,830
Messages
6,181,225
Members
453,025
Latest member
Hannah_Pham93

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top