Let me explain what I am working with WindowsXP/OfficeXP except using Access97. Within Access I have a richtext box that will allow the user to change font and bold plus a couple of other features. I have a command button for spell checking. This sends the contents of the box to Word (keeps the formatting of the text) and starts the spell checker, lets you take care of the spelling errors. Then at the end it sends the "corrected" text back to the richtext box. Then the text comes back as plain (without the formatting). That's the problem, I need the formatted text to come back and I don't have enough hair left trying to figure this out.
Most of this code came from hear and Microsoft and I really don't understand much of it. I know need to clean it up but I will take care of that when this is finished.
Any ideas?
This first set of code is on the form.
Private Declare Function OpenClipboard Lib "User32" (ByVal hwnd As Long) As Long
Private Declare Function RegisterClipboardFormat Lib "User32" Alias _
"RegisterClipboardFormatA" (ByVal lpString As String) As Long
Private Declare Function EmptyClipboard Lib "User32" () As Long
Private Declare Function CloseClipboard Lib "User32" () As Long
Private Declare Function SetClipboardData Lib "User32" ( _
ByVal wformat As Long, ByVal hMem As Long) As Long
Private Declare Function GetClipboardData Lib "User32" (ByVal wformat As _
Long) As Long
Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wflags As Long, _
ByVal dwbytes As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
ByVal destination As Long, source As Any, ByVal length As Long)
Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalFree Lib "kernel32" ( _
ByVal hMem As Long) As Long
Private Declare Function lstrcpy Lib "kernel32" (ByVal lstring1 As Any, _
ByVal lstring2 As Any) As Long
Private Const GHND = &O42
Private Const CF_TEXT = 1
Private Const MAXSIZE = 4096
Private Const GMEM_DDESHARE = &H2000
Private Const GMEM_MOVEABLE = &H2
This code is on the command button.
Private Sub cmdSplChk2_Click()
On Error GoTo Err_cmdSplChk2_Click
Dim sRTF As String
sRTF = Me!ActXCtl8
Dim Wrtf As String
Dim lSuccess As Long
Dim lRtf As Long
Dim hGlobal As Long
Dim lpString As Long
Dim lOrgTop As Long
lSuccess = OpenClipboard(Me.hwnd)
lRtf = RegisterClipboardFormat("Rich Text Format")
lSuccess = EmptyClipboard
hGlobal = GlobalAlloc(GMEM_MOVEABLE Or GMEM_DDESHARE, Len(sRTF))
lpString = GlobalLock(hGlobal)
CopyMemory lpString, ByVal sRTF, Len(sRTF)
GlobalUnlock hGlobal
SetClipboardData lRtf, hGlobal
CloseClipboard
GlobalFree hGlobal
Dim oWord As Object
Dim oDoc As Object
Set oWord = CreateObject("Word.Application")
Set oDoc = oWord.Documents.Add
oWord.Visible = True
lOrgTop = oWord.Top
oWord.WindowState = 0
oWord.Top = -3000
oWord.Selection.Paste
oDoc.Activate
oDoc.CheckSpelling
oWord.Selection.WholeStory
oWord.Selection.Copy
Dim hClipMemory As Long
Dim lpClipMemory As Long
Dim mystring As String
Dim Retval As Long
If OpenClipboard(0&) = 0 Then
MsgBox "cannot open Clipboard. Another app. may have it open"
GoTo OutofHere
End If
hClipMemory = GetClipboardData(CF_TEXT)
If IsNull(hClipMemory) Then
MsgBox "Could not allocate memory"
GoTo OutofHere
End If
lpClipMemory = GlobalLock(hClipMemory)
If Not IsNull(lpClipMemory) Then
mystring = Space$(MAXSIZE)
Retval = lstrcpy(mystring, lpClipMemory)
Retval = GlobalUnlock(hClipMemory)
mystring = Mid(mystring, 1, InStr(1, mystring, Chr$(0), 0) - 1)
Else
MsgBox "could not look to copy string from."
End If
OutofHere:
Retval = CloseClipboard()
Me!ActXCtl8 = mystring
With oWord
.ActiveDocument.Close savechanges:=False
.Quit
End With
Exit_cmdSplChk2_Click:
Exit Sub
Most of this code came from hear and Microsoft and I really don't understand much of it. I know need to clean it up but I will take care of that when this is finished.
Any ideas?
This first set of code is on the form.
Private Declare Function OpenClipboard Lib "User32" (ByVal hwnd As Long) As Long
Private Declare Function RegisterClipboardFormat Lib "User32" Alias _
"RegisterClipboardFormatA" (ByVal lpString As String) As Long
Private Declare Function EmptyClipboard Lib "User32" () As Long
Private Declare Function CloseClipboard Lib "User32" () As Long
Private Declare Function SetClipboardData Lib "User32" ( _
ByVal wformat As Long, ByVal hMem As Long) As Long
Private Declare Function GetClipboardData Lib "User32" (ByVal wformat As _
Long) As Long
Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wflags As Long, _
ByVal dwbytes As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
ByVal destination As Long, source As Any, ByVal length As Long)
Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalFree Lib "kernel32" ( _
ByVal hMem As Long) As Long
Private Declare Function lstrcpy Lib "kernel32" (ByVal lstring1 As Any, _
ByVal lstring2 As Any) As Long
Private Const GHND = &O42
Private Const CF_TEXT = 1
Private Const MAXSIZE = 4096
Private Const GMEM_DDESHARE = &H2000
Private Const GMEM_MOVEABLE = &H2
This code is on the command button.
Private Sub cmdSplChk2_Click()
On Error GoTo Err_cmdSplChk2_Click
Dim sRTF As String
sRTF = Me!ActXCtl8
Dim Wrtf As String
Dim lSuccess As Long
Dim lRtf As Long
Dim hGlobal As Long
Dim lpString As Long
Dim lOrgTop As Long
lSuccess = OpenClipboard(Me.hwnd)
lRtf = RegisterClipboardFormat("Rich Text Format")
lSuccess = EmptyClipboard
hGlobal = GlobalAlloc(GMEM_MOVEABLE Or GMEM_DDESHARE, Len(sRTF))
lpString = GlobalLock(hGlobal)
CopyMemory lpString, ByVal sRTF, Len(sRTF)
GlobalUnlock hGlobal
SetClipboardData lRtf, hGlobal
CloseClipboard
GlobalFree hGlobal
Dim oWord As Object
Dim oDoc As Object
Set oWord = CreateObject("Word.Application")
Set oDoc = oWord.Documents.Add
oWord.Visible = True
lOrgTop = oWord.Top
oWord.WindowState = 0
oWord.Top = -3000
oWord.Selection.Paste
oDoc.Activate
oDoc.CheckSpelling
oWord.Selection.WholeStory
oWord.Selection.Copy
Dim hClipMemory As Long
Dim lpClipMemory As Long
Dim mystring As String
Dim Retval As Long
If OpenClipboard(0&) = 0 Then
MsgBox "cannot open Clipboard. Another app. may have it open"
GoTo OutofHere
End If
hClipMemory = GetClipboardData(CF_TEXT)
If IsNull(hClipMemory) Then
MsgBox "Could not allocate memory"
GoTo OutofHere
End If
lpClipMemory = GlobalLock(hClipMemory)
If Not IsNull(lpClipMemory) Then
mystring = Space$(MAXSIZE)
Retval = lstrcpy(mystring, lpClipMemory)
Retval = GlobalUnlock(hClipMemory)
mystring = Mid(mystring, 1, InStr(1, mystring, Chr$(0), 0) - 1)
Else
MsgBox "could not look to copy string from."
End If
OutofHere:
Retval = CloseClipboard()
Me!ActXCtl8 = mystring
With oWord
.ActiveDocument.Close savechanges:=False
.Quit
End With
Exit_cmdSplChk2_Click:
Exit Sub