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

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Option Explicit

Sub SaveAsMasterXML()

Dim rngData As Range
Dim strData As String
Dim strTempFile As String
Dim x As Long, y As Long
Dim usr As String

If Sheets("MasterData").Range("A2") = "" Then
MsgBox "Data Not Entered"
Exit Sub
'
' Replace_in_sales Macro
'
'
Sheets("MasterData").Activate
Range("A:A,B:B,C:C,D:D,E:E,F:F, K:K").Select
Selection.Replace what:="&", Replacement:="&", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Sheets("ImportMasters").Activate
ActiveSheet.Unprotect "12345678"

Sheets("ImportMasters").Activate
y = Sheets("ImportMasters").Range("A2").CurrentRegion.Columns.Count
x = Sheets("MasterData").Range("A2:A" & Sheets("MasterData").Range("A" & Rows.Count).End(xlUp).Row).Rows.Count
Range("A2").Resize(x, y).Copy
End If

' 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

' open notepad with tempfile

Sheets("MasterData").Activate
Range("A:A,B:B,C:C,D:D,E:E,F:F, K:K").Select
Selection.Replace what:="&", Replacement:="&", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Sheets("MasterData").Activate
Range("A2").Select
MsgBox ("File saved on Desktop as Master.XML Rename the File. Copy path and paste in tally.")
End Sub
 
Upvote 0
Before we go further, you said that the same code works in other applications.

Did you do what I asked in post 7?
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.
You do this by going into the VB Editor, selecting the "Tools" menu, and clicking "References" and checking all the References that have a check mark next to them.
Compare what is checked on an application that is working, and compare that to what is checked on the application that isn't working.
 
Upvote 0
Before we go further, you said that the same code works in other applications.

Did you do what I asked in post 7?

You do this by going into the VB Editor, selecting the "Tools" menu, and clicking "References" and checking all the References that have a check mark next to them.
Compare what is checked on an application that is working, and compare that to what is checked on the application that isn't working.
I checked the references in the tool menu. Both the applications have the same boxes checked.
 
Upvote 0
Like I said, I really don't use this method at all, but if I try to convert your code with what is contained in those links, it looks like you would want to replace this part of your code:
VBA Code:
With CreateObject("Scripting.FileSystemObject")
' true to overwrite existing temp file
    .CreateTextFile(strTempFile, True).Write strData
End With
with this:
VBA Code:
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile(strTempFile, True)
a.WriteLine(strData)
a.Close
 
Upvote 0
I created a new sheet 3 times and pasted the code. But still the cursor stops at this point “ .CreateTextFile(strTempFile, True).Write strData “.. I can't understand why it is running perfectly in the original workbook from where I copied the code. I rechecked every line of code but to no avail.
 
Upvote 0
I created a new sheet 3 times and pasted the code. But still the cursor stops at this point “ .CreateTextFile(strTempFile, True).Write strData “.. I can't understand why it is running perfectly in the original workbook from where I copied the code. I rechecked every line of code but to no avail.
Did you even try my last suggestion?
 
Upvote 0

Forum statistics

Threads
1,214,940
Messages
6,122,356
Members
449,080
Latest member
Armadillos

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