PDA

View Full Version : Don't understand a piece of code here



asset
04-28-2013, 09:50 AM
Hi!:help:biggrin:
Who can help explain me this piece of code here:


intPos = InStrRev(strDocName, ".")
strDocName = Left(strDocName, intPos - 1)


This piece of code is part of the following macro which I found on Internet:

Sub ChangeDocsToTxtOrRTFOrHTML()
'with export to PDF in Word 2007
Dim fs As Object
Dim oFolder As Object
Dim tFolder As Object
Dim oFile As Object
Dim strDocName As String
Dim intPos As Integer
Dim locFolder As String
Dim fileType As String
On Error Resume Next
locFolder = InputBox("Enter the folder path to DOCs", "File Conversion", "C:\myDocs")
Select Case Application.Version
Case Is < 12
Do
fileType = UCase(InputBox("Change DOC to TXT, RTF, HTML", "File Conversion", "TXT"))
Loop Until (fileType = "TXT" Or fileType = "RTF" Or fileType = "HTML")
Case Is >= 12
Do
fileType = UCase(InputBox("Change DOC to TXT, RTF, HTML or PDF(2007+ only)", "File Conversion", "TXT"))
Loop Until (fileType = "TXT" Or fileType = "RTF" Or fileType = "HTML" Or fileType = "PDF")
End Select
Application.ScreenUpdating = False
Set fs = CreateObject("Scripting.FileSystemObject")
Set oFolder = fs.GetFolder(locFolder)
Set tFolder = fs.CreateFolder(locFolder & "Converted")
Set tFolder = fs.GetFolder(locFolder & "Converted")
For Each oFile In oFolder.Files
Dim d As Document
Set d = Application.Documents.Open(oFile.Path)
strDocName = ActiveDocument.Name
intPos = InStrRev(strDocName, ".")
strDocName = Left(strDocName, intPos - 1)
ChangeFileOpenDirectory tFolder
Select Case fileType
Case Is = "TXT"
strDocName = strDocName & ".txt"
ActiveDocument.SaveAs FileName:=strDocName, FileFormat:=wdFormatText
Case Is = "RTF"
strDocName = strDocName & ".rtf"
ActiveDocument.SaveAs FileName:=strDocName, FileFormat:=wdFormatRTF
Case Is = "HTML"
strDocName = strDocName & ".html"
ActiveDocument.SaveAs FileName:=strDocName, FileFormat:=wdFormatFilteredHTML
Case Is = "PDF"
strDocName = strDocName & ".pdf"

' *** Word 2007 users - remove the apostrophe at the start of the next line ***
'ActiveDocument.ExportAsFixedFormat OutputFileName:=strDocName, ExportFormat:=wdExportFormatPDF

End Select
d.Close
ChangeFileOpenDirectory oFolder
Next oFile
Application.ScreenUpdating = True
End Sub

gmaxey
04-28-2013, 10:24 AM
InStrRev returns the index of where one string is located in another. It starts looking at the end fo the string, but for whatever reason it returns the value from the start of the string.

So if you hav a file named "Test Document.doc" then the "." is located at position 14.

So to return the file name less the extension you want the left part of the string that is 13 characters long.

asset
04-28-2013, 07:53 PM
Thanks Greg! Now I understand what this piece of code does: it returns a name of the certain file without extension, so that it can be used further when saving with another extension.

macropod
04-28-2013, 07:58 PM
I used essentially the same approach for the code in your other thread:
http://www.vbaexpress.com/forum/showthread.php?t=46101

asset
04-28-2013, 10:56 PM
Paul, yes, I just found above macro in the Internet a little after posting that thread. But think both your macro and this macro will help. Thanks!