Monday, May 27, 2019

VBScript rename file names in folder

VBScript code rename files;find and replace file names windows;find and replace file names VBScript;VBScript Find and Replace in File Names










1) Create a file rename_script.vbs and paste the below code. VBScript can be written using Windows Notepad or any other plain text editor.

 Set objFso = CreateObject("Scripting.FileSystemObject")  
 Set Folder = objFSO.GetFolder("C:\Users\MyName\Documents\MyDirectory\")  
 For Each File In Folder.Files  
   sNewFile = File.Name  
   sNewFile = Replace(sNewFile,"find","_")  
 if (sNewFile<>File.Name) then  
   File.Move(File.ParentFolder+"\"+sNewFile)  
 end if  


2) Save the file and double click to execute.

Note: "Swim at your own risk", the code doesn't have extensive error handling. Just be sure to replace the "C:\Users\MyName\Documents\MyDirectory" with the path to your directory.

Sunday, May 26, 2019

How to display code snippets in blogger posts

How to insert code blocks in Blogger posts;code snippets blogspot;code snippets blogger;How to display code snippets in blogger posts;vba code display in blogger

1) First, we need to add the html code(syntax template) below on the head section of our blogger post.

<head>
<link href="https://alexgorbatchev.com/pub/sh/current/styles/shCore.css" rel="stylesheet" type="text/css"></link>
<link href="https://alexgorbatchev.com/pub/sh/current/styles/shThemeEclipse.css" rel="stylesheet" type="text/css"></link>
<script src="https://alexgorbatchev.com/pub/sh/current/scripts/shCore.js" type="text/javascript">
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJava.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js' type='text/javascript'/>
<script language='javascript' type='text/javascript'>
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.all();
</script>
</head>

2) Next step is the code rendering. Basically, we need to HTML escape some characters like right angle brackets, e.g. all < must be replaced with <. To do so, you can use the following online tool: http://codeformatter.blogspot.in/2009/06/about-code-formatter.html

3) Once you have the code with escaped characters, you can copy it on your blogger post inside
<pre> tags.


3) That's it! the complete example is below:


 <head>  
 <link href="https://alexgorbatchev.com/pub/sh/current/styles/shCore.css" rel="stylesheet" type="text/css"></link>  
 <link href="https://alexgorbatchev.com/pub/sh/current/styles/shThemeEclipse.css" rel="stylesheet" type="text/css"></link>  
 <script src="https://alexgorbatchev.com/pub/sh/current/scripts/shCore.js" type="text/javascript">  
 <script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJava.js' type='text/javascript'/>  
 <script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js' type='text/javascript'/>  
 <script language='javascript' type='text/javascript'>  
 SyntaxHighlighter.config.bloggerMode = true;  
 SyntaxHighlighter.all();  
 </script>  
 </head>  
4) Another handy online converter of code to html is hilite.me. Convert the piece of code which is desirable to insert as a code block using this converter and paste the html in the post.

Let’s say we have the following piece of code which is desirable to insert as a code block:

Function OdbcConnectionStringSQLServer(ByVal Driver As String, ByVal Server As String, ByVal database As String, _
ByVal Username As String, ByVal Password As String) As String

'SQL Server

OdbcConnectionStringSQLServer = "Driver={" & Driver & "};Server=" & Server _
& ";UID=" & Username & ";PWD=" & Password & ";Database=" & database


End Function

Below is the result after conversion. style I used: friendly, CSS I used: border:none;



Function OdbcConnectionStringSQLServer(ByVal Driver As String, ByVal Server As String, ByVal database As String, _
    ByVal Username As String, ByVal Password As String) As String

'SQL Server
  
     OdbcConnectionStringSQLServer = "Driver={" & Driver & "};Server=" & Server _
            & ";UID=" & Username & ";PWD=" & Password & ";Database=" & database
  

End Function



VBA Copy Files Using FileSystemObject

VBA FileSystemObject;Copy VBA FileSystemObject;Copy Files Using VBA FileSystemObject;copies one file from one folder to another folder with the VBA FileSystemObject






VBA code to copy file from one folder to another folder using FileSystemObject:



 Sub CopyFile(SourceFilePath As String, DestPath As String, OverWrite As Boolean)  
 ' (1) copies one file from one folder to another folder with the VBA FileSystemObject  
 ' (2) contains extensive error handling (safeguards)  
 ' (3) requires a reference to the object library "Microsoft Scripting Runtime" under Options &gt; Tools &gt; References... in the Visual Basic Editor.  
 Dim blFileExists As Boolean, blSourceErr As Boolean  
 Dim strFileName As String, strSuccessMsg As String, strNewDestPath As String, strNewSourcePath As String  
 Dim FSO As Scripting.FileSystemObject  
 Dim strErrMsg As String  
 Set FSO = New Scripting.FileSystemObject  
 'Set FSO = VBA.CreateObject("Scripting.FileSystemObject")  
 With FSO  
 strNewDestPath = .BuildPath(.GetAbsolutePathName(DestPath), "\")  
 strFileName = .GetFileName(SourceFilePath)  
 'check if the source file exists  
 If Not .FileExists(SourceFilePath) Then  
 ' check if the root drive was specified  
 If .DriveExists(Left(SourceFilePath, 2)) Then  
 blSourceErr = True  
 ' the provided source path is incomplete  
 ' build new path and ask the user if he accepts the suggestion  
 Else  
 strNewSourcePath = .BuildPath(.GetAbsolutePathName(SourceFilePath), "")  
 If Not MsgBox("The source path " &amp; Chr(34) &amp; SourceFilePath &amp; Chr(34) &amp; _  
 " is incomplete. Will you accept the following suggestion: " _  
 &amp; Chr(34) &amp; strNewSourcePath &amp; Chr(34) &amp; "?", vbYesNo, "Confirm new source path") = vbYes Then _  
 blSourceErr = True  
 End If  
 ' error  
 If blSourceErr Then _  
 strErrMsg = "The source file," &amp; Chr(34) &amp; strFileName &amp; Chr(34) &amp; _  
 " does not exist, or the specified path to the file, " &amp; Chr(34) &amp; _  
 Replace(SourceFilePath, strFileName, "") &amp; Chr(34) &amp; " is incorrect."  
 ' check if the destination folder already exists  
 ElseIf Not .FolderExists(strNewDestPath) Then  
 ' prompt the user if the destination folder should be created  
 If MsgBox("The destination folder, " &amp; Chr(34) &amp; strNewDestPath &amp; Chr(34) &amp; ", does not exist. Do you want to create it?", vbYesNo, _  
 "Create new folder?") = vbYes Then  
 .CreateFolder (strNewDestPath)  
 Else  
 strErrMsg = "The destination folder could not be created."  
 End If  
 ' check if the file already exists in the destination folder  
 Else  
 blFileExists = .FileExists(strNewDestPath &amp; strFileName)  
 If Not OverWrite Then  
 If blFileExists Then _  
 strErrMsg = "The file, " &amp; Chr(34) &amp; strFileName &amp; Chr(34) &amp; _  
 ", already exists in the destination folder, " &amp; Chr(34) &amp; _  
 strNewDestPath &amp; Chr(34) &amp; "."  
 End If  
 End If  
 ' attempt to copy file  
 If strErrMsg = vbNullString Then  
 On Error Resume Next  
 If strNewSourcePath = vbNullString Then strNewSourcePath = SourceFilePath  
 Call .CopyFile(strNewSourcePath, strNewDestPath, OverWrite)  
 If Err.Number &lt;&gt; 0 Then strErrMsg = "Run-time error " &amp; Err.Number &amp; Chr(10) &amp; Err.Description  
 On Error GoTo 0  
 End If  
 ' succesful copy  
 If strErrMsg = vbNullString Then  
 strSuccessMsg = "The file" &amp; Chr(34) &amp; strFileName &amp; Chr(34) &amp; " was copied to " &amp; _  
 Chr(34) &amp; strNewDestPath &amp; Chr(34) &amp; "."  
 If blFileExists Then strSuccessMsg = strSuccessMsg &amp; Chr(10) &amp; _  
 "(Note, the existing file in the destination folder was overwritten)."  
 MsgBox strSuccessMsg, vbInformation, "File copied"  
 ' error  
 Else  
 MsgBox strErrMsg, vbCritical, "Error!"  
 End If  
 End With  
 Set FSO = Nothing  
 End Sub  


Saturday, April 20, 2019

VBA Range Run-time error 91 Object variable or With block variable not set

Run-time error 91;Run-time error 91 Object variable or With block variable not set;Run-time error 91 while defining Range;VBA Range Run-time error 91 Object variable or With block variable not set;create 5 seconds before unload userform


Error:


I encountered the below error when I try to define a range variable.

Run-time error '91':
Object variable or With block variable not set


Code:

Dim changedRange As Range

changedRange = Sheets(sheetName).ListObjects(table).DataBodyRange --Error on this line

Solution:


Needed to use "Set", because I had defined changedRange as Range - and this is an Object ;)
Set changedRange = Sheets(sheetName).ListObjects(table).DataBodyRange

That solved the issue.


VBA - Create a time delay before unloading User Form

create delay to unload VBA userform;VBA;VBA unload userform delay;create 5 seconds before unload userform


I had a requirement to unload an VBA user form after few seconds delay, that I used to display progress of the tasks.

Solution:


You may try the below:

Application.Wait Now + TimeValue("0:00:03") 'Create a time delay of 3 seconds

Sample Code:

FrmProgress.Label1.Caption = "Loading data from database...."
FrmProgress.Show
FrmProgress.Repaint

'...code for whatever it is you want to do

Application.Wait Now + TimeValue("0:00:03") 'Create a time delay

Unload FrmProgress
Sample Code 2:

MsgBox "Running Script in Database...Please Wait..", vbInformation, "In-Progress"Wait (5)MsgBox "Script Executed Successfully!", vbInformation, "Execution Completed" Sub Wait(seconds As Integer)      Dim now As Long      now = Timer()      Do          DoEvents      Loop While (Timer < now + seconds)    End Sub


Thursday, January 24, 2019

Citrix SSL Error 61: Contact your help desk with the following information

Citrix SSL Error 61;Citrix SSL Error 61: Contact your help desk with the following information;the issuer of the server's security certificate ((SSL error 61)


I am using Ubuntu 12.04 LTS (32bit) and the latest Linux version of Citrix Receiver. Whenever I try to connect to my work network through Citrix I used get the error message below:

SSL error : Contact your help desk with the following information: You have not chosen to trust "/C=US/ST=/L=/O=Equifax/OU=Equifax Secure Certificate Authority/CN=", the issuer of the server's security certificate (SSL error 61).


Solution:


Make Firefox's certificates accessible to Citrix. Run the below code in the terminal.

