How do I save a docx as html? - vba

I want to save a docx (Word 2007) as a html file with the accompanying files in a subfolder.
To see how to do that, I have just done that in Word 2007 and recorded a macro.
It recorded everything except the saving:
Sub Makro3()
With ActiveDocument.WebOptions
.RelyOnCSS = True
.OptimizeForBrowser = True
.OrganizeInFolder = True
.UseLongFileNames = True
.RelyOnVML = False
.AllowPNG = True
.ScreenSize = msoScreenSize800x600
.PixelsPerInch = 96
.Encoding = 65001
End With
With Application.DefaultWebOptions
.UpdateLinksOnSave = True
.CheckIfOfficeIsHTMLEditor = False
.CheckIfWordIsDefaultHTMLEditor = False
.AlwaysSaveInDefaultEncoding = False
.SaveNewWebPagesAsWebArchives = True
End With
End Sub
As I want to do the same in VB6, I re-wrote the code replacing the enum as it was unavailable in VB6 and added a SaveAs line.
However, this saved the docx obviously as a docx again, just with a different extension (.html).
What am I doing wrong?
Public Sub pCreateHtml(ByVal uPath As String)
Dim oWord As New Word.Application
Set oWord = New Word.Application
Dim oDoc As Word.Document
Set oDoc = oWord.Documents.Open(uPath, True, True)
With oDoc.WebOptions
.RelyOnCSS = True
.OptimizeForBrowser = True
.OrganizeInFolder = True
.UseLongFileNames = True
.RelyOnVML = False
.AllowPNG = True
.ScreenSize = 3 'msoScreenSize800x600
.PixelsPerInch = 96
.encoding = 65001
End With
With oDoc.Application.DefaultWebOptions
.UpdateLinksOnSave = True
.CheckIfOfficeIsHTMLEditor = False
.CheckIfWordIsDefaultHTMLEditor = False
.AlwaysSaveInDefaultEncoding = False
.SaveNewWebPagesAsWebArchives = True
End With
oDoc.SaveAs Replace(uPath, ".docx", ".html")
oDoc.Saved = True
oDoc.Close
oWord.Quit
Set oWord = Nothing
End Sub

oDoc.SaveAs2 FileName:=Replace(uPath, ".docx", ".html"), FileFormat:=wdFormatHTML
//value of wdFormatHTML is 8

Related

Error when saving individual docs from a mail merge

I Have been using a VBA code to individually save all letters separately from a mail merge into a designated folder. It has always worked previously howver with the document I am trying to do it for now it is onyl saving the first document and then coming up with an error stating:
run-time error '5825' object has been deleted
When I go to debug it highlights the line near the bottom reading 'DocResult.Close False'
How can I fix this?
Tried changing this to True or deleting line entirely but does not fix problem. Each document is quite large so takes approx 30 seconds to save
Dim WithEvents wdapp As Application
Dim bCustomProcessing As Boolean
Private Sub Document_Open()
Set wdapp = Application
bCustomProcessing = False
ThisDocument.MailMerge.DataSource.ActiveRecord = 1
ThisDocument.MailMerge.ShowWizard 1
With ActiveDocument.MailMerge
If .MainDocumentType = wdFormLetters Then
.ShowSendToCustom = "Custom Letter Processing"
End If
End With
End Sub
Private Sub wdapp_MailMergeWizardSendToCustom(ByVal Doc As Document)
bCustomProcessing = True
Doc.MailMerge.Destination = wdSendToNewDocument
With Doc.MailMerge
For rec = 1 To .DataSource.RecordCount
.DataSource.ActiveRecord = rec
.DataSource.FirstRecord = rec
.DataSource.LastRecord = rec
.Execute
Next
End With
MsgBox "Merge Finished"
End Sub
Private Sub wdapp_MailMergeAfterMerge(ByVal Doc As Document, ByVal DocResult As Document)
If bCustomProcessing = True Then
With Doc.MailMerge.DataSource.DataFields
sFirmFileName = .Item(44).Value ' First Column of the data - CHANGE
End With
DocResult.SaveAs "xxx\" & sFirmFileName & ".doc", wdFormatDocument
' Path and File Name to save. can use other formats like wdFormatPDF too
DocResult.Close False
End If
End Sub
You have to set your object as nothing like this :
Set DocResult = nothing

