简体   繁体   中英

Type mismatch error when I store a couple paragraphs copied from Word into Excel in a string variable using vba

I am writing a function in vba that will take a cell on the worksheet's value and replace any carriage returns with another character. To populate the cell on the worksheet, I am manually copying (ctrl+c, ctrl+v) a few paragraphs from a Word document and pasting them into the formula box of excel, so it all shows up in that cell on the worksheet.

When I do this and try to store the contents of this cell as as string in my vba function, I get a Type mismatch error. What is going on here and how can I store the contents of the cell in a string variable in vba without getting this error?

I do not get a type mismatch error if I simply type in the cell itself and insert carriage returns using Alt+Enter.

Thanks for the help.

This kind of thing has been addressed numerous time on various forums, including here. See, for example:

VBA code that copies tables from multiple Word files to separate worksheets in excel Excel, naming the worksheet the name of the Word doc?

https://www.excelguru.ca/forums/showthread.php?8900-Help-with-VBA-to-extract-data-from-Word-to-Excel&p=36586&viewfull=1#post36586

For a single Word document, you might use code like:

Sub GetTableData()
'Note: this code requires a reference to the Word object model.
'See under the VBE's Tools|References.
Application.ScreenUpdating = False
Dim wdApp As New Word.Application, wdDoc As Word.Document, wdTbl As Word.Table
Dim strFile As String, WkSht As Worksheet, r As Long
Set WkSht = Activesheet
'Disable any Word Alerts
wdApp.DisplayAlerts = wdAlertsNone
'Disable any auto macros in the documents being processed
wdApp.WordBasic.DisableAutoMacros
strFile = "Filename & path"
Set wdDoc = wdApp.Documents.Open(FileName:=strFile, ReadOnly:=True, AddToRecentFiles:=False, Visible:=False)
With wdDoc
  WkSht.Name = Split(.Name, ".doc")(0)
  For Each wdTbl In .Tables
    With wdTbl.Range.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = "[^13^l]"
      .Replacement.Text = "?"
      .Forward = True
      .Wrap = wdFindStop
      .Format = False
      .MatchWildcards = True
      .Execute Replace:=wdReplaceAll
    End With
    r = WkSht.Cells(WkSht.Rows.Count, 1).End(xlUp).Row
    If r > 1 Then r = r + 2
    wdTbl.Range.Copy
    WkSht.Paste Destination:=WkSht.Range("A" & r)
  Next
  WkSht.UsedRange.Replace What:="?", Replacement:=Chr(10), LookAt:=xlPart, SearchOrder:=xlByRows
  .Close SaveChanges:=False
End With
wdApp.Quit
Set wdDoc = Nothing: Set wdApp = Nothing: Set WkSht = Nothing
Application.ScreenUpdating = True
End Sub

Note how whole tables are copied & pasted, without the circumlocution of your cell-by-cell approach. FWIW, Neither Word's paragraph breaks nor Word's manual line breaks can be copied & pasted directly into a single Excel cell; they're both interpreted as indication the end of a cell's contents by Excel.

Note also that, with the code above, you need to supply the document's path & name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM