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
 
yeah i agree, well if that is indeed a valid file path it should resolve the error about the file missing or path being invalid... it looks like it no longer gets cutoff... vba i know has problems with file paths and spacing... it might also require you surround the file path with single quotes... perhaps that is only an excel thing but worth a try if it isnt accepting the path still. When I say path i also mean the entire filename not just the directory like you describe in the code... i would change the name of 'path' to 'directory' to make things more clear to others who read your code and you can concatenate attachment this way...

Code:
directory & "\" & Filename

i would remain consistent with variable capitalization... go with camelCase... makes it much easier for others to read code when your naming is consistent and describes the object.

Code:
attachment = directory & "\" & fileName
dim the variables with these names imo is better, local scope variables should be camel case

If you are curious I would lookup the .Net naming convention and use that... it describes how different programming objects should be named. This all doesnt matter but it matters when you ask others to look at your code. ;)
 
Last edited:
Upvote 0

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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