Print Preview without opening Excel application [VB.net]

In VB.net Can I Print Preview without show Excel application? I have looked for this in many places and haven't found the answer.
Now I use this method.
Dim XSh As Object = CreateObject("excel.application")
XSh.workbooks.open("D:\WP\formPrint.xlsx")
XSh.visible = True
For i As Integer = 0 To count
XSh.worksheets(1).range("A21").value = DataGridView1.Rows(i).Cells(0).Value
XSh.worksheets(1).range("A22").value = DataGridView1.Rows(i).Cells(1).Value
XSh.worksheets(1).range("A23").value = DataGridView1.Rows(i).Cells(2).Value
XSh.worksheets(1).range("A24").value = DataGridView1.Rows(i).Cells(3).Value
XSh.Sheets.PrintPreview()
XSh.Sheets.PrintOut()
Next i
XSh.workbooks(1).close(SaveChanges:=False)
XSh.application.Quit
and I've tried to change
XSh.visible = False
It's don't show any things. I want to show just Print Preview don't show excel application file. How Can I do?
The Print Preview is part of the Excel Application, so it will only show when you set XsH.visible = true You can also only set it to true right before you show the Print Preview and set it back to false when it's printed.
Dim XSh As Object = CreateObject("excel.application")
XSh.workbooks.open("D:\WP\formPrint.xlsx")
XSh.visible = False
For i As Integer = 0 To count
XSh.worksheets(1).range("A21").value = DataGridView1.Rows(i).Cells(0).Value
XSh.worksheets(1).range("A22").value = DataGridView1.Rows(i).Cells(1).Value
XSh.worksheets(1).range("A23").value = DataGridView1.Rows(i).Cells(2).Value
XSh.worksheets(1).range("A24").value = DataGridView1.Rows(i).Cells(3).Value
XSh.visible = True
XSh.Sheets.PrintPreview()
XSh.Sheets.PrintOut()
XSh.visible = False
Next i
XSh.workbooks(1).close(SaveChanges:=False)
XSh.application.Quit

VBScript enable editing in Excel file & save as

Set obj = CreateObject("Excel.Application")
obj.Visible = False
Set objwbk = obj.Workbooks.Open("File Link")
obj.DisplayAlerts = False
objwbk.SaveAs "C:\Data.xlsx"
Set obj1 = obj.Workbooks.Open("C:\Data.xlsx")
obj1.Visible = True
I have the above code to create a copy of the file on SharePoint. I am able to open the file but it does not make a copy because the file is opened in readonly mode. I am unable to figure out how to use the ActiveProtectedWindow.edit method here to be able to successfully achieve my objective.
Maybe something like this:
Set obj = CreateObject("Excel.Application")
obj.Visible = False
obj.DisplayAlerts = False
Set objwbk = obj.Workbooks.Open("File Link")
If objwbk.Application.ProtectedViewWindows.Count > 0 Then
objwbk.Application.ActiveProtectedViewWindow.Edit
End If
objwbk.SaveAs "C:\Data.xlsx"
Set obj1 = obj.Workbooks.Open("C:\Data.xlsx")
obj1.Visible = True

Saving multiple e-mails to pdf with PDFMAKER

