Text Object Model and C++ -- General HowTo

Andre LeBlanc 21 Reputation points
2022-03-09T12:03:28.653+00:00

Hi There, how would I accomplish separating actions with functions e.g. render a line of text or table from one function; then later on a different function that will just write text or whatever. I have tried sending an ITextRange2* as a parameter and then using that pointer in the other function. I guess I need an explanation on how to start and end ranges ... Below is what I'm trying to do - thank you in advance for your help:
HRESULT CMHMView::AddDocumentInfo(ITextRange2* pRange, long& lDelta)
{
CRichEditCtrl& rCtrl = GetRichEditCtrl();
rCtrl.SetSel(0, -1);
rCtrl.Clear();

    float unitprice = 0.0f;
    CString priceDisplay;
    CString sDescription;

    CString sCustomerAddress = L"";
    CString sOfficeAddress = L"";

    HRESULT hr = S_FALSE;

    CComPtr<ITextRange2> ptr2;
    CComPtr<ITextFont> pFont;

    hr = pRange->GetFont(&pFont);

    if (pFont && ptr2)
    {
        CComPtr<ITextRow> pRow;
        CComPtr<ITextFont> pFontDuplicate;

        hr = pRange->GetRow(&pRow);
        hr = pFont->GetDuplicate(&pFontDuplicate);

        if (pRow && pFontDuplicate)
        {
            CComPtr<ITextPara> pPara, pParaDuplicate;

            long lStart{}, lEnd{};
            // Font
            hr = pFontDuplicate->SetName(CComBSTR(L"Courier New"));
            hr = pFontDuplicate->SetSize(12.0f);
            //Paragraph
            hr = pRange->GetPara(&pPara);
            hr = pPara->GetDuplicate(&pParaDuplicate);

            // Customer and Office Address Table (1x3)
            hr = pRow->SetCellCount(3);
            hr = pRow->SetCellIndex(0);
            hr = pRow->SetCellWidth(3600);
            hr = pRow->Insert(1);

            hr = ptr2->SetIndex(tomTable, 1, 0);
            hr = ptr2->SetText(CComBSTR(sCustomerAddress));
            hr = pParaDuplicate->SetAlignment(tomAlignLeft);
            hr = ptr2->SetFont(pFontDuplicate);
            hr = ptr2->SetPara(pParaDuplicate);
            hr = ptr2->MoveStart(tomCell, 2, &lDelta);
            hr = ptr2->SetText(CComBSTR(sOfficeAddress));

            hr = pParaDuplicate->SetAlignment(tomAlignRight);
            hr = pRange->SetFont(pFontDuplicate);
            hr = pRange->SetPara(pParaDuplicate);

            hr = pRange->EndOf(tomTable, tomMove, &lDelta);

            hr = pRange->SetText(CComBSTR(L"\n\nWe are pleased to submit our proposal for the following:\n\n"));

            hr = pRange->SetFont(pFontDuplicate);
            hr = pRange->Collapse(tomEnd);
            hr = pRange->Select();
            hr = pRange->GetEnd(&m_lEnd);
        }
    }

    return hr;
}

And the function that would call the above:

void CMHMView::AddPartsTable()
{
ITextDocument2* ptd2 = NULL;
    CRichEditCtrl& re = GetRichEditCtrl();
    IRichEditOle* pRichOle = re.GetIRichEditOle();
    HRESULT hr = pRichOle->QueryInterface(IID_PPV_ARGS(&ptd2));

    if (SUCCEEDED(hr))
    {
        CComPtr<ITextRange2> ptr2;
        CComPtr<ITextFont> pFont, pFont2;

        hr = ptd2->Range2(0, 0, &ptr2);
        hr = ptr2->GetFont(&pFont);
        hr = ptr2->GetFont(&pFont2);

        if (pFont && ptr2)
        {
            CComPtr<ITextRow> pRow, pRow2;
            CComPtr<ITextFont> pFontDuplicate, pFontDuplicate2;

            hr = ptr2->GetRow(&pRow);
            hr = pFont->GetDuplicate(&pFontDuplicate);

            if (pRow && pFontDuplicate)
            {
                CComPtr<ITextPara> pPara, pParaDuplicate;
                long lDelta{};
                long lStart{}, lEnd{};
                // Font
                hr = pFontDuplicate->SetName(CComBSTR(L"Courier New"));
                hr = pFontDuplicate->SetSize(12.0f);
                //Paragraph
                hr = ptr2->GetPara(&pPara);
                hr = pPara->GetDuplicate(&pParaDuplicate);

                               hr = AddDocumentInfo(ptr2, lDelta);

                               // Additional processing below //
                       }
        }       
    }
    pRichOle->Release();
}
Developer technologies C++
{count} votes

