페이지

레이블이 Programming Tips인 게시물을 표시합니다. 모든 게시물 표시
레이블이 Programming Tips인 게시물을 표시합니다. 모든 게시물 표시

2012년 10월 3일 수요일

Create Visio Macro from Visual Studio using Visual Basic

Finally I found a way to write VISIO MACRO from Visual Studio using Visual Basic.
Refer http://support.microsoft.com/kb/303872

        Dim vsoDoc As Microsoft.Office.Interop.Visio.Document
        Dim objPrj As Microsoft.Vbe.Interop.VBProject

        Dim objModule As Microsoft.Vbe.Interop.VBComponent

        Dim CR_LF As String = Chr(10) & Chr(13)

        ' Create a new Visio application
        vsoDoc = Application.Documents.Add("")

        ' Get VB project object
        objPrj = vsoDoc.VBProject

        ' SetVBComponents
        ' If VBComponents is 0, append macros to "ThisDocument"

        objModule = objPrj.VBComponents(0)

        ' Define macro
        strCode = "Private Sub Document_PageChanged(ByVal Page As IVPage)" & CR_LF & _

        "test" & CR_LF & _
        "End Sub"

        ' Write macro to ThisDocument
        objModule.CodeModule.AddFromString(strCode)

        ' SetVBComponents
        ' If VBComponents is not 0, append macros to other module (e.g., Module1)

        objModule = objPrj.VBComponents.Add(VBA.vbext_ComponentType.vbext_ct_Document)


        ' Define macro
        strCode = "sub VBAMacro()" & CR_LF  & _
"   msgbox ""VBA Macro called""" & CR_LF  & _
"end sub"

        ' Write macro to Module1


        objModule.CodeModule.AddFromString(strCode)

P.S When executing above code, I had an error, "Programmatic access to Office VBA project is denied". This error is occurred because Microsoft disables VBA project execution for security purpose. So, to resolve this error, follow guidelines provided by Microsoft.

http://support.microsoft.com/kb/282830






2012년 9월 17일 월요일

Error encountered while reading module 'Microsoft.Office.Tools.Common.v9.0

I encountered the following error when I created a Unit Test project in Visual Studio 2008 and tried to add a module to the test project.


The following error was encountered while reading module 'Microsoft.Office.Tools.Common.v9.0': Could not resolve type reference: [office]Microsoft.Office.Core.IRibbonExtensibility.


I instantly got the resolution of the problem at http://blogs.msdn.com/b/tolong/archive/2007/12/02/missing-office-microsoft-office-core-reference.aspx when  I googled.

That's easy. Just add COM library, Microsoft Office 12.0 Object Library, to the project.

But I had to struggle to work the unit test project for because I added the COM library to the unit test project instead of my project. I thought the test project needs the library because I didn't have such error when I add/modify my program...

Now, I can use the unit test project in Visual Studio 2008 after add the library my Project rather than the unit test project.

2012년 8월 30일 목요일

Visio SDK - Add Context Menu

To add user-defined features to the context menu, which is a menu shown when users click mouse right button, see the following code fragment.

