Thursday, May 19, 2016

Move cursor to specific text in Word VBA




Sometimes it's necessary to replace placeholders (e.g. <content_placeholder1>) in a Word document with some generated or imported content using VBA macro. Just replacing the placeholder text via Selection.Find.Execute Replace:=wdReplaceAll  will not move a cursor, you need to do it as a separate step like below:

Sub moveToPlaceholder(tableName As String)

    Dim placeholderText As String

    placeholderText = "<" & tableName & ">"
    
    ' first find
    With Selection.Find
      .Text = placeholderText
      .Forward = True
      .Wrap = wdFindContinue
      .Format = False
      .MatchCase = False
      .MatchWholeWord = False
      .MatchWildcards = False
      .Execute
    End With
    
    ' now move cursor at the
    Selection.EndKey Unit:=wdLine
    
    ' now replace
    With Selection.Find
      .Text = placeholderText
      .Replacement.Text = ""
      .Forward = True
      .Wrap = wdFindContinue
      .Format = False
      .MatchCase = False
      .MatchWholeWord = False
      .MatchWildcards = False
      .Execute Replace:=wdReplaceAll
    End With
  
End Sub

Wednesday, July 16, 2014

ERROR: character with byte sequence <...> in encoding "UTF8" has no equivalent in encoding "WIN1252";

I've encountered this error "ERROR: character with byte sequence <...> in encoding "UTF8" has no equivalent in encoding "WIN1252";" while using MySQL Workbench to migrate data from PostgreSQL to MySQL. I was not sure why this was happening because the target MySQL database had UTF8 encoding and I thought that everything can be mapped to it.

The real root cause of this problem turned out to be the PostgreSQL driver I used to connect to the source DB and specified in the Workbench which was PostgreSQL ODBC Driver (ANSI) and after I changed it to PostgreSQL ODBC Driver (UNICODE) everything worked fine.

Monday, May 26, 2014

How to view .oxps (Open XPS) files on Windows 7

I didn't expect this simple thing to be so tricky, mostly because the utility name is not XpsConverter.exe but OxpsConverter.exe instead.  So here's a solution for you for when you cannot find an XpsConverter.exe:

In order to convert and view .oxps files on Windows 7 you need to use OxpsConverter.exe which should be located in C:\Windows\System32 folder, just try to run C:\Windows\System32\OxpsConverter.exe from command line.

If you don't have it, download and install a Windows update for you version from here:
http://support.microsoft.com/kb/2732059

Using the converter is very simple, just run
C:\Windows\System32\OxpsConverter.exe "file_to_convert.oxps"
and it will open a small GUI asking you where to convert the file to .xps format which can be opened by default in Windows 7.

NOTE: If you don't have then try looking for XpsConverter.exe
in %programfiles%\Windows Kits\8.0\bin\ or %programfiles(86)%\Windows Kits\8.0\bin\
if you happen to have Windows Driver Kit 8 (WDK) installed.

Wednesday, May 22, 2013

org.postgresql.util.PSQLException: ERROR: could not determine data type of parameter

I was getting this error from Jasper server when leaving datetime input control blank:


Error Message

org.postgresql.util.PSQLException: ERROR: could not determine data type of parameter $3

Error Trace

org.postgresql.util.PSQLException: ERROR: could not determine data type of parameter $3 at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2102) at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1835) at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257) at 

Where clause in the query for java.sql.Timestamp column was like this:
and (time_col > $P{inputStartCreatedDate} or $P{inputStartCreatedDate} is null)

The solution is to add explicit type conversion like this:
and (time_col > $P{inputStartCreatedDate} or $P{inputStartCreatedDate}::timestamp is null)


Thursday, February 21, 2013

Calculation type issue in iReport

I was creating a variable in iReport and requested calculation type didn't work, I was just getting the value of the field. The cause of this turned out to be the wrong variable class for the variable. I.e. java.lang.String instead of java.lang.Double.

Thursday, December 13, 2012

How to update one or several classes in jar or ear file without doing full build

Sometimes you would like to update just one or several classes in jar or ear archive which is created in some lengthy build process and you do not want to do that full build.

In this example I want to update Yourclass.class class file inside Yourjar.jar jar file and then update it inside Yourear.ear ear file. The class file is already built by your IDE in bin folder in this path:

C:\<path to project>\bin\com\<package path>\Yourclass.class

Copy your jar and ear file to some temporary work folder c:\temp.

First, we will copy the class file with its package path to some temporary folder c:\temp because jar packaging tool will not let you to specify exact target path/folder. Second, we will use jar tool to repackage.


xcopy /Y "C:\<path to project>\bin\com\<package path>\Yourclass.class" c:\temp\com\<package path>\Yourclass.class

jar uf Yourjar.jar com\<package path>\Yourclass.class

jar uf Yourear.ear Yourjar.jar

Thursday, November 8, 2012

java.lang.ClassNotFoundException when running a JUnit test


If you get this error when running a JUnit 3 or 4 test in Eclipse:

java.lang.NoClassDefFoundError: <your unit test class name>
    at java.lang.ClassLoader.defineClass1(Native Method)
 ...
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:32)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:41)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:31)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

I've tried cleaning and building projects, restarting Eclipse and other suggested things to do in this case.

It turned out that errors in the project were responsible for that error. The project has another project with JRE specified incorrectly. As soon as I fixed this, unit test worked, even though the project still had some errors.