1 answer

Sort by: Most helpful
  1. Andre LeBlanc 21 Reputation points
    2022-03-10T15:14:30.49+00:00
    void CMHMView::InsertTableRow(long index, long& start, long& end, long ncells, 
        long cellwidths[], long skip, LPCTSTR* sText, BOOL bAppend)
    {
    
        if (SUCCEEDED(m_hr))
        {
            long lDelta{};
    
            CComPtr<ITextFont> pFont;
    
            m_hr = m_ptr2->GetFont(&pFont);
    
            CComPtr<ITextRow> pRow;
            CComPtr<ITextFont> pFontDuplicate;
    
            m_hr = m_ptr2->GetRow(&pRow);
            m_hr = pFont->GetDuplicate(&pFontDuplicate);
    
            if (pRow && pFontDuplicate)
            {
                CComPtr<ITextPara> pPara, pParaDuplicate;
                m_hr = m_ptr2->GetPara(&pPara);
                m_hr = pPara->GetDuplicate(&pParaDuplicate);
    
                m_hr = pFontDuplicate->SetName(CComBSTR(L"Courier New"));
                m_hr = pFontDuplicate->SetSize(12.0f);
    
                m_hr = m_ptr2->MoveStart(tomTable, start, &lDelta);
                m_hr = m_ptr2->Collapse(tomEnd);
                if (bAppend)
                {
                    m_hr = pRow->SetCellCount(ncells);
                    for (int i = 0; i < ncells; i++)
                    {
                        m_hr = pRow->SetCellIndex(i);
                        m_hr = pRow->SetCellWidth(cellwidths[i]);
                    }
    
                    m_hr = pRow->Insert(1);
                    m_hr = m_ptr2->SetIndex(tomTable, index, 0);
    
                    for (int i = 0; i < skip; i++)
                    {
                        if (i > 0)
                            m_hr = m_ptr2->MoveStart(tomCell, 1, &lDelta);
                        m_hr = m_ptr2->SetText(CComBSTR(L""));
                    }
                    for (int i = 0; i < ncells; i++)
                    {
                        if (i > 0)
                            m_hr = m_ptr2->MoveStart(tomCell, 1, &lDelta);
    
                        m_hr = m_ptr2->SetText(CComBSTR(sText[i]));
                        m_hr = m_ptr2->SetFont(pFontDuplicate);
                        m_hr = m_ptr2->SetPara(pParaDuplicate);
    
                    }
    
                    m_hr = m_ptr2->EndOf(tomTable, tomMove, &lDelta);
    
    
                    m_hr = m_ptr2->GetEnd(&end);
                }
                else
                {           
                    m_hr = m_ptr2->SetText(CComBSTR(L"\r"));
                    m_hr = m_ptr2->Collapse(tomEnd);
    
                    m_hr = pRow->SetCellCount(ncells);
                    for (int i = 0; i < ncells; i++)
                    {
                        m_hr = pRow->SetCellIndex(i);
                        m_hr = pRow->SetCellWidth(cellwidths[i]);
                        /*pRow->SetCellMergeFlags(tomHStartCell)*/
                    }
    
                    m_hr = pRow->Insert(1);
                    m_hr = m_ptr2->SetIndex(tomTable, index, 0);
    
                    for (int i = 0; i < ncells; i++)
                    {
                        if (i > 0)
                            m_hr = m_ptr2->MoveStart(tomCell, 1, &lDelta);
    
                        m_hr = m_ptr2->SetText(CComBSTR(sText[i]));
                        m_hr = m_ptr2->SetFont(pFontDuplicate);
                        m_hr = m_ptr2->SetPara(pParaDuplicate);
    
                    }
    
                    m_hr = m_ptr2->EndOf(tomTable, tomMove, &lDelta);
                    m_hr = m_ptr2->GetEnd(&end);
    
                }
            }
        }
        m_pREOle->Release();
    }
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.