Basically,
1. Define some variables


    ' Used in the marker event context string
    Public Const CONTEXT_BEGIN_MARKER As String = "/"
    Public Const CONTEXT_EVENT As String = "EVENT="
    Public Const CONTEXT_SOLUTION As String = "SOLUTION="
    Public Const SHAPE_RMA_CONNECTS_COMMAND_ID As Short = 100
    Public Const SHAPE_RMA_CONNECTS_COMMAND_ID_STR As String = "100"
    Public Const CONTEXT_SDK As String = "SDK"

    ' Caption of the right menu action item
    Public Const RMA_MENU_CAPTION As String = "&Add Attributes"

    ' Formula of the right menu action item
    Public Const RMA_MENU_FORMULA As String = "RUNADDONWARGS(""" & "QueueMarkerEvent"", """ & CONTEXT_BEGIN_MARKER & CONTEXT_EVENT & SHAPE_RMA_CONNECTS_COMMAND_ID_STR & CONTEXT_BEGIN_MARKER & CONTEXT_SOLUTION & CONTEXT_SDK & """)"


2. Attach following code to each shape

        ' It doesn't exist yet, so add the right menu
        ' action item.
        intActionRow = Shape.AddRow(Microsoft.Office.Interop.Visio.VisSectionIndices.visSectionAction, Microsoft.Office.Interop.Visio.VisRowIndices.visRowLast, Microsoft.Office.Interop.Visio.VisRowIndices.visRowAction)

        ' Set the menu caption.
        vsoCell = Shape.CellsSRC(Microsoft.Office.Interop.Visio.VisSectionIndices.visSectionAction, intActionRow, Microsoft.Office.Interop.Visio.VisCellIndices.visActionMenu)
        ' Set our caption string.
        strMenuCaption = StringToFormulaForString(RMA_MENU_CAPTION)
        vsoCell.FormulaU = strMenuCaption

        ' Set the action for the menu item.
        vsoCell = Shape.CellsSRC(Microsoft.Office.Interop.Visio.VisSectionIndices.visSectionAction, intActionRow, Microsoft.Office.Interop.Visio.VisCellIndices.visActionAction)
        vsoCell.FormulaU = RMA_MENU_FORMULA

        ' Release all objects related to this copy of the master
        ' before calling close which will delete this copy.
        ' Failure to do this can cause an exception to be thrown.
        'UPGRADE_NOTE: Object vsoCell may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
        vsoCell = Nothing



3. Add codes to Application_MarkerEvent to catch a event from the context menu and to do something


    Private Sub Application_MarkerEvent(ByVal app As Microsoft.Office.Interop.Visio.Application, ByVal SequenceNum As Integer, ByVal ContextString As String) Handles Application.MarkerEvent

        If ContextString <> "" And ContextString.Contains("/cmd=DoubleClick") Then
            Msgbox( ContextString)
        End If
    End Sub



2012년 8월 22일 수요일

Visio SDK - Set a page active

In order to set focus a certain page in Visio, use 

Application.ActiveWindow.Page = pageName

In my case, I need to active the first page of Visio whenever new page is added. Thus, following code is added on that purpose.

Application.ActiveWindow.Page = Application.Documents.Item(1).Pages.Item(1).NameU

2012년 8월 21일 화요일

Visio SDK Event Handling - Double Click

First approach...

As there is no event APIs for the Shape Double Click in Visio, I added codes to Application_BeforeShapeTextEdit.

Guessing from the event name and behavior of Visio, the API seems to be designed to process something before user edits text of the shape. As of now, the event is work well for my intent.

Although this approach is not perfect solution to get double click event, it is manageable solution.


Second approach
Close to perfect solution.

Add following code to cells of Visio shape


            queryString = "=QUEUEMARKEREVENT("" /cmd=DoubleClick /source="
            queryString = queryString & Shape.NameU & """)"
            Shape.Cells("EventDblClick").Formula = queryString


And then add codes to Application_MarkerEvent


    Private Sub Application_MarkerEvent(ByVal app As Microsoft.Office.Interop.Visio.Application, ByVal SequenceNum As Integer, ByVal ContextString As String) Handles Application.MarkerEvent
        Dim sourceTag As String
        If ContextString <> "" And ContextString.Contains("/cmd=DoubleClick") Then
            sourceTag = "/source="
            MsgBox("DoubleClick at ShapeID: " + ContextString.Substring(ContextString.IndexOf(sourceTag) + sourceTag.Length))
        End If
    End Sub

2011년 2월 22일 화요일

Visio 2007 Programming: Add master and change its name when new shape is added to a page

This code replace master name with its shape name.
Originally master is named like master.. The following code changes the name, master. to shape name such as Square, Pentagon, and etc.

Private Sub Application_ShapeAdded(ByVal Shape As Microsoft.Office.Interop.Visio.Shape) Handles Application.ShapeAdded

Dim strShapeName As String = ""
Dim nCount As Integer

Dim nMasterCnt As Integer
Dim tempMaster As Visio.Master
Dim tempMasterName As String

' Check whether a same type of shape is already added
' If a same type of shape exists, the name of newly added shape will
' ShapeName.Number. The period mark will be used to determine
' the duplication of shapes and the number represents
' the total number of shapes in the document
strShapeName = Shape.NameU

' Place newly added shape to the stencil as a master
vsoStencil.Drop(Shape, 1, 1)

' Set name for newly added master
' Get the number of masters in the stencil
nMasterCnt = vsoStencil.Masters.Count
For i = 1 To nMasterCnt
tempMaster = vsoStencil.Masters.Item(i)
tempMasterName = tempMaster.Shapes.ItemU(1).NameU
If tempMasterName = Shape.NameU Then
vsoStencil.Masters.ItemU(i).Name = Shape.NameU
End If
Next
End Sub

2011년 2월 20일 일요일

Visio 2007 Programming: Change stencil title

Imports Microsoft.Office.Interop.Visio
Public Class ThisAddIn
Private vsoDoc As Visio.Document
Private Sub ThisAddIn_Startup(ByVal sender As Object, _
ByVal e As System.EventArgs ) _
Handles Me.Startup
vsoDoc = Application.Documents.Add("")
vsoDoc = Application.Documents.OpenEx("Basic Shapes.vss", _
VisOpenSaveArgs.visOpenDocked + VisOpenSaveArgs.visOpenCopy)
End Sub

Private Sub ThisAddIn_Shutdown(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles Me.Shutdown

End Sub
End Class

Above code creates a blank document with docked basic shape stencil. However, the title of stencil window is "Stencil 2" and the number is constantly changing whenever a new document is created. So, How do I set the title with a meaningful name?

To solve this problem, add following statement after a document is created or opened.

vsoDoc.Title = "Basic Shapes"

Visio 2007 Programming: Create new document with basic shape stencil

Following skeleton codes are generated through "New Project" -> Visual Basic -> Visio Add-in.

Public Class ThisAddIn
Private Sub ThisAddIn_Startup(ByVal sender As Object, _
ByVal e As System.EventArgs ) _
Handles Me.Startup

End Sub

Private Sub ThisAddIn_Shutdown(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles Me.Shutdown

End Sub

End Class


1. Import Visio Interoperability reference before the class definition and define a variable, vsoDoc for Visio document.

Imports Microsoft.Office.Interop.Visio
Public Class ThisAddIn
Private vsoDoc As Visio.Document
Private Sub ThisAddIn_Startup(ByVal sender As Object, _
ByVal e As System.EventArgs ) _
Handles Me.Startup

End Sub

Private Sub ThisAddIn_Shutdown(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles Me.Shutdown

End Sub

2. Add codes of Visio document creation in ThisAddIn_Startup()

Imports Microsoft.Office.Interop.Visio
Public Class ThisAddIn
Private vsoDoc As Visio.Document
Private Sub ThisAddIn_Startup(ByVal sender As Object, _
ByVal e As System.EventArgs ) _
Handles Me.Startup
vsoDoc = Application.Documents.Add("")
vsoDoc = Application.Documents.OpenEx("Basic Shapes.vss", _
visOpenDocked + visOpenCopy)

End Sub

Private Sub ThisAddIn_Shutdown(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles Me.Shutdown

End Sub

3. To resolve errors with the flags visOpenDocked and visOpenCopy, append VisOpenSaveArgs to each flag. If VisOpenSaveArgs is not used, Visual Studio shows errors such as "Name 'visOpenDocked' is not declared."

Imports Microsoft.Office.Interop.Visio
Public Class ThisAddIn
Private vsoDoc As Visio.Document
Private Sub ThisAddIn_Startup(ByVal sender As Object, _
ByVal e As System.EventArgs ) _
Handles Me.Startup
vsoDoc = Application.Documents.Add("")
vsoDoc = Application.Documents.OpenEx("Basic Shapes.vss", _
VisOpenSaveArgs.visOpenDocked +
VisOpenSaveArgs.visOpenCopy)
End Sub

Private Sub ThisAddIn_Shutdown(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles Me.Shutdown

End Sub

P.S Test following codes and see the difference with the above codes

Imports Microsoft.Office.Interop.Visio
Public Class ThisAddIn
Private vsoDoc As Visio.Document
Private Sub ThisAddIn_Startup(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles Me.Startup

vsoDoc = Application.Documents.Add("")
vsoDoc = Application.Documents.add("Basic Shapes.vss")

End Sub

Private Sub ThisAddIn_Shutdown(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles Me.Shutdown

End Sub

End Class

2010년 4월 3일 토요일

POSiX + Eclipse

Porject -> Properties로 이동

c/c++ build -> GCC C Compiler -> Miscellaneous에 -lpthread option 추가.

c/c++ build -> Settings -> GCC C Linker -> Libraries에 pthread library 추가

Compile
gcc -O0 -g3 -Wall -c -fmessage-length=0 -lpthread -omain.o ..\main.c

Link
gcc -oOpenMPPC.exe main.o -lpthread
와 같이 나오면 성공

Windows + Eclipse에서 OpenMP Programming

반나절 동안 고생하여 성공한 Windows 환경에서 OpenMP Programming 방법.
IDE는 Eclipse 3.5를 사용한다.
필요한 Software

1. MinGW: http://www.mingw.org/에서 Download하지 말고 http://www.tdragon.net/recentgcc/에서 최신 Version을 Download하여 설치한다.

2. Pthread: 아마 OpenMP를 PThread를 이용하여 구현한 것 같다. http://www.mingw.org/에서 Pthread 최신 Version을 Download 받는다. 압축을 풀어 Pre−built.2 폴더로 이동한다.
Pre−built.2/include/의 모든 파일은 /include로
Pre−built.2/lib/*.dll 파일은 /bin로
Pre−built.2/lib/*.dll이외의 파일은 /lib로 복사한다.

3. gcc: Ver 4.2이후 것을 Download 받아 bin 폴더 밑의 *.dll을 /bin으로 복사한다.

4. C:\MinGW\lib\gcc\mingw32\4.4.1\libgomp.spec 파일의 내용을 다음과 같이 변경한다.
# This spec file is read by gcc when linking. It is used to specify the
# standard libraries we need in order to link with -fopenmp.
#*link_gomp: -lgomp %{static: -lpthread }
*link_gomp: -lgomp -lpthreadGC2

이 네가지 작업을 끝나면 기본적인 개발 환경은 구성되었다.

마지막으로 Eclipse를 실행하여 Proect Protperty -> C/C++ Build -> Settings로 이동하여

GCC C Compiler -> Miscellaneous와 MinGW C Linker -> Miscellaneous에 -fopenmp option을 추가하며 모든 설정이 완료된다.

2009년 5월 30일 토요일

Enable log edit

In default, SVN seems do not allow to change log. I mistakenly commint my changes without entering logs but I coubldn't change the log until I found following direction.

Error Type: Repository Has Not Been Enabled To Accept Revision Propchanges



Solution

To enable the log modification, create a file in the “hooks” folder of Subversion repository with the name of “pre-revprop-change.bat”, and then type in following scripts.

rem Only allow log messages to be changed.
if "%4" == "svn:log" exit 0
echo Property '%4' cannot be changed >&2
exit 1

This solution was suggested in the TortoiseSVN documentation.

SCM (Software Configuration Management) and SVN


SCM may be well know acronym of the Supply Chain Management. Like other IT acronym, SCM also represents Software Configuration Management. SCM is as important as software development because it reduces the burden of the mangement of development artifacts and allows to track the trends of changes. Besides these things, it gives a lot of benefits to managers and programmers.

Today, I start to use TortoiseSVN to manage my source codes. Personally I don't like downloading source files to local when checking out because it has potential to leave several different versions of a program in the different folders. This may make problems and is nothing better than to manage different versions of artifacts manually.

However, TortoiseSVN is easy to use, I spend some time to read some part of manual and provides enough functionalited compared to the commercial SCM tools. The only unsatisfactory thing is that it seems share revision numbers. For example, a.java has revision number 3. b.java is modified and its revision number is changed from 4 to 5. a.java is commited just after that, its revision number becomes 6 instead of 4.

Thus, a file could have big differene gap even if it is changed only once and this big gap may prevent to figure out how much changes are occurred in actual.

Except that, I really satisfy that. Give my many thanks to the TortoiseSVN and SVN community members.

2009년 5월 29일 금요일

Delete files or folders recursively

I got egiht version of an sample codes having .svn folders and .DS_Store files. I started to delete those two things manually but soon I got tired. So, I got on the internet and found following good tip.

Source: http://www.daniweb.com/forums/thread61479.html#

To do this, create a batch file first and then copy following command to newly created batch file.

REM Following command is desinged to delete folders
FOR /F "tokens=*" %%G IN ('DIR /B /AD /S *.svn*') DO RMDIR /S /Q %%G
REM Following command is design to delete files
FOR /F "tokens=*" %%G IN ('DIR /B /AH /S *.DS_Store*') DO RMDIR /S /Q /AH %%G

Command DIR /B /AD /S *.svn* finds all folder having .svn as its name and the the found folder names are passed to RMDIR /S /Q %%G by FOR /F ~ DO ~. It is also applied to file but need to use different option value.

Good luck to whom to refer this command some day.