Tuesday, December 15, 2009

Formatting International Date

A quick post to help establish the numerical designator for a day, and create a International date in the form (DD(designator) MMMM, yyyy).

Code Snippet
  1. Private Function FormatInternationalDate(ByVal DateToFormat As Date) As String
  2.     Dim NumericalDesignator As String = String.Empty
  3.     Select Case Day(DateToFormat)
  4.         Case Is = 1, 21, 31
  5.             NumericalDesignator = "st"
  6.         Case Is = 2, 22
  7.             NumericalDesignator = "nd"
  8.         Case Is = 3, 23
  9.             NumericalDesignator = "rd"
  10.         Case Else
  11.             NumericalDesignator = "th"
  12.     End Select
  13.     '// Return date as 24th June, 2009
  14.     Return DateToFormat.Day.ToString & NumericalDesignator & Space(1) & DateToFormat.ToString("MMMM, yyyy")
  15. End Function

Monday, December 14, 2009

Locating a file placed in the bin directory when Build Action is set to Content

Task: How could a get a content file from the bin directory without having to hard code the directories that are created under the bin when a project is built. Using reflection I get the Executing Assembly’s Codebase (Note: You could also use Assembly.GetExecutingAssembly().Location then there’s no need for the replace in line 3)

Code Snippet
  1. Dim pathToMyFile As String = String.Empty
  2. With Assembly.GetExecutingAssembly().CodeBase
  3.     Dim tmpPath As String = .Replace("file:///", String.Empty)
  4.     pathToMyFile = tmpPath.Substring(0, tmpPath.LastIndexOf("bin") + 4)
  5.     Dim directoryList() As String = Directory.GetFiles(pathToMyFile, "MyFileName.docx", SearchOption.AllDirectories)
  6.     For Each myFile In directoryList
  7.         pathToMyFile = myFile
  8.     Next
  9. End With

Thursday, November 19, 2009

Are all Dietitian’s naturally nut jobs?

You can’t eat anything. You have to eat less. You can’t drink milk. You have to eat every 2.5 hours.  You should only eat grilled chicken, salmon or tuna in very small portions. No fast food. No sugars. No soda pop. A gallon of water per day.  People will understand if you eat during meetings.  You should walk 2 miles during your lunch break.  Are these people serious? I think I’ve heard everything now.

Tuesday, November 3, 2009

H1N1 Scare, scared but no worries here!

I’m not opposed to getting the H1N1 flu vaccine. What really scares me is having to go to the clinic to get the shot.  There’s sick people there!! Touch anything, a door, chair, pen, paper, magazine or even breathe and you could become infected, right? My wife is going today to protect our future child due in February and I am terrified she will contract something simply by going to the clinic.  Now I’ve heard the news broadcasts and I’ve listened to the crazies out there claiming this is a hoax but I’m sure there were people saying the same thing during the Spanish Flu as well and that one killed big time! I’m not going to put my life or the life of my family in danger by not getting vaccinated!

Thursday, October 22, 2009

OpenXml – Links

Here are some great resources I’ve found that have helped me with learning OpenXml.

Add images wordProcessingML

Server-Side Generation of Word 2007 Docs

Beth Massi's Blog - Loads of great code and resources

Brian Jones Website - Another great resource for OpenXml

Building Server-Side Document Generation Solutions

Creation of a Word 2007 document using Open XML Format SDK

Open DOCX using C# to extract text for search engine

DOCX - Html Preview Handler for SharePoint

Stuart Whiteford's Blog - Some good stuff here

Open XML SDK 2.0 for Microsoft Office

Eric White's Blog - Excellent resource for new learning

OpenXml – Adding a watermark to a document

Dependencies:

DocumentFormat.OpenXml.dll (version 2.0.3930.0)

Imports:

DocumentFormat.OpenXml.WordProcessing

DocumentFormat.OpenXml.Packaging

Code Snippet
  1. Private Sub CreateWatermarkedPageHeaderPart(ByRef myDocMainPart As MainDocumentPart, ByVal paragraphs As IEnumerable(Of XElement))
  2. Dim docPartObj As New DocPartObjectSdt
  3. Dim doc As New DocPartGallery() With {.Val = "Watermarks"}
  4. Dim docPartUnique As New DocPartUnique
  5. docPartObj.Append(doc, docPartUnique)
  6. Dim sdtContent As New SdtContentBlock
  7. Dim p As New Paragraph
  8. Dim pp As New ParagraphProperties
  9. Dim ps As New ParagraphStyleId With {.Val = "Header"}
  10. pp.Append(ps)
  11. Dim r As New Run
  12. Dim rPr As New RunProperties
  13. Dim no As New NoProof
  14. Dim la As New Languages With {.EastAsia = "zh-TW"}
  15. rPr.Append(no, la)
  16. Dim pict As New Picture
  17. Dim newId As String = (Guid.NewGuid.ToString("N"))
  18. Dim shap As New Shapetype With {.Id = newId, .CoordinateSize = "21600,21600", .OptionalNumber = 136, .Adjustment = "10800", .EdgePath = "m@7,l@8,m@5,21600l@6,21600e"}
  19. Dim form As New Formulas
  20. Dim fval1 As New Formula With {.Equation = "sum #0 0 10800"}
  21. Dim fval2 As New Formula With {.Equation = "prod #0 2 1"}
  22. Dim fval3 As New Formula With {.Equation = "sum 21600 0 @1"}
  23. Dim fval4 As New Formula With {.Equation = "sum 0 0 @2"}
  24. Dim fval4a As New Formula With {.Equation = "sum 21600 0 @3"}
  25. Dim fval5 As New Formula With {.Equation = "if @0 @3 0"}
  26. Dim fval6 As New Formula With {.Equation = "if @0 21600 @1"}
  27. Dim fval7 As New Formula With {.Equation = "if @0 0 @2"}
  28. Dim fval8 As New Formula With {.Equation = "if @0 @4 21600"}
  29. Dim fval9 As New Formula With {.Equation = "mid @5 @6"}
  30. Dim fval10 As New Formula With {.Equation = "mid @8 @5"}
  31. Dim fval11 As New Formula With {.Equation = "mid @7 @8"}
  32. Dim fval12 As New Formula With {.Equation = "mid @6 @7"}
  33. Dim fval13 As New Formula With {.Equation = "sum @6 0 @5"}
  34. form.Append(fval1, fval2, fval3, fval4, fval4a, fval5, fval6, fval7, fval8, fval9, fval10, fval11, fval12, fval13)
  35. Dim path As New Vml.Path With {.AllowTextPath = Vml.BooleanValues.T, .ConnectionPointType = Vml.Office.ConnectValues.Custom, .ConnectionPoints = "@9,0;@10,10800;@11,21600;@12,10800", .ConnectAngles = "270,180,90,0"}
  36. Dim txtP As New Vml.TextPath With {.On = Vml.BooleanValues.T, .FitShape = Vml.BooleanValues.T}
  37. Dim hdls As New Vml.Handles
  38. Dim hdl As New Vml.Handle With {.Position = "#0,bottomRight", .XRange = "6629,14971"}
  39. hdls.Append(hdl)
  40. shap.Append(form, path, txtP, hdls)
  41. Dim shape As New Vml.Shape With {.Id = "PowerPlusWaterMarkObject357831064", .OptionalString = "", .Type = "#" & newId, .Style = "position:absolute;margin-left:0;margin-top:0;width:412.4pt;height:247.45pt;rotation:315;z-index:-251656192;mso-position-horizontal:center;mso-position-horizontal-relative:margin;mso-position-vertical:center;mso-position-vertical-relative:margin", .AllowInCell = Vml.BooleanValues.F, .FillColor = "#7f7f7f [1612]", .Stroked = Vml.BooleanValues.F}
  42. Dim fill As New Vml.Fill With {.Opacity = ".5"}
  43. Dim shTxt As New Vml.TextPath With {.Style = "font-family:""Calibri"";font-size:1pt", .String = "DRAFT"}
  44. Dim w10 As New Vml.Wordprocessing.TextWrap With {.AnchorX = Vml.Wordprocessing.HorizontalAnchorValues.Margin, .AnchorY = Vml.Wordprocessing.VerticalAnchorValues.Margin}
  45. shape.Append(fill, shTxt, w10)
  46. pict.Append(shap, shape)
  47. r.Append(rPr, pict)
  48. p.Append(pp, r)
  49. sdtContent.Append(p)
  50. Dim sdt As New SdtBlock
  51. Dim sdtPr As New SdtProperties
  52. sdtPr.Append(docPartObj)
  53. sdt.Append(sdtPr, sdtContent)
  54. Dim head As New Header
  55. head.Append(sdt)
  56. Dim headerPart As HeaderPart = myDocMainPart.AddNewPart(Of HeaderPart)()
  57. head.Save(headerPart.GetStream())
  58. Dim relId As String = myDocMainPart.GetIdOfPart(headerPart)
  59. Dim mainPart As XDocument = myDocMainPart.GetXDocument()
  60. Dim section As XElement = mainPart.Root.Element(ns + "body").Element(ns + "sectPr")
  61. Dim headAttId As XAttribute = New XAttribute(ns_r + "id", relId)
  62. Dim headAttType As XAttribute = New XAttribute("type", "default")
  63. Dim headerElement As XElement = New XElement(ns + "headerReference", headAttId, headAttType)
  64. section.AddFirst(headerElement)
  65. 'Add draft to each section in paragraphs
  66. For Each sectionPr As XElement In paragraphs.Descendants(ns + "sectPr")
  67. sectionPr.AddFirst(headerElement)
  68. Next
  69. End Sub

Wednesday, October 21, 2009

OpenXml – Delete Header and Footer Parts

Dependencies:

DocumentFormat.OpenXml.dll (version 2.0.3930.0)

Imports:

DocumentFormat.OpenXml.WordProcessing

DocumentFormat.OpenXml.Packaging

Code Snippet
  1. Private Sub DeleteHeaderAndFooterParts(ByRef myDoc As WordprocessingDocument)
  2. Dim headRefs As List(Of HeaderReference) = myDoc.MainDocumentPart.Document.Descendants(Of HeaderReference)().ToList()
  3. For Each headRef As HeaderReference In headRefs
  4. Dim relId As String = headRef.GetAttribute("id", wns_r).Value
  5. myDoc.MainDocumentPart.DeletePart(relId)
  6. headRef.Remove()
  7. Next
  8. Dim footRefs As List(Of FooterReference) = myDoc.MainDocumentPart.Document.Descendants(Of FooterReference)().ToList()
  9. For Each footRef As FooterReference In footRefs
  10. Dim relId As String = footRef.GetAttribute("id", wns_r).Value
  11. myDoc.MainDocumentPart.DeletePart(relId)
  12. footRef.Remove()
  13. Next
  14. myDoc.MainDocumentPart.Document.Save(myDoc.MainDocumentPart.GetStream(FileMode.Create))
  15. End Sub

OpenXml – Delete Page Breaks

Dependencies:

DocumentFormat.OpenXml.dll (version 2.0.3930.0)

Imports:

DocumentFormat.OpenXml.WordProcessing

DocumentFormat.OpenXml.Packaging

Code Snippet
  1. Private Sub DeletePageBreaks(ByRef myDoc As WordprocessingDocument)
  2. Dim noBreaks As List(Of Break) = myDoc.MainDocumentPart.Document.Descendants(Of Break)().ToList()
  3. For Each brk As Break In noBreaks
  4. brk.Remove()
  5. Next
  6. myDoc.MainDocumentPart.Document.Save(myDoc.MainDocumentPart.GetStream(FileMode.Create))
  7. End Sub

OpenXml – Delete Form Protection

Dependencies:

DocumentFormat.OpenXml.dll (version 2.0.3930.0)

Imports:

DocumentFormat.OpenXml.WordProcessing

DocumentFormat.OpenXml.Packaging

Code Snippet
  1. Private Sub DeleteFormProtection(ByRef myDoc As WordprocessingDocument)
  2. Dim formProts As List(Of FormProtection) = myDoc.MainDocumentPart.Document.Descendants(Of FormProtection)().ToList()
  3. For Each formProt As FormProtection In formProts
  4. formProt.Remove()
  5. Next
  6. myDoc.MainDocumentPart.Document.Save(myDoc.MainDocumentPart.GetStream(FileMode.Create))
  7. End Sub

OpenXml – Delete Previous Section Changes

Dependencies:

DocumentFormat.OpenXml.dll (version 2.0.3930.0)

Imports:

DocumentFormat.OpenXml.WordProcessing

DocumentFormat.OpenXml.Packaging

Code Snippet
  1. Private Sub DeletePreviousSectionChanges(ByRef myDoc As WordprocessingDocument)
  2. Dim sectPrChanges As List(Of PreviousSectionProperties) = myDoc.MainDocumentPart.Document.Descendants(Of PreviousSectionProperties)().ToList()
  3. For Each sectPrChange As PreviousSectionProperties In sectPrChanges
  4. sectPrChange.Remove()
  5. Next
  6. myDoc.MainDocumentPart.Document.Save(myDoc.MainDocumentPart.GetStream(FileMode.Create))
  7. End Sub

Thursday, October 8, 2009

Apologies for no new posts

I have been busy with some extreme Open XML lately, will post some good stuff soon.

Thursday, July 2, 2009

Override WinForm's Window position

Private Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos" (ByVal hwnd As Integer, ByVal hWndInsertAfter As Integer, ByVal x As Integer, ByVal y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer
Private Const SWP_NOSIZE As Integer = &H1
'
' Will override the default position of the form and set it to whatever is specified
'

Dim hWnd As Integer = Me.Handle.ToInt32
SetWindowPos(hWnd, 0, 400, 200, 0, 0, SWP_NOSIZE)

Calling .NET From COM Originally posted by Mikec276 on C Sharp Friends It might be hard to convince your IT manager to let you build y...