none
The number align change when I send it to MS Word RRS feed

  • Question

  • I have a function that when the user press a button , opens a Word document and send  a number and date (Date value is as Number not data) to the Word Document. It works good but when I decided to Format the date value. The date alignment change. For Example the date in Access text box is as 96/02/01 (Shamsi Date) but when it was send to Word it change to 01/02/96. I used Format function but it doesn't works. The code is as follow:

    Function FnWriteToWordDoc()
        Dim objWord As Word.Application
        Dim objDoc As Word.Document
        Dim objPara As Word.Paragraph
        Dim objSelection As Word.Section
        Dim strFileName As String
        Dim strLetterInfo As String
       

        Set objWord = New Word.Application
        strFileName = Me.txtIndicatorNumber

        objWord.Visible = True
        objWord.windowState = wdWindowStateMaximize
        Set objDoc = objWord.Documents.add
        'Set the page setup properties of the document
        With objDoc.PageSetup
            .Orientation = wdOrientPortrait
            .PaperSize = wdPaperA4
        End With


        'set the selection properties

        With objWord.Selection
            .Font.Name = "Badr"
            .Font.Size = 14
        End With

        strLetterInfo = "Letter Date: " & Format(Me.txtIndicationDate, "00/00/00") & vbCrLf & "Letter Number:  " & Me.txtIndicatorNumber
        objWord.Selection.TypeText strLetterInfo
        objDoc.SaveAs "Z:\Virsa\temp\" & strFileName

    End Function

                           


    Karim Vaziri Regards,

    Friday, April 7, 2017 12:37 PM

All replies

  • I'm afraid I don't know anything about Shamsi dates, but it seems to me that if your "txtIndicationDate" is a text box on your form, and that text box is displaying the value in the format you want, you can bypass all formatting and insert into your Word document exactly the text that the control is currently displaying, by referencing its .Text property.  So you would change your code to read:

        Me.txtIndicationDate.SetFocus  ' required for access to the .Text property

        strLetterInfo = "Letter Date: " & Me.txtIndicationDate.Text & vbCrLf & "Letter Number:  " & Me.txtIndicatorNumber

    Does that come out right for you?


    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    Friday, April 7, 2017 7:13 PM
  • Dear Dirk,

    I used this code but unfortunately it didn't work. I tried it in Word and found that when I want type 96/01/22 I should type 22/01/96. Is there any function that get 960122 and change it to 220196 (sort it from end to start)?


    Karim Vaziri Regards,

    Tuesday, April 11, 2017 10:13 AM
  • I used this code but unfortunately it didn't work. I tried it in Word and found that when I want type 96/01/22 I should type 22/01/96. Is there any function that get 960122 and change it to 220196 (sort it from end to start)?

    I don't think I entirely understand what you are saying.  If the control shows the date formatted the way you want *when it has the focus*, then the code I posted ought to have inserted the exact text of the control, formatted exactly that way, into the Word document.  If it didn't, I have to conclude that Word reformatted it.  That would mean that Word recognized it as a date and changed it to match its setting for how dates should be formatted.  Is that what happened?  If you set a breakpoint in the code on the line "objWord.Selection.TypeText strLetterInfo", you can check the value of strLetterInfo by hovering your mouse pointer over the name of the variable.  Then you can see how the date was formatted.

    You can certainly take a string like "960122" and re-order its parts, using the Mid(), Left(), and Right() functions; e.g.,

        Dim strDate As String, strNewDate As String
        strDate = "960122"
        strNewDate = Right(strDate, 2) & Mid(strDate, 3, 2) & Left(strDate, 2)
        ' strNewDate is now equal to "220196"

    But I don't know where the issue is being introduced, so I don't know whether that is helpful or not.


    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    Tuesday, April 11, 2017 3:31 PM