sudo ln -s /usr/share/ca-certificates/mozilla/* /opt/Citrix/ICAClient/keystore/cacerts
This did the trick for me.

Could not load file or assembly System.Web.Mvc, Version=3.0.0.1


Could not load file or assembly 'System.Web.Mvc, Version=3.0.0.1;Could not load file or assembly 'System.Web.Mvc, Version=3.0.0.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified;

Error:

System.IO.FileNotFoundException: Could not load file or assembly 'System.Web.Mvc, Version=3.0.0.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified

Solution:

  • Remove the MVC reference and add the correct reference to the project
  • Change the Copy Local property of the reference to true
  • Update the bindingRedirect setting in web.config as below

web.config runtime section:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.1" />
</dependentAssembly>

Change the Copy Local setting will include the System.Web.MVC.dll file in the bin folder when you publish the project, so that it works even if the server is not updated with the new version.

Error 3 'CompareAttribute' is an ambiguous reference between 'System.ComponentModel.DataAnnotations.CompareAttribute' and 'System.Web.Mvc.CompareAttribute'

Error 3 'CompareAttribute' is an ambiguous reference between 'System.ComponentModel.DataAnnotations.CompareAttribute' and 'System.Web.Mvc.CompareAttribute';Error 3;CompareAttribute is an ambiguous reference

Error:


Error 3 'CompareAttribute' is an ambiguous reference between 'System.ComponentModel.DataAnnotations.CompareAttribute' and 'System.Web.Mvc.CompareAttribute'

Cause:


This happens when running VS 11 with mvc Beta 4 and .Net 4.5.

Solution:


To make this work between .NET4 and .NET45 you need to add the below statement in those files to:

using CompareAttribute = System.Web.Mvc.CompareAttribute;

Wednesday, January 23, 2019

Error 8 The type or namespace name 'PagedList' could not be found

Error 8 The type or namespace name 'PagedList' could not be found;Error 8 The type or namespace name 'PagedList' could not be found (are you missing a using directive or an assembly reference?)

I encountered the below error while compiling the project.

Error 8 The type or namespace name 'PagedList' could not be found (are you missing a using directive or an assembly reference?)


Solution:

Try installing the PagedList.MVC NuGet package for your project.
You could use the following steps to do so :

Right-click your Project within the Solution Explorer
  • Choose the "Manage NuGet Packages" option that appears
  • Search for "PagedList.MVC" within the search box
  • Click "Install" on the first option that appears : 



This should add all of the appropriate references that you need. You'll then just need to add the appropriate using statements within your code (generally, you could right-click on the ToPagedList() method and choose the "Resolve..." option to do this automatically).

Wednesday, January 25, 2017

Telerik ASP.NET Ajax client-side framework failed to load

Telerik;0x800a139e - JavaScript runtime error: ASP.NET Ajax client-side framework failed to load;0x800a1391 - JavaScript runtime error: 'Sys' is undefined

For Internet Explorer users getting the following error:


Problem:

Recently I encountered an issue, RadScriptManager started throwing the following errors:

0x800a139e - JavaScript runtime error: ASP.NET Ajax client-side framework failed to load.

0x800a1391 - JavaScript runtime error: 'Sys' is undefined


This issue is observed on IE 11 and doesn't appear on Chrome or Firefox Browsers.

Suggested solution:


I had below in my web.config. I removed runtimeversion2.0 and it worked well.


<add name="Telerik_Web_UI_WebResource_axd" path="Telerik.Web.UI.WebResource.axd"
                 type="Telerik.Web.UI.WebResource" verb="*"
                 preCondition="integratedMode,runtimeVersionv2.0"/>  




Thursday, October 13, 2016

0 and 1 String was not recognized as a valid Boolean - Boolean Parameter in SQLDatasource

String was not recognized as a valid Boolean, System.FormatException: String was not recognized as a valid Boolean,Boolean parameter SQLDataSource,Setting up Boolean Value SQLDataSource InsertParameters

I had a SQLDataSource with insert parameters and one of which data type was set to Boolean. And I was setting the default value for insert parameters in code behind.

aspx code:
<asp:Parameter Name="bIsActive" Type="Boolean" />


I assumed that 1 or 0 would naturally be converted into Boolean when I did this:

SqlDataSource1.InsertParameters["bIsActive"].DefaultValue = c.Checked ? "1" : "0";


But instead I saw this:

String was not recognized as a valid Boolean.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.FormatException: String was not recognized as a valid Boolean.
Fig 1: String was not recognized as a valid Boolean


ASP.NET can easily convert 1 and 0 (integer types), but struggles with Strings.

Solution:


I used True and False strings instead of 0 and 1.

SqlDataSource1.InsertParameters["bIsActive"].DefaultValue = c.Checked ? "True" : "False";

Thursday, October 6, 2016

This Page Can’t Be Displayed Turn On TLS 1.0, TLS 1.1, And TLS 1.2

This Page Can’t Be Displayed Turn On TLS 1.0, TLS 1.1, And TLS 1.2,This page can't be displayed error,Cannot Access Some HTTPS Sites with Internet Explorer

For Internet Explorer users getting the following error:

This page can’t be displayed - Turn on TLS 1.0, TLS 1.1, and TLS 1.2
in Advanced settings and try connecting to https://websitename
again. If this error persists, contact your site administrator.
Change settings. 
This issue is observed on IE 11 and doesn't appear on Chrome or Firefox Browsers.


Resolution:


 Launch Internet Explorer | Tools (Alt+X on IE 11) | Options | Advanced and make sure the settings match those below: 


Wednesday, October 5, 2016

Access denied error message while saving files on C drive - Windows 7

Access denied,Access denied saving files,Windows 7 Access Denied saving files, Windows 7 Cannot save files to Drive:,open text files as administrator

I have Windows 7 machine and have set myself as the Administrator. However, when trying to modify an XML file in Notepad and save the file, it states 'Access Denied!'. It will not save the file.



Below is the solution worked for me:

Step 1. Open the notepad application in elevated mode. This can be done by right click the Notepad icon from Windows > Click 'Run as administrator'.

Step 2. You can now edit and save that file in the same folder without any issues.



If you want to open the notepad elevated every time, then right click the Notepad shortcut icon > in 'shortcut' tab click on the 'Advanced' button > tick 'run as administrator'.