Rich Text into Word document (1 Viewer)

stu_c

Registered User.
Local time
Today, 11:47
Joined
Sep 20, 2007
Messages
489
Hi all
I use the below code (with a lot more fields) that converts from an access form into a word template using bookmarks.
I have decided to use Rich Tech in the text box so we can bold stuff but I am having issues when it converts it now as the shows all the <DIV>, <strong> tags etc how can I stop this?
Ideally what I wanted was to put something in bold in the text box to can copy this across into the word document

Code:
FUNC_CreateReportDownload
'FIND WORD FILE
    Dim oWd  As Object 'Word.Application
    Dim oDoc As Object 'Word.Document
    Dim bm   As Object 'Word.Bookmark
   
    Set oWd = CreateObject("Word.Application")
    Set oDoc = oWd.Documents.Add(CurrentProject.Path & "\TemplateFiles\HRReportTemplate.docx")

'PERSONAL FORM
   Set bm = oDoc.Bookmarks("PERSONALDETAILS")
   bm.Range.Text = [Forms]![FRM_FullCaseDetails]![HRReport]
   oWd.Visible = True
End Function
 
Last edited:

Minty

AWF VIP
Local time
Today, 11:47
Joined
Jul 26, 2013
Messages
10,371
Word doesn't use HTML/Rich Text type tags to identify text formatting unfortunately.

I tried to implement something similar recently, and in the end gave up, due to lack of time to fix the problem.
I didn't try this, but it just occurred to me while writing this, what happens if you right click and copy the Rich Text data from the form and paste it into Word via the clipboard?
 

stu_c

Registered User.
Local time
Today, 11:47
Joined
Sep 20, 2007
Messages
489
I have just tried it doing a simple CTRL+V to CTRL+P and works

Word doesn't use HTML/Rich Text type tags to identify text formatting unfortunately.

I tried to implement something similar recently, and in the end gave up, due to lack of time to fix the problem.
I didn't try this, but it just occurred to me while writing this, what happens if you right click and copy the Rich Text data from the form and paste it into Word via the clipboard?
 

Minty

AWF VIP
Local time
Today, 11:47
Joined
Jul 26, 2013
Messages
10,371
In that case you might be able to replicate that in your word code, buy copying putting the data from the form into the windows clipboard and pasting it.

Kludgey but might work?
 

stu_c

Registered User.
Local time
Today, 11:47
Joined
Sep 20, 2007
Messages
489
Hi
chow could I do that do a bookmark location on word though?
In that case you might be able to replicate that in your word code, buy copying putting the data from the form into the windows clipboard and pasting it.

Kludgey but might work?
 

Josef P.

Well-known member
Local time
Today, 12:47
Joined
Feb 2, 2023
Messages
826
You can insert a html file in Word.

Principle:
Code:
htmlFile = CreateTextFile("<html>" & AccessRichTextValue & "</html>")
WordRange.InsertFile htmlFile
 

stu_c

Registered User.
Local time
Today, 11:47
Joined
Sep 20, 2007
Messages
489
Morning Josh
Thank you for the below,
the issue is I have multiple bookmarks so not sure how this would work?

You can insert a html file in Word.

Principle:
Code:
htmlFile = CreateTextFile("<html>" & AccessRichTextValue & "</html>")
WordRange.InsertFile htmlFile
 

Josef P.

Well-known member
Local time
Today, 12:47
Joined
Feb 2, 2023
Messages
826
the issue is I have multiple bookmarks so not sure how this would work?
And trying it is too tedious?

