PDA

View Full Version : Variable used to create directory



dach
04-22-2011, 05:09 PM
I am trying to export a Name: field from the current Word document and use it to create a directory on c:.

I have tried in Word 2007 and 2010.

I get a Run-time error '76': Path not found.

I cannot find the bug in my code, can you? :dunno

Thanks

Daniel




Sub CreateDirectory()
Mkdir("c:/" & ThatID("Name:"))
End Sub


Public Function ThatID(xx As String) As String
Dim ID As String

Selection.HomeKey Unit:=wdStory

Selection.Find.ClearFormatting
With Selection.Find
.Text = xx
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=2
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Copy

ID = Selection

Selection.EndKey Unit:=wdStory

ThatID = ID

End Function

Frosty
04-25-2011, 09:44 AM
This is not a point at which you should be stuck. There are issues with this code, but without seeing the whole picture, it's tough to see what's happening (no idea what the document you're searching looks like). I like your structure (using a function to return the folder name you want), however, I would suggest your top routine being the following:

Sub CreateDirectory()
Dim sNewFolder as String
sNewFolder = ThatID("Name:")
Mkdir("c:/" & sNewFolder)
End Sub

And then see the value of sNewFolder using Debug.Print or stepping through and hovering over (that's the F8 key)