PDA

View Full Version : [SOLVED:] Carriage Return



gmooney100
06-22-2021, 12:29 PM
Hello,

I am getting a carriage return that is preventing the last line of code here from firing


Sub BuildPPT()

Dim FilePath As String
Dim TextFile As Integer
Dim FileContent As String
Dim URL1 As String
Dim URL2 As String
Dim varrPos As Variant
Dim i As Long
Dim pptStrFile As String


'File Path & Name of Text File
FilePath = GetDownloadPath & "Category Review BUCategory.txt"

'Determine the next file number available for use by the FileOpen function
TextFile = FreeFile

'Open the text file
Open FilePath For Input As TextFile

'Store file content inside a variable
FileContent = Input(LOF(TextFile), TextFile)


'Close Text File
Close TextFile

pptStrFile = Trim(GetDownloadPath) & Trim(FileContent)
pptStrFile = Trim(Replace(pptStrFile, vbCr, ""))

ActivePresentation.Slides.InsertFromFile pptStrFile, 1, 1, 26

The value of pptStrFile should be:


C:\Users\mogr0002\Downloads\Category Review Grand Canyon - Packaged Beverages.pptx

The immediate window for ? pptStrFile returns the value I am looking for but then adds another blank line below it which I don't why and that is preventing the last line of the code from working.

The text that is in the text file that is being read is:

Category Review Grand Canyon - Packaged Beverages.pptx


Any thoughts?

SamT
06-22-2021, 12:56 PM
'File Path & Name of Text File
FilePath = GetDownloadPath & "Category Review BUCategory.txt"

'Determine the next file number available for use by the FileOpen function
TextFile = FreeFile

'Open the text file
Open FilePath For Input As TextFile

'Store file content inside a variable
FileContent = Input(LOF(TextFile), TextFile)

MSgBox FileContent
MsgBox GetDownloadPath
'
'
'

gmooney100
06-22-2021, 12:59 PM
SamT

MsgBox FileContent = Category Review Grand Canyon - Packaged Beverages.pptx
MsgBox GetDownloadPath = C:\Users\mogr0002\Downloads\

John Wilson
06-22-2021, 01:28 PM
I would input by Line and check for blank lines ( They are probably in the textfile)

Sub getFC()
Dim filenum As Integer
Dim buffer As String
Dim filepath As String
Dim FileContent As String
'use your path of course
filepath = "C:\Users\Info\Desktop\test.txt"
filenum = FreeFile
Open filepath For Input As filenum
'Store file content inside a variable
While Not EOF(filenum)
Line Input #filenum, buffer
' ignore blank line
If Not Trim(buffer) = "" Then
FileContent = buffer
End If
Wend
Debug.Print FileContent
End Sub

gmooney100
06-22-2021, 01:44 PM
John Wilson,

I ran your code and it doesn't look like I have blank lines that show in the immediate window now? Was this code supposed to remove any blank lines and if so, I should input this code into my existing code to remove the blank lines?

SamT
06-22-2021, 03:49 PM
Inspect "Category Review BUCategory.txt" in Notepad. Especially look for VbCrs and extra spaces at either end of lines and in any blank lines

Also

Inspect "Category Review BUCategory.txt" in Word. Especially look for VbCrs and extra spaces at either end of lines and in any blank lines


:dunno

John Wilson
06-23-2021, 08:29 AM
My code reads the text file line by line till End Of File (EOF) it would ignore any blank line and is an alternative to the Lentgh Of File method you used which will not ignore blank lines. AS both Sam and I have said the blank line (maybe just a carriage return) if probably in the text file itself and the best answer would be to remove it

SamT
06-23-2021, 08:45 AM
Try

FileContent = Input(LOF(TextFile)-3, TextFile)
:dunno