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

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
' 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
End With

' write to temp file
usr = Environ("username")
strTempFile = "C:\Users\" & usr & "\Desktop\RENAME Master.xml"
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile(strTempFile, True)
a.WriteLine (strData)
a.Close
 
Upvote 0
Even the person who wrote the code for me is baffled as to why it is not working. He asked me to create a new workbook and try and I did that too
FYI The same code is running perfectly in 8 different applications but this one NO IDEA why
 
Upvote 0
variable not defined
It sounds like you have "Option Explicit" on, which forces you to declare all variables before using them (which is actually a good thing).
Just put this line at before the code update I gave you:
Rich (BB code):
' write to temp file
Dim fs, a
usr = Environ("username")
strTempFile = "C:\Users\" & usr & "\Desktop\RENAME Master.xml"
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile(strTempFile, True)
a.WriteLine(strData)
a.Close
 
Upvote 0
It sounds like you have "Option Explicit" on, which forces you to declare all variables before using them (which is actually a good thing).
Just put this line at before the code update I gave you:
Rich (BB code):
' write to temp file
Dim fs, a
usr = Environ("username")
strTempFile = "C:\Users\" & usr & "\Desktop\RENAME Master.xml"
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile(strTempFile, True)
a.WriteLine(strData)
a.Close
Run-time error '5' --Invalid procedure call or arguement.
 
Upvote 0
Run-time error '5' --Invalid procedure call or arguement.
On which line of code?
If you hit "Debug", it should highlight the offending line of code.
Or, you can try stepping through the code manually, and seeing which line causes the error.
 
Upvote 0
On which line of code?
If you hit "Debug", it should highlight the offending line of code.
Or, you can try stepping through the code manually, and seeing which line causes the error.
a.WriteLine(strData)
This line
 
Upvote 0
a.WriteLine(strData)
This line
Dim rngData As Range
Dim strData As String
Dim strTempFile As String
Dim x As Long, y As Long
Dim usr As String

Dim fs, a
Is there additional text to be stored in Dim fs, a ...like as .....
 
Upvote 0

Forum statistics

Threads
1,214,377
Messages
6,119,183
Members
448,872
Latest member
lcaw

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