(Solved) – Convert Word Doc to PDF

(solved)-–-convert-word-doc-to-pdf

I am using code that I pulled from Stack Overflow to convert a word doc to a PDF but am getting below error

Run-time error ‘5174’:

Application-defined or object-defined error

Trouble Line:

.ExportAsFixedFormat OutputFileName:="C:Users" & ws.Range("A" & i) & " - COVID" & ".pdf", ExportFormat:=wdExportFormatPDF

I have also added the reference library Microsoft Word 16.0 Object Library. So the question: How do to correct the error so I can save a word & PDF copy of the same word doc? (Word doc is saving fine)


Sub ReplaceText()

Dim wApp As Object: Set wApp = CreateObject("Word.Application")
wApp.Visible = True
Dim wDoc As Object
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Source")

Dim lr As Long, i As Long
lr = ws.Range("A" & ws.Rows.Count).End(xlUp).Row

For i = 2 To lr

    Set wDoc = wApp.Documents.Open("C:UsersCOVID.docx")

    With wDoc

        .Application.Selection.Find.Text = "<>"
        .Application.Selection.Find.Execute
        .Application.Selection = ws.Range("B" & i)
        .Application.Selection.EndOf

        .Application.Selection.Find.Text = "<>"
        .Application.Selection.Find.Execute
        .Application.Selection = ws.Range("C" & i)
        .Application.Selection.EndOf

        .Application.Selection.Find.Text = "<<DEPARTMENT>>"
        .Application.Selection.Find.Execute
        .Application.Selection = ws.Range("D" & i)
        .Application.Selection.EndOf

        .SaveAs2 Filename:=("C:Users" & ws.Range("A" & i) & " - COVID" & ".docx"), _
        FileFormat:=wdFormatXMLDocument, AddtoRecentFiles:=False
        'wdExportFormatPDF

        .ExportAsFixedFormat OutputFileName:="C:Users" & ws.Range("A" & i) & " - COVID" & ".pdf", ExportFormat:=wdExportFormatPDF


    wDoc.Close False
    Set wDoc = Nothing

    End With
Next i

End Sub
</code></pre>
</div>
<div id="comments" class="comments-area">
		<div id="respond" class="comment-respond">
		<h2 id="reply-title" class="comment-reply-title">Leave a Reply <small><a rel="nofollow" id="cancel-comment-reply-link" href="/solved-convert-word-doc-to-pdf/#respond" style="display:none;">Cancel reply</a></small></h2><form action="https://vbforfree.com/wp-comments-post.php" method="post" id="commentform" class="comment-form"><p class="comment-notes"><span id="email-notes">Your email address will not be published.</span> Required fields are marked <span class="required">*</span></p><p class="comment-form-comment"><label for="comment">Comment</label> <textarea id="comment" name="comment" cols="45" rows="8" maxlength="65525" required="required"></textarea></p><p class="comment-form-author"><label for="author">Name <span class="required">*</span></label> <input id="author" name="author" type="text" value="" size="30" maxlength="245" required='required' /></p>
<p class="comment-form-email"><label for="email">Email <span class="required">*</span></label> <input id="email" name="email" type="text" value="" size="30" maxlength="100" aria-describedby="email-notes" required='required' /></p>
<p class="comment-form-url"><label for="url">Website</label> <input id="url" name="url" type="text" value="" size="30" maxlength="200" /></p>
<p class="comment-form-cookies-consent"><input id="wp-comment-cookies-consent" name="wp-comment-cookies-consent" type="checkbox" value="yes" /> <label for="wp-comment-cookies-consent">Save my name, email, and website in this browser for the next time I comment.</label></p>
<p class="form-submit"><input name="submit" type="submit" id="submit" class="submit" value="Post Comment" /> <input type='hidden' name='comment_post_ID' value='3796' id='comment_post_ID' />
<input type='hidden' name='comment_parent' id='comment_parent' value='0' />
</p></form>	</div><!-- #respond -->
	</div>
			</article>
			<div class="nv-sidebar-wrap col-sm-12 nv-right blog-sidebar" >
		<aside id="secondary" role="complementary">
				<div id="search-2" class="widget widget_search"><form role="search" method="get" class="search-form" action="https://vbforfree.com/">
				<label>
					<span class="screen-reader-text">Search for:</span>
					<input type="search" class="search-field" placeholder="Search …" value="" name="s" />
				</label>
				<input type="submit" class="search-submit" value="Search" />
			<div class="nv-search-icon-wrap"><span class="nv-icon nv-search" >
				<svg width="15" height="15" viewBox="0 0 1792 1792" xmlns="http://www.w3.org/2000/svg"><path d="M1216 832q0-185-131.5-316.5t-316.5-131.5-316.5 131.5-131.5 316.5 131.5 316.5 316.5 131.5 316.5-131.5 131.5-316.5zm512 832q0 52-38 90t-90 38q-54 0-90-38l-343-342q-179 124-399 124-143 0-273.5-55.5t-225-150-150-225-55.5-273.5 55.5-273.5 150-225 225-150 273.5-55.5 273.5 55.5 225 150 150 225 55.5 273.5q0 220-124 399l343 343q37 37 37 90z"/></svg>
			</span></div></form></div>		<div id="recent-posts-2" class="widget widget_recent_entries">		<p class="widget-title">Recent Posts</p>		<ul>
											<li>
					<a href="https://vbforfree.com/solved-excel-copy-data-from-different-workbooks-into-1-workbook/">(Solved) – Excel copy data from different workbooks into 1 workbook</a>
									</li>
											<li>
					<a href="https://vbforfree.com/solved-the-best-way-to-copy-repeating-n-number-of-byte-in-string-array-in-vb/">(Solved) – The best way to copy repeating n number of byte in string array in vb</a>
									</li>
											<li>
					<a href="https://vbforfree.com/solved-excel-vba-countif-with-multiple-ranges/">(Solved) – Excel VBA Countif with Multiple ranges</a>
									</li>
											<li>
					<a href="https://vbforfree.com/solved-can-someone-help-excel-vb-code-generate-case-statement/">(Solved) – Can someone help Excel VB Code generate Case statement</a>
									</li>
											<li>
					<a href="https://vbforfree.com/solved-replacing-formulas-in-multiple-sheets-with-the-different-values-based-on-a-different-sheet-vba/">(Solved) – Replacing formulas in multiple sheets with the different values based on a different sheet VBA</a>
									</li>
					</ul>
		</div><div id="recent-comments-2" class="widget widget_recent_comments"><p class="widget-title">Recent Comments</p><ul id="recentcomments"></ul></div><div id="categories-2" class="widget widget_categories"><p class="widget-title">Categories</p>		<ul>
				<li class="cat-item cat-item-1"><a href="https://vbforfree.com/uncategorized/">Uncategorized</a>
</li>
	<li class="cat-item cat-item-3"><a href="https://vbforfree.com/visual-basic/">visual basic</a>
</li>
		</ul>
			</div>			</aside>
</div>
		</div>
	</div>
</main><!--/.neve-main-->


<footer class="site-footer hfg_footer " id="site-footer">
	<div class="footer--row footer-bottom layout-full-contained"
	id="cb-row--footer-bottom"
	data-row-id="bottom" data-show-on="desktop">
	<div
		class="footer--row-inner footer-bottom-inner dark-mode footer-content-wrap">
		<div class="container">
			<div
				class="hfg-grid nv-footer-content hfg-grid-bottom row--wrapper "
				data-section="hfg_footer_layout_bottom" >
				<div class="builder-item hfg-item-last hfg-item-first col-12 col-md-12 col-sm-12 hfg-item-center"><div class="item--inner builder-item--footer_copyright"
	data-section="footer_copyright"
	data-item-id="footer_copyright">
	<div class="component-wrap">
	<p><a href="https://themeisle.com/themes/neve/" rel="nofollow">Neve</a> | Powered by <a href="http://wordpress.org" rel="nofollow">WordPress</a></p></div>

	</div>

</div>							</div>
		</div>
	</div>
</div>

</footer>

</div><!--/.wrapper-->
<div id="cookie-law-info-bar"><span>This website uses cookies to improve your experience. <a class="cli_settings_button"   style="margin:5px 20px 5px 20px;" >Cookie settings</a><a  data-cli_action="accept" id="cookie_action_close_header"  class="medium cli-plugin-button cli-plugin-main-button cookie_action_close_header cli_action_button" style="display:inline-block;  margin:5px; ">ACCEPT</a></span></div><div id="cookie-law-info-again" style="display:none;"><span id="cookie_hdr_showagain">Privacy & Cookies Policy</span></div><div class="cli-modal" id="cliSettingsPopup" tabindex="-1" role="dialog" aria-labelledby="cliSettingsPopup" aria-hidden="true">
  <div class="cli-modal-dialog" role="document">
    <div class="cli-modal-content cli-bar-popup">
      <button type="button" class="cli-modal-close" id="cliModalClose">
        <svg class="" viewBox="0 0 24 24"><path d="M19 6.41l-1.41-1.41-5.59 5.59-5.59-5.59-1.41 1.41 5.59 5.59-5.59 5.59 1.41 1.41 5.59-5.59 5.59 5.59 1.41-1.41-5.59-5.59z"></path><path d="M0 0h24v24h-24z" fill="none"></path></svg>
      </button>
      <div class="cli-modal-body">
        <div class="cli-container-fluid cli-tab-container">
    <div class="cli-row">
        <div class="cli-col-12 cli-align-items-stretch cli-px-0">
            <div class="cli-privacy-overview">
                <h4>Privacy Overview</h4>                                   
                <div class="cli-privacy-content">
                    <div class="cli-privacy-content-text">This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.</div>
                </div>
                <a class="cli-privacy-readmore" data-readmore-text="Show more" data-readless-text="Show less"></a>            </div>
        </div>  
        <div class="cli-col-12 cli-align-items-stretch cli-px-0 cli-tab-section-container">
            <div class="cli-tab-section cli-privacy-tab">
                <div class="cli-tab-header">
                    <a class="cli-nav-link cli-settings-mobile" >
                        Privacy Overview                    </a>
                </div>
                <div class="cli-tab-content">
                    <div class="cli-tab-pane cli-fade">
                        <p>This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.</p>
                    </div>
                </div>

            </div>
              
                            <div class="cli-tab-section">
                    <div class="cli-tab-header">
                        <a class="cli-nav-link cli-settings-mobile" data-target="necessary" data-toggle="cli-toggle-tab" >
                            Necessary 
                        </a>
                    
                    <span class="cli-necessary-caption">Always Enabled</span>                     </div>
                    <div class="cli-tab-content">
                        <div class="cli-tab-pane cli-fade" data-id="necessary">
                            <p>Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.</p>
                        </div>
                    </div>
                </div>
              
                            <div class="cli-tab-section">
                    <div class="cli-tab-header">
                        <a class="cli-nav-link cli-settings-mobile" data-target="non-necessary" data-toggle="cli-toggle-tab" >
                            Non-necessary 
                        </a>
                    <div class="cli-switch">
                        <input type="checkbox" id="checkbox-non-necessary" class="cli-user-preference-checkbox" data-id="checkbox-non-necessary" checked />
                        <label for="checkbox-non-necessary" class="cli-slider" data-cli-enable="Enabled" data-cli-disable="Disabled">Non-necessary</label>
                    </div>                    </div>
                    <div class="cli-tab-content">
                        <div class="cli-tab-pane cli-fade" data-id="non-necessary">
                            <p>Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.</p>
                        </div>
                    </div>
                </div>
                       
        </div>
    </div> 
</div> 
      </div>
    </div>
  </div>
</div>
<div class="cli-modal-backdrop cli-fade cli-settings-overlay"></div>
<div class="cli-modal-backdrop cli-fade cli-popupbar-overlay"></div>
<script type="text/javascript">
  /* <![CDATA[ */
  cli_cookiebar_settings='{"animate_speed_hide":"500","animate_speed_show":"500","background":"#FFF","border":"#b1a6a6c2","border_on":false,"button_1_button_colour":"#000","button_1_button_hover":"#000000","button_1_link_colour":"#fff","button_1_as_button":true,"button_1_new_win":false,"button_2_button_colour":"#333","button_2_button_hover":"#292929","button_2_link_colour":"#444","button_2_as_button":false,"button_2_hidebar":false,"button_3_button_colour":"#000","button_3_button_hover":"#000000","button_3_link_colour":"#fff","button_3_as_button":true,"button_3_new_win":false,"button_4_button_colour":"#000","button_4_button_hover":"#000000","button_4_link_colour":"#62a329","button_4_as_button":false,"font_family":"inherit","header_fix":false,"notify_animate_hide":true,"notify_animate_show":false,"notify_div_id":"#cookie-law-info-bar","notify_position_horizontal":"right","notify_position_vertical":"bottom","scroll_close":false,"scroll_close_reload":false,"accept_close_reload":false,"reject_close_reload":false,"showagain_tab":true,"showagain_background":"#fff","showagain_border":"#000","showagain_div_id":"#cookie-law-info-again","showagain_x_position":"100px","text":"#000","show_once_yn":false,"show_once":"10000","logging_on":false,"as_popup":false,"popup_overlay":true,"bar_heading_text":"","cookie_bar_as":"banner","popup_showagain_position":"bottom-right","widget_position":"left"}';
  /* ]]> */
</script>
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-UA1509857901"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) --><script type='text/javascript' src='https://vbforfree.com/wp-content/plugins/otter-blocks/vendor/codeinwp/gutenberg-animation/build/animate.js?ver=5.3.2'></script>
<script type='text/javascript'>
/* <![CDATA[ */
var NeveProperties = {"ajaxurl":"https:\/\/vbforfree.com\/wp-admin\/admin-ajax.php","nonce":"93d8374648"};
/* ]]> */
</script>
<script type='text/javascript' src='https://vbforfree.com/wp-content/themes/neve/assets/js/frontend.js?ver=2.5.2'></script>
<script type='text/javascript' src='https://vbforfree.com/wp-includes/js/comment-reply.min.js?ver=5.3.2'></script>
<script type='text/javascript' src='https://vbforfree.com/wp-includes/js/wp-embed.min.js?ver=5.3.2'></script>

</body>

</html>