I'm brand spanking new to VBA. But I've programmed a bit in SAS, just a bit in Assembler (mainframe and PC), Word Perfect (macros), a bit in Java, HTML, other stuff. What I do is, when I have a problem and I think I can program it, I look for code on the internet and adjust it to fit my needs. I have read a little bit of VBA programming. What I'm trying to do is make a macro to save a bunch of Outlook e-mail messages with PDFMAKER. I've come up with the below, so far. When I step the program, pmkr2 gets assigned type "ObjectPDFMaker" and stng gets assigned type "ISettings". So far, so good. Then I try to set stng and can't do it. I get the error "Method or data member not found." If I get rid of Set it highlights .ISettings and I get the same error. I go into F2 and the AdobePDFMakerforOffice library is there, and the class ISettings is there, but I can't seem to set stng. I'm wa-a-a-ay frustrated. Please help.
Sub ConvertToPDFWithLinks()
Dim pmkr2 As Object
Set pmkr2 = Application.COMAddIns.Item(6).Object ' Assign object reference.
Dim pdfname As String
pdfname = "C:\stuff\stuff\tester.pdf"
Dim stng As AdobePDFMakerForOffice.ISettings
Set stng = AdobePDFMakerForOffice.ISettings
stng.AddBookmarks = True
stng.AddLinks = True
stng.AddTags = True
stng.ConvertAllPages = True
stng.CreateFootnoteLinks = True
stng.CreateXrefLinks = True
stng.OutputPDFFileName = pdfname
stng.PromptForPDFFilename = False
stng.ShouldShowProgressDialog = True
stng.ViewPDFFile = False
pmkr.GetCurrentConversionSettings stng
pmkr2.CreatePDFEx stng, 0
Set pmkr2 = Nothing ' Discontinue association.
End Sub
I updated your code a little. See if this has any affect:
Sub ConvertToPDFWithLinks()
Dim pmkr2 As AdobePDFMakerForOffice.PDFMaker
'Set pmkr2 = Application.COMAddIns.Item(6).Object ' Assign object reference.
Set pmkr2 = Nothing
For Each a In Application.COMAddIns
If InStr(UCase(a.Description), "PDFMAKER") > 0 Then
Set pmkr2 = a.Object
Exit For
End If
Next
If pmkr2 Is Nothing Then
MsgBox "Cannot Find PDFMaker add-in", vbOKOnly, ""
Exit Sub
End If
Dim pdfname As String
pdfname = "C:\stuff\stuff\tester.pdf"
Dim stng As AdobePDFMakerForOffice.ISettings
pmkr2.GetCurrentConversionSettings stng
stng.AddBookmarks = True
stng.AddLinks = True
stng.AddTags = True
stng.ConvertAllPages = True
stng.CreateFootnoteLinks = True
stng.CreateXrefLinks = True
stng.OutputPDFFileName = pdfname
stng.PromptForPDFFilename = False
stng.ShouldShowProgressDialog = True
stng.ViewPDFFile = False
pmkr2.CreatePDFEx stng, 0
Set pmkr2 = Nothing ' Discontinue association.
End Sub
The main changes were in how the addin is obtained and in how stng is created.

VB - Print Word Document hidden / Visible = False

I need to print a document and have the application hidden. I am using Microsoft Word 2010.
I have tried to hide the application with the code shown below in the Yellow Comments.
objWord.Visible = False
&
objDoc.ActiveWindow.Visible = False
but the window still appear at this line:
objDoc = objWord.Documents.Open("T:\Helsingborg\A\Transport\2. Transportdrift\11. M-trp\Fraktsedlar\Växjö\Växjö Mall.doc")
And becomes hidden as soon as it gets to:
objDoc.ActiveWindow.Visible = False
I really can't find any way to solve this, since I can't use ActiveWindow.Visible = False before the document is opened.
This is my current code.
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
' Step 1
Dim objWord
objWord = CreateObject("Word.Application")
' Hidden window!
objWord.Visible = False
' Save the original printer, otherwise you will reset the system default!
Dim previousPrinter
previousPrinter = objWord.ActivePrinter
objWord.ActivePrinter = ""
' Step 2
Dim objDoc
objDoc = objWord.Documents.Open("T:\Helsingborg\A\....\Växjö Mall.doc")
objDoc.ActiveWindow.Visible = False
' Step 3 -- in this case, print out the document without any prompts
objDoc.PrintOut
' Restore the original printer
objWord.ActivePrinter = previousPrinter
' Step 4
objDoc.Close
' Step 5
objWord.Quit
MsgBox("Everything is now printed") ' Change to custom MSGBOX
End Sub
EDIT: An alternative would be if I can print the word document without opening Word.
set objDoc = GetObject(Filename)
set objWord = objDoc.Parent
in place of the Documents.Open statement
objDoc.Close False