How JustAnswer Works:

  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site.
    Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.

Ask Suryanto Your Own Question

Suryanto
Suryanto, Programmer
Category: Programming
Satisfied Customers: 538
Experience:  8 years experience in VBA(Excel, Word, Outlook and Access)
48489642
Type Your Programming Question Here...
Suryanto is online now
A new question is answered every 9 seconds

VBA to get path of Excel File from which it is running

Resolved Question:

I would like to store some audio information in a folder which also stores an Excel spreadsheet. I want to be able to move this folder around. Ingo has pointed me in the direction so I now know how to write VBA code to play  an audiofile if I know the full path of the aduio file.  E.G., I would like VBA code such that if the code isrunning in Excel worksheet C:/Folder1/Folder2/MySheet.xlm then code will return the string "C:/Folder1/Folder2/"
Submitted: 3 years ago.
Category: Programming
Expert:  John D replied 3 years ago.
Hi,

Can you copy the code that you are using to play the audio and paste here so I can take a look
Customer: replied 3 years ago.

 

Hi John

 

Here's the code.

 

 

Public Declare Function sndPlaySound32 _
Lib "winmm.dll" _
Alias "sndPlaySoundA" ( _
ByVal lpszSoundName As String, _
ByVal uFlags As Long) As Long

 


Sub test()
'sndPlaySound32 "C:\Windows\Media\Chimes.wav", 0&
sndPlaySound32 "C:\Users\George\My Documents\Timothy's Academics\Timothy's Spanish\Testing123.wav", 0&
'c:
End Sub

 

 

I got it from a website recommended by Ingo U. But the problem is not with this code, which works. The problem is that I want to create a folder called "Sounds" in the folder which contains the Worksheet which runs the code. I want to be able to move the folder which contains the both worksheet and the folder "Sounds" around to different computers.

I need the VBA code to access sound in the folder "Sounds" which is inside the folder containing the worksheet. It is no good if the string containing the path to these audio files is written in the VBA code and can't change when the folder which contains both the worksheet and "sounds"is moved around.

 

Suppose the folder containing the worksheet is c:\folder1\folder2

What I need is code like

 

 

Function PathToThisWorksheet() as string

' this function returns "c:\folder1\folder2\ in the example mentioned above"

end sub

 

So That I can play audio file "MyAudio1.wav"with the following code

 

sub test3

 

sndPlaySound32 PathToThisWorksheet & "Sounds\" & "MyAudio1.wav"

 

 

 

 

Expert:  John D replied 3 years ago.

In the Sub test()

add this line at the beginning of the code

myPath = "C:\Users\George\My Documents\Timothy's Academics\Timothy's Spanish\Testing123.wav"

 

.

 

then in the line that follows replace that text between the quotes. e.g.

"C:\Users\George\My Documents\Timothy's Academics\Timothy's Spanish\Testing123.wav"

 

with the variable myPath

 

This way you will be able to retrieve the path by referring to the variable myPath

 

Hope this helps.

 

Customer: replied 3 years ago.

John, I guess I didn't make my situation clear. Imagine I create the workbook "SpanishTest.xlm" and save it in folder

 

C:\Users\George\My Documents\Timothy's Academics\Timothy's Spanish\

 

Imagine further that this folder, besides containing "SpanishTest.xlm" also contains the folder "Sounds". If I want to play the sound Testing123.wav, I could do so using the following code

 

mypath = "C:\Users\George\My Documents\Timothy's Academics\Timothy's Spanish\Sounds\Testing123.wav"

 

sndPlaySound32 mypath, 0&

 

Now Imagine that I save the entire folder

 

C:\Users\George\My Documents\Timothy's Academics\Timothy's Spanish

 

to a thumb drive and copy it to the desktop of Timothy's computer. I go into this folder on the desktop of Timothy's computer and doubleclick on SpanishTest.xlm. Now the code to play Testing123.wav won't work because the specified path is no longer relevant. I don't want to have to change the VBA code every time I move the folder

 

Timothy's Spanish

 

to a new location.

 

Recall that when you try to do a "Save As" in Excel, the program remembers the path from which the workbook was loaded and suggests this path as the location to which the new workbook will be saved. What I am trying to do is find out how I might use VBA to grab hold of that suggested path, put the path into a string variable, and use this string variable to create the path to the folder "Sounds" where the file Testing123.wav is located. The folder "Sounds"will always be in the same folder as the folder from which Spanishtest.xlm, the active workbook, was loaded.

 

Expert:  John D replied 3 years ago.
As this is going to take some time to review the remarks and resolve the issue I will opt put so the question becomes open to all experts
Expert:  Suryanto replied 3 years ago.
Dear Customer,

Welcome to Justanswer.

I am Suryanto and I will assist you.

Use this

Function PathToThisWorksheet() as string

PathToThisWorksheet = ThisWorkbook.Path & "\"
end sub

