Issues with thisworkbook.path

AndreasL

New Member
Joined
Dec 8, 2021
Messages
18
Office Version
  1. 365
Platform
  1. Windows
I've searched everywhere and I've tried using the tips I've come across, however I still run into isses using Thisworkbook.path.
This code works fine if I use $Environ(username), and put the path, but I'm not sure the other ones in my office has the onedrive folder saved in the same location.
It would then also only work if synced to PC, so I'm trying to work around this solution.
Turning off sync to cloud is not an option as there are several working with this sheet daily.

My code is as follows now, please help me locate the issue. (I get path/access error (75) even tho Thisworkbook.path works in other macros exporting PDF to folders in the same location).
It seems neither the Exportpath or the LoadPicture is working with this at the moment, but first of all I'm stuck with this line:

VBA Code:
.Export Filename:=Exportpath, FilterName:="bmp"

VBA Code:
 Set oWs = Ark3
 Set oRng = oWs.Range("A1:O36")
 Exportpath = ThisWorkbook.Path & "/" & "temp" & "/" & "PLAN.bmp"

 oRng.CopyPicture xlScreen, xlPicture
 lWidth = oRng.Width
 lHeight = oRng.Height
    Worksheets("PLAN").Unprotect
 Set oChrtO = oWs.ChartObjects.Add(Left:=0, Top:=0, Width:=lWidth, Height:=lHeight)

 oChrtO.Activate
 With oChrtO.Chart
  .Paste
  .Export Filename:=Exportpath, FilterName:="bmp"
 End With

 oChrtO.Delete
 
 IMGplanen.Picture = LoadPicture(ThisWorkbook.Path & "/" & "temp" & "/" & "PLAN.bmp")
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
VBA Code:
 Exportpath = ThisWorkbook.Path & "/" & "temp" & "/" & "PLAN.bmp"

Is there a reason you are using forward slash ("/") instead of back slash ("\") in the path?
 
Upvote 0
VBA Code:
 Exportpath = ThisWorkbook.Path & "/" & "temp" & "/" & "PLAN.bmp"

Is there a reason you are using forward slash ("/") instead of back slash ("\") in the path?

I've tested and they both seem to work equally. I've always been confused as to what to use since url's and cloud saved paths have forward and explorer has backward.
Don't know if it's a region thing or what. Most online seem to use backwards, but forward also works for me.

I've gotten the Environ to work now, so it's a fix I guess, but it really annoys me that I seemingly can't use "ThisWorkbook.Path" in my coding.
 
Upvote 0
Have you checked what the final result looks like? It could tell you and us a lot.

VBA Code:
Exportpath = ThisWorkbook.Path & "/" & "temp" & "/" & "PLAN.bmp"
MsgBox Exportpath
 
Upvote 0
C:\users\myusername\Onedrive\Pakkeavdelingen\Produksjonsplan, Forbruk og Kontroll/temp/PLAN.bmp

Looks wonky with "/" and "\" in the same path, but it works.
Msgbox ThisworkbookPath gives a sharepoint path with only "/" in it. So local storage is "\" while url's are "/".
 
Upvote 0
So why not change it like
VBA Code:
Exportpath = ThisWorkbook.Path & "\" & "temp" & "\" & "PLAN.bmp"
 
Upvote 0
So why not change it like
VBA Code:
Exportpath = ThisWorkbook.Path & "\" & "temp" & "\" & "PLAN.bmp"

Okay, now I'm embarrassed. It works. I'm pretty sure I've tested all possibilities with "/" and "\" but this may have eluded me.
For future knowledge, is it always "\" in vba coding or is it depending on the direction they face in the path when I check using msgbox?
Also, is: ThisWorkbook.Path & "\temp\Plan.bmp" not a viable way to enter this? Should it always be like you posted or can I chain together the entire remaining path?
 
Upvote 0
Okay, now I'm embarrassed. It works. I'm pretty sure I've tested all possibilities with "/" and "\" but this may have eluded me.
For future knowledge, is it always "\" in vba coding or is it depending on the direction they face in the path when I check using msgbox?
Also, is: ThisWorkbook.Path & "\temp\Plan.bmp" not a viable way to enter this? Should it always be like you posted or can I chain together the entire remaining path?
Nevermind.

Changed
VBA Code:
IMGplanen.Picture = LoadPicture(ThisWorkbook.Path & "\" & "temp" & "\" & "PLAN.BMP")
to ThisWorkbook.Path now and I get path/access error 75 on this line:

VBA Code:
 .Export Filename:=exportpath, FilterName:="BMP"
 
Upvote 0
On logical drives, whose path starts with a letter (like C: for instance), folders that are part of the path are always separated by a "\".
You may compose a path name any way you want, as long as you meet the above condition, and for both folder names and file names no invalid characters are used: <>"/:\|?*
 
Upvote 0
now and I get path/access error 75 on this line:
The full folder path must exist and the target file, if it already exists, must be able to be overwritten, so a read-only attribute has to be cleared.
 
Upvote 0
Solution

Forum statistics

Threads
1,224,815
Messages
6,181,136
Members
453,021
Latest member
Justyna P

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