Question of Import Export Files

U

unplugs

Hiyee all,

Until this stage, I'm still considering a newbie in VBA althoug
I can deal with certain userform and some codes.

Anyway, I got a question.... a very basic question for export an
import files. I'm not that understand and don't know much about th
codes that can do so.

My problem is, I created 1 user form, get users entries and th
entries will then placed into excel spreadsheet respectively. Afte
that, I would want to save the spreadsheet to a text file, or wor
file. How can I do tat?

1) How the coding is if we export the data in spreadsheet to notepad ?

2) Can we specify a file name for it?

3) After we saved the file to notepad, where the file will gone? I
which location?

4) Can we auto specify a file name for the file? For example, m
spreadsheet will capture user's computer login(network), and he wil
made entries inside. After that, if he run the macro, it will save hi
file using his login name.

5) About the code that capture user's login name in networ, I saw
thread discuss about this topic yesterday, and I can't find the threa
now... Can anyone tell me where is the thread is?


Thanks a lot.... Hope it wont take ur time to answer my noo
Question... :<

Hope to hear from u all soon...
 
U

unplugs

My data in spreadsheet are placed in column A5:F5. And I had define th
range of data as "Range", which is dynamic range that follow the user'
entries...

I had tried an example in my reference book.... but I can't figure ou
the problem... and I'm not understand those codes. I hope I can lear
from u all... Thank
 
U

unplugs

Thanks God! About my 5th question, I had found the thread...

Anyway, i got a question about it...

' Declare for call to mpr.dll.
Declare Function WNetGetUser Lib "mpr.dll" _
Alias "WNetGetUserA" (ByVal lpName As String, _
ByVal lpUserName As String, lpnLength As Long) As Long

Const NoError = 0 'The Function call was successful

Sub GetUserName()

' Buffer size for the return string.
Const lpnLength As Integer = 255

' Get return buffer space.
Dim status As Integer

' For getting user information.
Dim lpName, lpUserName As String

' Assign the buffer size constant to lpUserName.
lpUserName = Space$(lpnLength + 1)

' Get the log-on name of the person using product.
status = WNetGetUser(lpName, lpUserName, lpnLength)

' See whether error occurred.
If status = NoError Then
' This line removes the null character. Strings in C ar
null-
' terminated. Strings in Visual Basic are no
null-terminated.
' The null character must be removed from the C strings to b
used
' cleanly in Visual Basic.
lpUserName = Left$(lpUserName, InStr(lpUserName, Chr(0)) - 1)
Else

' An error occurred.
MsgBox "Unable to get the name."
End
End If

' Display the name of the person logged on to the machine.
MsgBox "Welcome " & lpUserName

End Sub


The above is the codes to get the user name....Anyway, can the usernam
placed in a cell? If can... How to do that? I want to placed th
username in sheet1 cell "G5".... Is there any method to do so?

and why I cannot put as:
Cells(NextRow,7) = & lpUserName
It gave me the error msg when I put "&" infront....
 
D

Dave Peterson

Drop the & from this line:

Cells(NextRow,7) = & lpUserName

& is VBA's concatenate operator. It's used to join two pieces of text.

But you just want one.
 
U

unplugs

Dear Dave,

Even if I drop the "&" in the line, i cannot placed the use
name that I captured into the cell. My code for capture the user nam
is code in module.

And for the question from 1 to 4, anyone can help me on this..
I hope someone can explain to me... Thank
 
D

Dave Peterson

Here's one that I use:

Option Explicit
Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Function fOSUserName() As String
' Returns the network login name
Dim lngLen As Long, lngX As Long
Dim strUserName As String
strUserName = String$(254, 0)
lngLen = 255
lngX = apiGetUserName(strUserName, lngLen)
If lngX <> 0 Then
fOSUserName = Left$(strUserName, lngLen - 1)
Else
fOSUserName = ""
End If
End Function

Sub testitnow()
Dim NextRow As Long
Range("a1").Activate
NextRow = 3
Cells(NextRow, 7) = fOSUserName
End Sub

====
I didn't look at your first couple of posts.

But if you want to write to a text file, you can do it by saving the data (.prn
or .txt) the File|SaveAs dialog.

If you want more control, maybe you can steal some code...

Earl Kiosterud's Text Write program:
http://www.tushar-mehta.com/
Look for Text Write in the left hand frame.

Chip Pearson's:
http://www.cpearson.com/excel/imptext.htm

J.E. McGimpsey's:
http://www.mcgimpsey.com/excel/textfiles.html

=====
Each of these creates a file directly. It doesn't use notepad for the
creation--although you could use notepad to open the file later.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top