Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-04-2022, 10:36 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 I am automating word using vba. Need Help replacing paragraph with variable information

I am attaching file in this thread.
I want to automate one paragraph in attach document. Paragraph is in front of MISC NOTES:


That paragraph is dyanamic i recieved a value and i need to paste that in place of that paragraph first i delete the existing one and paste a new one.
Attached Files
File Type: doc ROUTING TAG.doc (88.5 KB, 23 views)
Reply With Quote
  #2  
Old 02-04-2022, 11:39 AM
Charles Kenyon Charles Kenyon 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
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,463
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

Just a note that I moved your question into the vba forum.
Reply With Quote
  #3  
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,137
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
  #4  
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
  #5  
Old 02-06-2022, 02:02 AM
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,137
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

All the code I posted should be copied into the module. Rename Macro1 as BAYTAG and delete the three CALLs. Macro1, as posted, calls the two supplementary macros. They are not separate processes.
__________________
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
  #6  
Old 02-06-2022, 09:40 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

Ok Thanks Graham...Sorry for bothering. Actually what the flow is 1st we get the paragraph value from excel Attaching image of excel and want to paste the same paragraph on word first we have to delete the existing paragraph and will paste what we copy from excel. which is already attached. Please share some input and then will dig into that.
This is last. Thanks
Attached Files
File Type: docx Flow.docx (258.4 KB, 8 views)
Reply With Quote
  #7  
Old 02-06-2022, 10:39 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,137
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

If you copy the required text from Excel, the following will replace the text in the table. The table has a text box in the cell. If you don't want to keep that text box and its content, remove the marked section from the code.
Code:
Sub BayTag()
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 oShape 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.Cells(2).Range
    oRng.End = oRng.End - 1

    'If you want to keep the content of the text box.....
    If oRng.ShapeRange.Count > 0 Then
        oRng.ShapeRange(1).Select
        Set oShape = oDoc.Tables(3).Range.Cells(4).Range
        oShape.Collapse 1
        oShape.Text = vbCr
        oShape.Collapse 0
        oShape.FormattedText = Selection.FormattedText
    End If
    '..... end of text box code

    oRng.Paste
    '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
  #8  
Old 02-07-2022, 04:40 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 Graham...I had checked the code and getting below error...
I think i am doing something wrong actually i am from RPA background and want to automate this...I try all thing but all in vain.

On button click i want to extract data from excel and have to paste that data on word file which we have already a given format.
Attached Images
File Type: jpg Error.jpg (181.6 KB, 17 views)
Reply With Quote
  #9  
Old 02-07-2022, 05:26 AM
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,137
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

That is not an error message. It simply reports what is on the clipboard. Clearly you haven't copied the text from Excel as it is showing the code listing you copied from the forum.
If you want to include the collection of data from the worksheet then you may need to make some changes to:
Code:
Sub BayTag()
Dim sValue As String
    sValue = GetExcelData
    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 GetExcelData() As String
Dim strWorkbook As String: strWorkbook = "C:\Path\Job Aid Bay.xlsm" 'The path of the workbook
Dim xlApp As Object
Dim xlBook As Object
    On Error Resume Next
    Set xlApp = GetObject(, "Excel.Application")
    If Err Then
        Set xlApp = CreateObject("Excel.Application")
    End If
    On Error GoTo 0
    Set xlBook = xlApp.Workbooks.Open(FileName:=strWorkbook)
    xlApp.Visible = True
    GetExcelData = xlBook.Sheets("MAIN").Range("B12") 'the Excel cell to copy
    xlBook.Close savechanges:=False
lbl_Exit:
    Err.Clear
    Set xlBook = Nothing
    Set xlApp = Nothing
    Exit Function
End Function

Private Sub AddPara(oDoc As Document, sText As String)
Dim oRng As Range
Dim oShape 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.Cells(2).Range
    oRng.End = oRng.End - 1

    'If you want to keep the content of the text box.....
    If oRng.ShapeRange.Count > 0 Then
        oRng.ShapeRange(1).Select
        Set oShape = oDoc.Tables(3).Range.Cells(4).Range
        oShape.Collapse 1
        oShape.Text = vbCr
        oShape.Collapse 0
        oShape.FormattedText = Selection.FormattedText
    End If
    '..... end of text box code

    oRng.Text = sText
    '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
  #10  
Old 02-07-2022, 05:40 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 sir and where we keep our word document in below code. I mean where we open the word doc and paste the paragraph in below code.

Private Sub AddPara(oDoc As Document, sText As String)
Dim oRng As Range
Dim oShape 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.Cells(2).Range
oRng.End = oRng.End - 1

'If you want to keep the content of the text box.....
If oRng.ShapeRange.Count > 0 Then
oRng.ShapeRange(1).Select
Set oShape = oDoc.Tables(3).Range.Cells(4).Range
oShape.Collapse 1
oShape.Text = vbCr
oShape.Collapse 0
oShape.FormattedText = Selection.FormattedText
End If
'..... end of text box code

oRng.Text = sText
'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
Reply With Quote
  #11  
Old 02-07-2022, 07:22 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

Please check this error. Sorry please look.
Please look this is final one
Attached Images
File Type: jpg Error.jpg (118.2 KB, 18 views)

Last edited by Puneet Singh; 02-07-2022 at 09:35 AM.
Reply With Quote
  #12  
Old 02-07-2022, 10:14 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,137
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

This is a Word macro, not an Excel macro! It is run from the open document.
__________________
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
  #13  
Old 02-07-2022, 10:45 PM
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

Thankyou sir Thankyou so much its working. Will do samething for attach doc also.
in this i want to change the the paragraph. I highlight the paragraph in bold will do the same thing here also. how you find the paragraph number.
Attached Files
File Type: docx 1ST CFAS A024R2P.docx (22.8 KB, 7 views)
Reply With Quote
  #14  
Old 02-08-2022, 12:26 AM
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,137
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

Each line in that document is a separate paragraph.

There are thirty one paragraphs highlighted in bold?

There are six groups of paragraphs highlighted in bold.

What is it that you want to change them for? Do you want to change the whole lot for the content of a cell in Excel? Or do you want to change the blocks for the contents of six cells? Or do you want to replace 31 paragraphs with the content of 31 cells? Whichever it is you need to identify the cell(s).
Is the block always going to end with "*****THIS IS A COMPLETE UNDERTAKING*****"?
__________________
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
  #15  
Old 02-08-2022, 02:01 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

Whatever highlighted in bold i need to change that.
Attached Images
File Type: jpg Word.jpg (170.0 KB, 16 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 01:28 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