Results 1 to 3 of 3

Thread: Mismatch Type Error

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2018
    Posts
    2

    Mismatch Type Error

    The following code updates the bookmark BMK when the Word VBA Userform code is loaded from the activedocument

    Code:
                        
    
               With ActiveDocument
    
                        Set BMRange = ActiveDocument.Bookmarks("BMK").Range
                        BMRange.Text = "  "
                        If Len("BMK") > 0 Then ActiveDocument.Bookmarks.Add "BMK", BMRange
    
               End With


    The code below, in an excel VBA userform, opens the word application and then opens the below document. Then it sets that document as the active document.


    Code:
        
        Dim BMRange As Range
        Dim wrdApp As Word.Application
        Dim wrdDoc As Word.Document
    
        If wrdApp Is Nothing Then
        Set wrdApp = CreateObject("Word.Application")
        Set wrdDoc = wrdApp.Documents.Open("c:\Document Name.docx")
        Documents("Document Name.docx").Activate
    When I try to use first code, in an Excel VBA userform, to specify the active document the bookmark belongs to it gives me an error of mismatch type. Can someone explain to me why this is occurring? Also without restructuring the code anyone know how I can make it work?

    Thanks

  2. #2

    Thread Starter
    New Member
    Join Date
    Jan 2018
    Posts
    2

    Re: Mismatch Type Error

    Figured it out...For anyone with a similar issue.

    When a Range is altered in word from excel it must be specified as a Word.Range like below

    Code:
    Dim BMRange As Word.Range

  3. #3
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Mismatch Type Error

    The reason is that there is also an Excel.Range ... so when you're in Excel and you declare something as a "Range" it thinks you want an Excel.Range ... which is not a Word.Range... thus the type mismatch error. So, as you found out, the way to correct that is to be explicit in telling VB which Range you want to use.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width