What is wrong with my code

RAJESH1960

Banned for repeated rules violations
Joined
Mar 26, 2020
Messages
2,313
Office Version
  1. 2019
Platform
  1. Windows
' write to temp file
usr = Environ("username")
strTempFile = "C:\Users\" & usr & "\Desktop\Master.xml"
With CreateObject("Scripting.FileSystemObject")
' true to overwrite existing temp file
.CreateTextFile(strTempFile, True).write strData

End With

This is just a part of the code. Everytime I run the code stops at .CreateTextFile(strTempFile, True).write strData and show an error as follows -
Run time error ‘5’:
Invalid procedure call or argument.
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
If I shift the .write to the next line it shows a syntex error
.CreateTextFile(strTempFile, True)
.Write strData
 
Upvote 0
If I shift the .write to the next line it shows a syntex error
.CreateTextFile(strTempFile, True)
.Write strData
That is because the "Write" line does not do with the "CreateObject" object.

Try getting rid of the "With" and setting it up like they show in the links I provided you.
 
Upvote 0
I think the issue is the ".write StrData" at the end of that line.
I don't use this method, but from what I see, it appears that the "write" piece needs to go on the next line.

See: CreateTextFile method (Visual Basic for Applications)
and: ASP CreateTextFile Method
and: VBScript >> FileSystemObject >> CreateTextFile
That is because the "Write" line does not do with the "CreateObject" object.

Try getting rid of the "With" and setting it up like they show in the links I provided you.
The same code runs successfully in other applications. But whenever I copy the same to a different or new application it shows this error.
 
Upvote 0
Check you VB References to make sure that you have the correct libraries selected.

If you are not sure which ones they are, open up an application where it does work, and see which References are selected there, and make sure the same references are selected in this one.

If that does not work, I suggest trying setting up like shown in those links I provided.
 
Upvote 0
Joe, Please help me and tell me what to do. As I am baby fed with the codes I wouldn't understand what to do. I hope this will help you to solve my problem.
' get the clipboard data
' magic code for is for early binding to MSForms.DataObject
With CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
.GetFromClipBoard
strData = .GetText
Sheets("ImportMasters").Activate
ActiveSheet.Protect "12345678"

End With

' write to temp file
usr = Environ("username")
strTempFile = "C:\Users\" & usr & "\Desktop\Master.xml"
With CreateObject("Scripting.FileSystemObject")
' true to overwrite existing temp file
.CreateTextFile(strTempFile, True).Write strData

End With
 
Upvote 0
This is the beginning of the code
Dim rngData As Range
Dim strData As String
Dim strTempFile As String
Dim x As Long, y As Long
Dim usr As String
 
Upvote 0
What exactly is "strData"?
I don't see that anywhere in your code before you try using it.
 
Upvote 0

Forum statistics

Threads
1,214,584
Messages
6,120,387
Members
448,956
Latest member
JPav

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