Trouble pasting a Word equation into Excel

0 votes

I want to paste equations from Word into Excel 2007. The following Excel VBA code achieves this:

Sub ExpandEqn(MyText As String)

Dim appWd As Word.Application
Dim docWd As Word.Document
Dim objRange As Word.Range
Dim objEq As OMath

Set FindActiveCell = Application.ActiveCell
GetRange = CStr(FindActiveCell.Address())
ActiveCell.Offset(1, 0).Activate
NextActiveCell = CStr(FindActiveCell.Address())

Set appWd = CreateObject("Word.Application")
appWd.Visible = False
Set docWd = appWd.Documents.Add
Set objRange = docWd.Application.Selection.Range
objRange.Text = MyText
docWd.Application.Selection.OMaths.Add objRange
docWd.Application.Selection.OMaths.BuildUp
docWd.Application.Selection.WholeStory
docWd.Application.Selection.Copy

Range(NextActiveCell).Select
ActiveSheet.PasteSpecial Format:="Picture (Enhanced Metafile)",Link:=False,DisplayAsIcon:=False

appWd.Quit (False)
Set docWd = Nothing
Set appWd = Nothing

End Sub

Unfortunately, the pasted equation is the width of the Word document with lots of empty space e.g.

enter image description here

Is it possible to paste in the equation alone, excluding the space? The image can be pasted as a bitmap and works just fine, however, I require an updated metafile.

Nov 21, 2022 in Others by Kithuzzz
• 38,000 points
488 views

1 answer to this question.

0 votes

Use this:

Sub ExpandEqn(MyText As String)
    Dim appWd As Word.Application
    Dim docWd As Word.Document
    Dim objRange As Word.Range
    Dim objEq As OMath
    Dim FindActiveCell As Range
    Dim intColumnWidth As Integer
    Dim intRowHeight As Integer

    Set FindActiveCell = Application.ActiveCell
    GetRange = CStr(FindActiveCell.Address())
    ActiveCell.Offset(1, 0).Activate
    NextActiveCell = CStr(FindActiveCell.Address())

    Set appWd = CreateObject("Word.Application")
    appWd.Visible = False
    Set docWd = appWd.Documents.Add
    Set objRange = docWd.Application.Selection.Range
    objRange.Text = MyText
    docWd.Application.Selection.OMaths.Add objRange
    docWd.Application.Selection.OMaths.BuildUp
    docWd.Application.Selection.WholeStory
    docWd.Application.Selection.Copy

    ActiveCell.Offset(1, 0).Activate
    NextActiveCell = CStr(FindActiveCell.Address())
    Range(NextActiveCell).Select
    intColumnWidth = Range(NextActiveCell).ColumnWidth
    intRowHeight = Range(NextActiveCell).RowHeight
    docWd.Application.Selection.Columns.Width = intColumnWidth
    docWd.Application.Selection.Rows.Height = intRowHeight

    ActiveSheet.PasteSpecial Format:="Picture (Enhanced Metafile)", Link:=False, DisplayAsIcon:=False

I hope this helps you.

answered Nov 21, 2022 by narikkadan
• 63,600 points

Related Questions In Others

0 votes
1 answer

Excel VBA - Trouble importing WhatsApp chat history files into an Excel sheet

Since the OpenText method isn't working for ...READ MORE

answered Oct 14, 2022 in Others by narikkadan
• 63,600 points
723 views
0 votes
1 answer
0 votes
1 answer

Export a simple Dictionary into Excel file in python

You can use pandas. import pandas as pd dict1 ...READ MORE

answered Oct 23, 2022 in Others by narikkadan
• 63,600 points
7,523 views
0 votes
1 answer

How to insert a picture into Excel at a specified cell position with VBA

Try this: With xlApp.ActiveSheet.Pictures.Insert(PicPath) With ...READ MORE

answered Oct 24, 2022 in Others by narikkadan
• 63,600 points
2,491 views
0 votes
1 answer

How can I find and replace text in Word using Excel VBA?

Try this code Option Explicit Const wdReplaceAll = 2 Sub ...READ MORE

answered Oct 15, 2022 in Others by narikkadan
• 63,600 points
4,966 views
0 votes
1 answer

Multiple find and replace in MS Word from a list in MS Excel

If I understand you correctly, you want ...READ MORE

answered Oct 28, 2022 in Others by narikkadan
• 63,600 points
721 views
0 votes
1 answer

Indent table pasted from Excel into Word

It is feasible to pick up the ...READ MORE

answered Oct 30, 2022 in Others by narikkadan
• 63,600 points
549 views
0 votes
1 answer

Word Mail Merge with Excel data has to be saved in different files with custom names

Try this: Public Sub Mail_Merge() On Error GoTo ErrH Dim ...READ MORE

answered Nov 6, 2022 in Others by narikkadan
• 63,600 points
1,790 views
0 votes
1 answer

Trying to create an enclosing bookmark after pasting a picture from Excel into Word using VBA

Try this: Sub IMPORT_TO_WORD() Dim ws1 As Worksheet, ws2 ...READ MORE

answered Oct 30, 2022 in Others by narikkadan
• 63,600 points
874 views
0 votes
1 answer

Excel web query to login into a website

To be recognized by the web server ...READ MORE

answered Sep 21, 2022 in Others by narikkadan
• 63,600 points
2,945 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP