I’m desparately trying to copy a text inside a cell of a narrow word table including softhyphens produced by MSWord’s AutoHyphenation feature to Excel or Access.
It works, when the softhyphens are already available in the table cell.
Run the attached code in MSWord the first time after uncommenting the commented Code lines:
Option Explicit
Sub CopyWithHyphens()
Dim oExc As Object, excWB As Object, excSheet As Object
Dim myStart As Long, myEnd As Long, CopyRange As Range
Const xlPasteValues = -4163
' when uncommenting the below lines, on my system, the softhyphens aren't available in the table cell until the Sub has finished,
' obviously auto-hyphenation cannot proceed during VBA processing?????
' Dim TestTable As Table
'
''prepare table
' With ActiveDocument
' .AutoHyphenation = True
' If .Tables.Count > 0 Then .Tables(.Tables.Count).Delete
' End With
' Set TestTable = ActiveDocument.Tables.Add(Range:=Selection.Range, NumRows:=1, NumColumns:= _
' 1, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
' wdAutoFitFixed)
' With TestTable
' .ApplyStyleHeadingRows = True
' .ApplyStyleLastRow = False
' .ApplyStyleFirstColumn = True
' .ApplyStyleLastColumn = False
' .ApplyStyleRowBands = True
' .ApplyStyleColumnBands = False
' .AllowAutoFit = False
' With .Columns(1)
' .SetWidth CentimetersToPoints(1.999), wdAdjustNone
' End With
' With .Cell(1, 1).Range
' .Text = "This is a long text for demonstration of Autohypenation in a table cell"
' End With
' End With
'select string to copy from the Table,
'remove Cell-Text-End Marker (1 character)
With ActiveDocument
' .Paragraphs(1).Hyphenation = True
myStart = .Tables(1).Cell(1, 1).Range.Start
myEnd = .Tables(1).Cell(1, 1).Range.End - 1
Set CopyRange = .Range(myStart, myEnd)
End With
CopyRange.Copy 'copy the delelcted range, now including also the softhyphens within the text
'open Excel
Set oExc = CreateObject("Excel.Application")
If Not oExc Is Nothing Then
Set excWB = oExc.Workbooks.Add
Set excSheet = excWB.worksheets(1)
oExc.Visible = True
excSheet.Range("A1").PasteSpecial xlPasteValues 'paste the text of the reduced CopyRange to Excel
End If
Set oExc = Nothing
'..
End Sub
Run the code again after commenting teh previously uncommented code lines.
Running the code unommented the first time transfers the string without softhyphens, running it re-commented the second time transfers also the softhyphens, wich is the expected behaviour.
To run the procedure multiple times is very unstable when using the clipboard to transfer the data, does anybody know a method to transfer the text still with the softhyhens included?
I tried it with to store the data in an object by using range.xml
, but the softhyphens are not included; the behaviour is similar by using stringValue = range.text
.
Anybody an idea?
Grettings
Bernd