VBA: Capture Computer Name and Insert it into String.

SteveOranjin

Board Regular
Joined
Dec 18, 2017
Messages
170
Hello,

I'd like to be able to send this file to other people and have it work for them, without having to go through some of the VBA editing that I must go through presently.

The macro works fine, but when it goes all the way to the file destination, it says that nothing is present. What this indicates to me is that file name path is not going through as I'd like.

The below code, highlighted in red, is what I was made to understand would work for getting VBA to grab the name of the computer and return it. I added a comment which is not really in the code on my computer, which I have underlined. That code is also in red. The comment should grant additional insight.

Hope you are well,

Steve

Rich (BB code):
Sub Image_Import2()Dim fileList() As String
Dim fName As String
Dim fPath As String
Dim i As Integer
Dim startrow As Integer
Dim ws As Worksheet
Dim filetype  As String
Dim sHostName As String


' Dim shellApp As Shell32.Shell


'=======================================================
' Set shellApp = New Shell32.Shell
' fName = shellApp.BrowseForFolder(0, "Please choose a folder", 0).Self.Path
' (last arg. 512 instead of 0 to disable new folder)
' MsgBox fPath
'=======================================================




sHostName = Environ$("computername")
'=======================================================
fPath = "C:\Users\" & sHostName & "\Desktop\Image Name Processing\"
'' This would normally be entered in as, "C:\Users\Data Coordinator I\Desktop\Image Name Processing\"

' fPath = "C:\Image Directory\Mfr Images\"
' fPath = "C:\Image Directory\SizeNSort\Input\"
' fPath = "C:\Users\gweha\Documents\Bravo_MLS\MLS_VENDOR_FILES\Artcraft Lighting\Images\"
' fPath = "\\NAS\Volume_1\Vendor Files\Checkolite\2011 Images\"
' fPath = "\\NAS\Volume_1\Vendor Files\Cyan\Cyan Images\"


filetype = "*"
Set ws = Worksheets("Import Images")
startrow = 1    'starting row for the data
'========================================================


fName = Dir(fPath & "*." & filetype)
While fName <> ""
    i = i + 1
    ReDim Preserve fileList(1 To i)
    fileList(i) = fName
    fName = Dir()
Wend
If i = 0 Then
    MsgBox "No files found"
    Exit Sub
End If
    
For i = 1 To UBound(fileList)
    ws.Range("B" & i + startrow).Value = fileList(i)
Next
Columns(1).AutoFit


End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Try
Code:
sHostName = Environ("userprofile")
'=======================================================
fPath = sHostName & "\Desktop\Image Name Processing\"
 
Upvote 0
If you add the msgbox what does it say?
Code:
sHostName = Environ("userprofile")
'=======================================================
fPath = sHostName & "\Desktop\Image Name Processing\"
MsgBox fPath
 
Upvote 0
Rich (BB code):
Dim sHostName As String


' Dim shellApp As Shell32.Shell


'=======================================================
' Set shellApp = New Shell32.Shell
' fName = shellApp.BrowseForFolder(0, "Please choose a folder", 0).Self.Path
' (last arg. 512 instead of 0 to disable new folder)
' MsgBox fPath
'=======================================================




sHostName = Environ$("computername")
'=======================================================
fPath = "C:\Users\" & sHostName & "\Desktop\Image Name Processing\"
Msgbox fPath
'' This would normally be entered in as, "C:\Users\Data Coordinator I\Desktop\Image Name Processing\"

That returns "Data-COORDINATO\Desktop\Image Name Processing\

Steve
 
Last edited:
Upvote 0
What does the msgbox say with the code I suggested?
 
Upvote 0
It says "DATA-COORDINATO\Desktop\Image Name Processing"

When I enter the file path manually, I input the below, and it works.

"C:\Users\Data Coordinator I\Desktop\Image Name Processing"
 
Last edited:
Upvote 0
MrExcel is not letting me add the file path correctly. There is an additional backslash after the last g there.
 
Upvote 0
Not sure what is happening, as when I run it I get
C:\Users\Fluff\Desktop\Image Name Processing\
 
Upvote 0
What if you try
Code:
sHostName = Environ("username")
'=======================================================
fPath = "C:\Users\" & sHostName & "\Desktop\Image Name Processing\"
MsgBox fPath
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top