Hope this helps

Thank you

Suryanto

 

Suryanto, Programmer
Category: Programming
Satisfied Customers: 538
Experience: 8 years experience in VBA(Excel, Word, Outlook and Access)
Suryanto and 2 other Programming Specialists are ready to help you

JustAnswer in the News:

 
 
 
Ask-a-doc Web sites: If you've got a quick question, you can try to get an answer from sites that say they have various specialists on hand to give quick answers... Justanswer.com.
JustAnswer.com...has seen a spike since October in legal questions from readers about layoffs, unemployment and severance.
Web sites like justanswer.com/legal
...leave nothing to chance.
Traffic on JustAnswer rose 14 percent...and had nearly 400,000 page views in 30 days...inquiries related to stress, high blood pressure, drinking and heart pain jumped 33 percent.
Tory Johnson, GMA Workplace Contributor, discusses work-from-home jobs, such as JustAnswer in which verified Experts answer people’s questions.
I will tell you that...the things you have to go through to be an Expert are quite rigorous.
 
 
 

What Customers are Saying:

 
 
 
  • My Expert answered my question promptly and he resolved the issue totally. This is a great service. I am so glad I found it I will definitely use the service again if needed. One Happy Customer New York
< Last | Next >
  • My Expert answered my question promptly and he resolved the issue totally. This is a great service. I am so glad I found it I will definitely use the service again if needed. One Happy Customer New York
  • Wonderful service, prompt, efficient, and accurate. Couldn't have asked for more. I cannot thank you enough for your help. Mary C. Freshfield, Liverpool, UK
  • This expert is wonderful. They truly know what they are talking about, and they actually care about you. They really helped put my nerves at ease. Thank you so much!!!! Alex Los Angeles, CA
  • Thank you for all your help. It is nice to know that this service is here for people like myself, who need answers fast and are not sure who to consult. GP Hesperia, CA
  • I couldn't be more satisfied! This is the site I will always come to when I need a second opinion. Justin Kernersville, NC
  • Just let me say that this encounter has been entirely professional and most helpful. I liked that I could ask additional questions and get answered in a very short turn around. Esther Woodstock, NY
  • Thank you so much for taking your time and knowledge to support my concerns. Not only did you answer my questions, you even took it a step further with replying with more pertinent information I needed to know. Robin Elkton, Maryland
 
 
 

Meet The Experts:

 
 
 
  • ATLPROG

    Computer Software Engineer

    Satisfied Customers:

    7463
    MS in IT.Several years of programming experience in Java C++ C C# Python VB Javascript HTML
< Last | Next >
  • http://ww2.justanswer.com/uploads/SP/spatlanta2010/2011-6-23_12450_photo.64x64.gif ATLPROG's Avatar

    ATLPROG

    Computer Software Engineer

    Satisfied Customers:

    7463
    MS in IT.Several years of programming experience in Java C++ C C# Python VB Javascript HTML
  • http://ww2.justanswer.com/uploads/ComputersGuru/2010-02-13_051118_Photo41.JPG LogicPro's Avatar

    LogicPro

    Computer Software Engineer

    Satisfied Customers:

    5603
    Expert in C, C++, Java, DOT NET, Python, HTML, Javascript, Design.
  • http://ww2.justanswer.com/uploads/unvadim/2010-11-15_210218_avatar.jpg unvadim's Avatar

    unvadim

    Computer Software Engineer

    Satisfied Customers:

    1158
    Good knowledge of OOP principles. 3+ years of programming experience with Java and C++. Sun Certified Java Programmer 5.0.
  • http://ww2.justanswer.com/uploads/lifesaver333/2010-10-17_191349_ls.jpeg lifesaver's Avatar

    lifesaver

    Computer Software Engineer

    Satisfied Customers:

    950
    Several years of intensive programming and application development experience in various platforms.
  • http://ww2.justanswer.com/uploads/EH/ehabtutor/2012-8-2_202016_1.64x64.jpg ehabtutor's Avatar

    ehabtutor

    Computer Software Engineer

    Satisfied Customers:

    864
    Bachelor of computer science, 5+ years experience in software development, software company owner
  • http://ww2.justanswer.com/uploads/RA/rajivsharma086/2012-6-6_17128_displaypic.64x64.jpg Raj's Avatar

    Raj

    Computer Engg.

    Satisfied Customers:

    860
    BE CS, 4+ Experience in Programming and Database (ERP)
  • http://ww2.justanswer.com/uploads/eljonis/2010-01-06_130406_eljon2.jpg Eljon's Avatar

    Eljon

    Consultant

    Satisfied Customers:

    590
    11 yrs of programming (PHP, WordPress, XSL, SQL, JavaScript)
 
 
 
Chat Now With A Programmer
Suryanto
Suryanto
Computer Engineer
505 Satisfied Customers
8 years experience in VBA(Excel, Word, Outlook and Access)