Solved

Word VBA - HTML fragment, paste to a content control

Posted on 2011-02-17
9
891 Views
Last Modified: 2012-06-22
Following on from another q

Not my code but what happens is we have a fragment of HTML and this is placed on the clipboard using windows functions

            CopyMemory ByVal lpData, ByVal sData, Len(sData)
            GlobalUnlock hMemHandle
            EmptyClipboard
            SetClipboardData m_cfHTMLClipFormat, hMemHandle

The fragment is in sData and is still correct. It is copied to memory then SetClipboardData puts it on the clipboard.

The clipboard contents are pasted to the content control using
Selection.PasteSpecial , , , , WdPasteDataType.wdPasteHTML

The problem is that accented characters in the html fragment are getting converted to something else (UTF-8 to something else I guess)

Any idea how to fix what I am doing or another way to approach it?

TIA

I'll have to post another question I guess, but that is where I am
0
Comment
Question by:Philip Pinnell
  • 6
  • 2
9 Comments
 
LVL 10

Expert Comment

by:Horn E. Towed
ID: 34918185
I looked at your previous question and the first thing that came to mind is that the accented characters are not part of the English alphabet.

Do you have the proper language ad-on installed for the non-English characters?
0
 
LVL 13

Author Comment

by:Philip Pinnell
ID: 34924821
I can transfer the accented character from the file to word doc correctly if I leave the html tags as is So I can get <b>Telefónica</b> so there is no problem with these characters per se.

Because the code is trying to translate the html tags into Word styles by putting the fragment on the clipboard then using a Word paste as HTML it is here that the mess up is occurring.

As I say it was not written by me and it uses the Windows functions CopyMemory  and SetClipboardData  to get the fragment onto the clipboard. It seems to be in this step the problem lies.
0
 
LVL 13

Author Comment

by:Philip Pinnell
ID: 34925159
Code that puts the fragment on the cloipboard
Public Sub PutHTMLClipboard(sHtmlFragment As String, _
   Optional sContextStart As String = "<HTML><BODY>", _
   Optional sContextEnd As String = "</BODY></HTML>")
   
   Dim sData As String
   
   If RegisterCF = 0 Then Exit Sub
   
   'Add the starting and ending tags for the HTML fragment
   sContextStart = sContextStart & "<!--StartFragment -->"
   sContextEnd = "<!--EndFragment -->" & sContextEnd
   
   'Build the HTML given the description, the fragment and the context.
   'And, replace the offset place holders in the description with values
   'for the offsets of StartHMTL, EndHTML, StartFragment and EndFragment.
   sData = m_sDescription & sContextStart & sHtmlFragment & sContextEnd
   sData = Replace(sData, "aaaaaaaaaa", _
                   Format(Len(m_sDescription), "0000000000"))
   sData = Replace(sData, "bbbbbbbbbb", Format(Len(sData), "0000000000"))
   sData = Replace(sData, "cccccccccc", Format(Len(m_sDescription & _
                   sContextStart), "0000000000"))
   sData = Replace(sData, "dddddddddd", Format(Len(m_sDescription & _
                   sContextStart & sHtmlFragment), "0000000000"))

   'Add the HTML code to the clipboard
   If CBool(OpenClipboard(0)) Then
   
      Dim hMemHandle As Long, lpData As Long
      
      hMemHandle = GlobalAlloc(0, Len(sData) + 10)
      
      If CBool(hMemHandle) Then
               
         lpData = GlobalLock(hMemHandle)
         If lpData <> 0 Then
            
            CopyMemory ByVal lpData, ByVal sData, Len(sData)
            GlobalUnlock hMemHandle
            EmptyClipboard
            SetClipboardData m_cfHTMLClipFormat, hMemHandle
                        
         End If
      
      End If
   
      Call CloseClipboard
   End If

End Sub

Open in new window

0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 34925254
How can we get the data that you are using? Is it from a publicly available site?
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 13

Author Comment

by:Philip Pinnell
ID: 34925765
The data is just a text file

Attached is a cut down example.

The macro should read the text file and put the content in to a word document and 'translate' the HTML tags into Word styles. Ovbviously there could be any HTML tags.

The process works ok except that some or accented text is garbled ie Telefónica -> Telefónica

EEUpload.txt
0
 
LVL 13

Accepted Solution

by:
Philip Pinnell earned 0 total points
ID: 34961195
Part of the problem here was reading the input file

We found we needed an encoding parm

Set doc = Documents.Open(FileName:=sFile, Format:=wdOpenFormatUnicodeText, Encoding:=1252)

This still did not work for all accented characters so we moved away from using the clipboard and wrote the chunks to a temp.html file then did a

...range.insertfile("temp.html")
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 34961275
That makes more sense. I got a bit lost in your clipboard code, and didn't understand why you were using it.

Unless you need to copy formatting, you can simply set the text property of one range to that of another.

myContentControl.Range.Text = doc.Range.Text



0
 
LVL 13

Author Comment

by:Philip Pinnell
ID: 34961301
Yeah the input was HTML fragments
0
 
LVL 13

Author Closing Comment

by:Philip Pinnell
ID: 34995348
No solution found.

Workaround detailed here ie "what we did"
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now