Each bookmark has a range. This is exactly where the html file will be inserted.
Code:
Private Sub Test()

   Const TemplateName As String = "InsertHtml.dotx"
   Const HtmlCode1 As String = "<div>abc<b>xyz</b>123</div>"
   Const HtmlCode2 As String = "<div>abc<br />xyz<br />123</div>"

   Dim wdApp As Word.Application
   Dim doc As Word.Document

   Set wdApp = New Word.Application
   wdApp.Visible = True

   Set doc = wdApp.Documents.Add(CurrentProject.Path & "\" & TemplateName)

   InsertHtmlText doc.Bookmarks("Bookmark1"), HtmlCode1
   InsertHtmlText doc.Bookmarks("Bookmark2"), HtmlCode2

End Sub

Private Sub InsertHtmlText(ByVal BookmarkRef As Word.Bookmark, ByVal HtmlCode As String)

   Dim HtmlFilePath As String

   HtmlFilePath = CreateHtmlFile(HtmlCode)

   BookmarkRef.Range.InsertFile HtmlFilePath

   Kill HtmlFilePath

End Sub

Private Function CreateHtmlFile(ByVal HtmlCode As String) As String

   Dim TempHtmlFilePath As String
   Dim fso As Scripting.FileSystemObject

   Const tempFolderConst As Long = 2 ' = Scripting.SpecialFolderConst.TemporaryFolder

   If Left(Trim(HtmlCode), 5) <> "<html" Then
      HtmlCode = "<html>" & HtmlCode & "</html>"
   End If

   Set fso = New Scripting.FileSystemObject
   TempHtmlFilePath = fso.GetSpecialFolder(tempFolderConst) & "\" & fso.GetTempName & ".html"

   With fso.CreateTextFile(TempHtmlFilePath, True)
      .Write HtmlCode
      .Close
   End With

   Set fso = Nothing

   CreateHtmlFile = TempHtmlFilePath

End Function
 

Attachments

  • InsertHtml.zip
    29.1 KB · Views: 88

stu_c

Registered User.
Local time
Today, 11:47
Joined
Sep 20, 2007
Messages
489
Sorry I don't really understand :\

And trying it is too tedious?

Each bookmark has a range. This is exactly where the html file will be inserted.
Code:
Private Sub Test()

   Const TemplateName As String = "InsertHtml.dotx"
   Const HtmlCode1 As String = "<div>abc<b>xyz</b>123</div>"
   Const HtmlCode2 As String = "<div>abc<br />xyz<br />123</div>"

   Dim wdApp As Word.Application
   Dim doc As Word.Document

   Set wdApp = New Word.Application
   wdApp.Visible = True

   Set doc = wdApp.Documents.Add(CurrentProject.Path & "\" & TemplateName)

   InsertHtmlText doc.Bookmarks("Bookmark1"), HtmlCode1
   InsertHtmlText doc.Bookmarks("Bookmark2"), HtmlCode2

End Sub

Private Sub InsertHtmlText(ByVal BookmarkRef As Word.Bookmark, ByVal HtmlCode As String)

   Dim HtmlFilePath As String

   HtmlFilePath = CreateHtmlFile(HtmlCode)

   BookmarkRef.Range.InsertFile HtmlFilePath

   Kill HtmlFilePath

End Sub

Private Function CreateHtmlFile(ByVal HtmlCode As String) As String

   Dim TempHtmlFilePath As String
   Dim fso As Scripting.FileSystemObject

   Const tempFolderConst As Long = 2 ' = Scripting.SpecialFolderConst.TemporaryFolder

   If Left(Trim(HtmlCode), 5) <> "<html" Then
      HtmlCode = "<html>" & HtmlCode & "</html>"
   End If

   Set fso = New Scripting.FileSystemObject
   TempHtmlFilePath = fso.GetSpecialFolder(tempFolderConst) & "\" & fso.GetTempName & ".html"

   With fso.CreateTextFile(TempHtmlFilePath, True)
      .Write HtmlCode
      .Close
   End With

   Set fso = Nothing

   CreateHtmlFile = TempHtmlFilePath

End Function
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 11:47
Joined
Sep 12, 2006
Messages
15,656
I saw this thread.

Is there a way of turning a Rich Text string into a HTML string for inclusion in an email, so the formatting codes behave correctly.
Is this the same issue as mentioned above?

From testing a little I imagine even vbCrLf characters need changing into a HTML version.

I'll start a new thread if that's easier.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 05:47
Joined
Feb 28, 2001
Messages
27,186
If you have plain text to insert in Word, perhaps you might find this thread useful.


My picture was taken BEFORE this problem came up. I have considerably less hair now, and it is possible that Word is a contributing factor in that difference.
 

stu_c

Registered User.
Local time
Today, 11:47
Joined
Sep 20, 2007
Messages
489
Hi Doc,
I have managed to get plain text to go into a word document and insert to the correct bookmark location that isnt the issue (as shown in my first post with the code), my issue is trying to get rich text in bold ect from an access form to word as when I do it shows the <B> etc
If you have plain text to insert in Word, perhaps you might find this thread useful.


My picture was taken BEFORE this problem came up. I have considerably less hair now, and it is possible that Word is a contributing factor in that difference.
 

stu_c

Registered User.
Local time
Today, 11:47
Joined
Sep 20, 2007
Messages
489
anyone able to help further with this?
 

Users who are viewing this thread

Top Bottom