Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / December 2004

Tip: Looking for answers? Try searching our database.

Split Directory\filename into two variables

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
bill_campbell@bcbsmt.com - 07 Dec 2004 04:54 GMT
In my macro a user selects a filename using:

With Dialogs(wdDialogFileOpen)
If .Display Then
strSourceFile = WordBasic.FilenameInfo$(.Name, 1)
End If
End With

I want to split strSourceFile into a directory part, and a filename
part.  I've done this before by starting at the left most point of a
string, then backing up one character untill a backslash is found, but
I think there is probably something all ready built into VBA to do this.
JBNewsGroup - 07 Dec 2004 05:59 GMT
Hi Bill,

Look at the functions "Instr" (left to right scan) and "InstrRev" (right to
left scan).   For Instance:

   J = InstrRev (strSourceFile,"\",-1,vbTextCompare)
   If (J <= 0) then
      --  no backslash, do something --
   Else
       DirectoryPart = Left$(strSourceFile , J-1)
       FileNamePart = Mid$(strSourceFile ,J+1)
  End If

In the Lewft$ function change J-1 to J  if you want the backslash included.
Instead of Mid$ you could also use Right$.

Jerry Bodoff

> In my macro a user selects a filename using:
>
[quoted text clipped - 8 lines]
> string, then backing up one character untill a backslash is found, but
> I think there is probably something all ready built into VBA to do this.
bill_campbell@bcbsmt.com - 07 Dec 2004 07:15 GMT
I found (mostly by luck) that by switching the number following .Name
to 5 and 3, I could pull the information I needed.
> > strSourceFile = WordBasic.FilenameInfo$(.Name, 1)
bill_campbell@bcbsmt.com - 07 Dec 2004 07:15 GMT
I found (mostly by luck) that by switching the number following .Name
to 5 and 3, I could pull the information I needed.
> > strSourceFile = WordBasic.FilenameInfo$(.Name, 1)
Pete Bennett - 07 Dec 2004 13:25 GMT
It's even easier to use FileSystemObject (you'll need to add in a reference
to Microsoft Scripting Runtime to get it to work)

Dim fso as FileSystemObject

Debug.Print fso.GetParentFolderName("c:\temp\harry.xml")
Debug.Print fso.GetFileName("c:\temp\harry.xml")

Will give you the information you need.  Play with it and you'll see it's a
pretty good little file handler.  It even supplies read and write functions
as well.

> In my macro a user selects a filename using:
>
[quoted text clipped - 8 lines]
> string, then backing up one character untill a backslash is found, but
> I think there is probably something all ready built into VBA to do this.
JBNewsGroup - 07 Dec 2004 14:18 GMT
Hi Pete,

I have been using the FileSystemObject for a while and I did not realize
that I could do I/O with it.  I am going to check it out.  It is like a
friend says, "read the book when you need it".

Thanks for the tip.

Jerry Bodoff

> It's even easier to use FileSystemObject (you'll need to add in a reference
> to Microsoft Scripting Runtime to get it to work)
[quoted text clipped - 20 lines]
> > string, then backing up one character untill a backslash is found, but
> > I think there is probably something all ready built into VBA to do this.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.