Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-04-2022, 11:38 PM
gmayor's Avatar
gmayor gmayor is offline I am automating word using vba. Need Help replacing paragraph with variable information Windows 10 I am automating word using vba. Need Help replacing paragraph with variable information Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,142
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

MISC NOTES: is in table 3 of the form. There is an empty paragraph between tables 2 and 3, which I assume is where you want the value? That being the case you can use the following macro to write the value in that paragraph. Macro1 is an example of how to use that macro to write data from the clipboard to the paragraph.


Code:
Sub Macro1()
Dim sValue As String
    sValue = GetClipBoard
    If Not sValue = "" Then
        If MsgBox("Paste the value :" & vbCr & sValue, vbYesNo) = vbYes Then
            AddPara ActiveDocument, sValue
        End If
    End If
End Sub

Private Function GetClipBoard() As String
Dim oData As DataObject
Dim testClip As String
    On Error Resume Next
    Set oData = New DataObject
    oData.GetFromClipboard
    testClip = oData.GetText
    If Err.Number > 0 Then
        MsgBox "The clipboard is empty", vbCritical
        GetClipBoard = ""
    Else
        GetClipBoard = testClip
    End If
lbl_Exit:
    Err.Clear
    Set oData = Nothing
    Exit Function
End Function

Private Sub AddPara(oDoc As Document, sText As String)
Dim oRng As Range
Dim bProtected As Boolean
    'Unprotect the file
    If oDoc.Tables.Count < 3 Then
        MsgBox "Incompatible document", vbCritical
        GoTo lbl_Exit
    End If
    If Not oDoc.ProtectionType = wdNoProtection Then
        bProtected = True
        oDoc.Unprotect Password:=""
    End If
    Set oRng = oDoc.Tables(3).Range
    With oRng
        .Start = .Previous.Paragraphs.Last.Range.Start
        .Collapse 1
        .End = oRng.Paragraphs(1).Range.End - 1
        .Text = sText
    End With
    'Reprotect the document.
    If bProtected = True Then
        oDoc.Protect _
                Type:=wdAllowOnlyFormFields, _
                NoReset:=True, _
                Password:=""
    End If
lbl_Exit:
    Set oRng = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #2  
Old 02-05-2022, 07:14 AM
Puneet Singh Puneet Singh is offline I am automating word using vba. Need Help replacing paragraph with variable information Windows XP I am automating word using vba. Need Help replacing paragraph with variable information Office 2010
Novice
I am automating word using vba. Need Help replacing paragraph with variable information
 
Join Date: Feb 2022
Posts: 13
Puneet Singh is on a distinguished road
Default

Thanks for the response
I am getting compile error and also i attach the file i want to save the paragraph in grey area.
Attached Images
File Type: jpg Compile Error.jpg (93.1 KB, 19 views)
File Type: jpg File.jpg (177.1 KB, 20 views)
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
I am automating word using vba. Need Help replacing paragraph with variable information Replacing random paragraph endings in text jp91306 Word 3 04-23-2019 03:32 PM
I want to create a bar chart of multiple variable. Then I need to draw trend lin of those variable shimulsiddiquee Excel 1 05-16-2017 07:39 AM
Replacing paragraph formatting before column break also changes the next paragraph after the break jjmartin1340 Word 3 09-21-2015 10:50 PM
Run Time Error '91': Object variable or With block variable not set using Catalogue Mailmerge Berryblue Mail Merge 1 11-13-2014 05:36 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:18 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft