I need to convert this old vba code into the new Office/Word Javascript API Add-in version.
I just can't seem to find any online documentation where Word Add-in Javascript API can access data from another document file. Is it even possible?
Private Sub Document_Open()
Dim docTemplate As Document
Dim strTemplate As String
Dim hdr1 As HeaderFooter
Dim hdr2 As HeaderFooter
Dim doc As Document
Set doc = ActiveDocument
strTemplate = "C:\wizards\LondonLetterHead.dot"
Set docTemplate = GetObject(strTemplate)
Set hdr1 = docTemplate.Sections(1).Headers(wdHeaderFooterFirstPage)
Set hdr2 = doc.Sections(1).Headers(wdHeaderFooterPrimary)
hdr2.Range.FormattedText = hdr1.Range
docTemplate.Close False
End Sub
Can the Javascript API do something like the VBA code I provided?
Related
I need to enter around 3 pieces of data into a website I don't own, press submit, and scrape a few pieces of data. I need to repeat this using different dates around 50 times. I've done this before for stuff I've reused a lot with Powershell. However, I was wondering if there was something simple like point and click I could use as if I spend hours on this I could have just as easily typed in the data and copied the results into a spreadsheet.
Any browser will do.
You can try to use IE automation using VBA to scrape the data from IE and paste it in to Excel Workbook.
Example:
Sub Automate_IE_Load_Page()
'This will load a webpage in IE
Dim i As Long
Dim URL As String
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object
'Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")
'Set IE.Visible = True to make IE visible, or False for IE to run in the background
IE.Visible = True
'Define URL
URL = "https://www.example.com"
'Navigate to URL
IE.Navigate URL
' Statusbar let's user know website is loading
Application.StatusBar = URL & " is loading. Please wait..."
' Wait while IE loading...
'IE ReadyState = 4 signifies the webpage has loaded (the first loop is set to avoid inadvertently skipping over the second loop)
Do While IE.ReadyState = 4: DoEvents: Loop 'Do While
Do Until IE.ReadyState = 4: DoEvents: Loop 'Do Until
'Webpage Loaded
Application.StatusBar = URL & " Loaded"
'Unload IE
Set IE = Nothing
Set objElement = Nothing
Set objCollection = Nothing
End Sub
References:
(1) Automate Internet Explorer (IE) Using VBA
(2) VBA Web Scraping with GetElementsByTagName
(3) IE (Internet Explorer) Automation using Excel VBA
Further, You can try to modify the code based on your requirement.
I have following problem, i can not create a simple chart with PowerPoint:
Sub CreateChart()
Dim myChart As Chart
Dim gChartData As ChartData
Dim gWorkBook As Excel.Workbook
Dim gWorkSheet As Excel.Worksheet
' Create the chart and set a reference to the chart data.
Set myChart = ActivePresentation.Slides(1).Shapes.AddChart.Chart
Set gChartData = myChart.ChartData
' Set the Workbook and Worksheet references.
Set gWorkBook = gChartData.Workbook
Set gWorkSheet = gWorkBook.Worksheets(1)
In Line Set myChart = ActivePresentation.Slides(1) etc. following error:
Run-time error '429': ActiveX component can't create object.
in line Set gChartData = myChart. if i press CtrL + space, i don't see the paramather ChartData, just ChartArea.
Something is not working appropiately. The references that i have are:
Visual Basic For Applications
Microsoft Excel 14.0 Object Library
OLE Automation
Microsofot Office 14.0 Object Library
Microsoft PowerPoint 14.0 Object Library
Microsoft Feeds
Microsoft Forms 2.0 Object Library
The code was copied from the website from microsoft:
https://msdn.microsoft.com/en-us/library/office/ff973127%28v=office.14%29.aspx
How can it be possible that i use some code from microsoft and it doesn't work...? very frustrating.
Regards
I am trying to create an appointment/meeting notice template that prevents forwarding, defaults to no response required and is sent from a delegated/shared calendar. I have the script to change the response required and can disable the forwarding option in the actions, but I can't figure out the delegate. I've found the getname script:
Sub ResolveName()
Dim myNamespace As Outlook.NameSpace
Dim myRecipient As Outlook.Recipient
Dim CalendarFolder As Outlook.Folder
Set myNamespace = Application.GetNamespace("MAPI")
Set myRecipient = myNamespace.CreateRecipient("Larry M Garrett")
myRecipient.Resolve
If myRecipient.Resolved Then
Call ShowCalendar(myNamespace, myRecipient)
End If
End Sub
Sub ShowCalendar(myNamespace, myRecipient)
Dim CalendarFolder As Outlook.Folder
Set CalendarFolder = _
myNamespace.GetSharedDefaultFolder _
(myRecipient, olFolderCalendar)
CalendarFolder.Display
End Sub
This works in visual basic, but it only opens the shared calendar. It doesn't set the appointment to come from the shared calendar. I get a code error if i put it into the "view code" section and try to run the form. I feel like i'm missing the step where i tell it to send from the delegated calendar but i can't find it. Thanks.
It looks like you need to use the shared calendar folder to create an appointment item. Use the sharedCalendarFolder.Items.Add method to create an appointment.
I have "File-Stream" enabled database. I have created table with column that has the "file-stream" attribute and manage to record successfully row in the table. So, what I have got is image stored in my "File-Stream" as BLOB.
What is my problem?
I have to get this image and show it in the web browser using classic asp (I am total newbie in this server-language and I am not allowed to use the asp.net). I have searched and read a lot (there are many information about doing that with asp.net and almost nothing showing how to do this with classic asp) and found a article (http://www.simple-talk.com/sql/learn-sql-server/an-introduction-to-sql-server-filestream/) that shows to ways to read the data:
"Accessing FILESTREAM Data using TSQL" and "Accessing FILESTREAM data with Managed API"
The first one I have been able to understand and use. The second one (there is a example with vb.net code) I haven't been able.
This is the code:
‘Create a connection to the database
Dim ConStr As String
ConStr = "Data Source=JACOBXPS\katmaifs;Initial Catalog=NorthPole" & _
";Integrated Security=True"
Dim con As New SqlConnection(ConStr)
con.Open()
'Retrieve the FilePath() of the image file
Dim sqlCommand As New SqlCommand()
sqlCommand.Connection = con
sqlCommand.CommandText = "SELECT ItemImage.PathName() AS PathName " + _
"FROM items WHERE ItemNumber = 'MS1001'"
Dim filePath As String = sqlCommand.ExecuteScalar()
'Obtain a Transaction Context
Dim transaction As SqlTransaction = con.BeginTransaction("ItemTran")
sqlCommand.Transaction = transaction
sqlCommand.CommandText = "SELECT GET_FILESTREAM_TRANSACTION_CONTEXT()"
Dim txContext As Byte() = sqlCommand.ExecuteScalar()
' Open and read file using SqlFileStream Class
Dim sqlFileStream As New SqlFileStream(filePath, txContext, FileAccess.Read)
Dim buffer As Byte() = New Byte(sqlFileStream.Length) {}
sqlFileStream.Read(buffer, 0, buffer.Length)
'Bind the image data to an image control
Dim ms As MemoryStream = New MemoryStream(buffer)
Dim bmp As Bitmap = New Bitmap(ms)
ItemImage.Image = bmp
'Cleanup
sqlFileStream.Close()
sqlCommand.Transaction.Commit()
con.Close()
I was not able to transform this in classic asp but in the same article I have read something more frustrating:
When accessing FILESTREAM data using TSQL, SQL Server reads the content of the >FILESTREAM data file and serves it to the client. SQL Server memory is used for reading >the content of the data file. Accessing FILESTREAM data using Win32 Streaming does not >use SQL Server memory. In addition it allows the application to take advantage of the >Streaming capabilities of the NT File System.
So what is my real problem?
Can be the vb.net code transform and used in a classic asp?And if it can, does that mean that when I am using the "file-stream" enable futures and want to display data in the web it will be more slow then using desktop application?
I am pretty confused and will appreciate any answer or link with article to read.
A little confused on the question but if all you're trying to do is display some BLOB data from the database as an Image on the webpage then you should have an "image.asp" page that looks something like this ...
SQL = "SELECT FILE_NAME,IMAGE_FILE FROM tblImages WHERE ID = " & request("id")
Set rs =db.Execute( SQL )
if not(rs.eof) then
Response.ContentType = "application/octet-stream"
Response.AddHeader "Content-Disposition", "attachment;filename=" & rs("FILE_NAME")
Response.BinaryWrite rs("IMAGE_FILE")
else
response.Write("No such record found in the database at row " & request("id"))
end if
Is there a way to add an attachment to an email you're sending out without the attachment being on the filesystem? From reading over the DOM (http://msdn.microsoft.com/en-us/library/bb175153%28office.12%29.aspx) it says that the attachment source can be either a file path or "an Outlook item that constitutes the attachment". I don't use VBA of the office DOMs all that much and I'm not sure what this is. In addition, all the examples I can find are only giving usage examples by using the filesystem path.
I'm calling this from Word for documents that create themselves by filling out some form fields, then print themselves. I'd like them to go out via E-mail as well, but have no need for a permanent copy of the created file. I do realize that I could save them off to a temp directory, attach the saved file, then delete the file once the mail object is sent. It seems a waste to do this though.
Is there an way I can have Word pass the in-memory Document object off to Outlook to attach to the email?
The answer is no, you cannot attach an in-memory document to an outlook mailitem without saving it to disk first.
Oesor,
I made an assumption that email should get sent automatically, so SendMail method is out.
I tried a couple of things to see whether it would work. In both cases code would be embedded in your Word file.
In pre-2007 Word you could use RoutingSlip functionality:
ActiveDocument.HasRoutingSlip = True 'Creates RoutingSlip
With ActiveDocument.RoutingSlip
.Subject = "email Subject"
.AddRecipient = "recipient#domain.com"
.Delivery = wdAllAtOnce
End With
ActiveDocument.Route
Apparently, this code doesn't work in Word 2007. So instead you can use SendForReview functionality:
ActiveDocument.SendForReview "recipient#domain.com", "email subject", False, True
Email gets sent right away (w/o the popup Outlook window), but a couple of caveats exist: the document has to have a corresponding file - it won't work for a new document that has never been saved, and the first time the recipient opens the attached document from e-mail there may be a popup message about starting the review process.
I hope this helps,
Max.
This should work for you, if the relevant attachment source is an inline item, such as an embedded image. I have not tried it with attached files that are not inline but it may work there too. The basic ideas are:
1) Treat the content of the Emails as a Word document because the native Editor for Outlook is Word.
2) Use Word's Copy and Paste to carry everything around with you via the Clipboard because it is a well tested approach. In the example, I have pasted the new section in at the start in a new Paragraph but you could obviously place it anywhere you want.
The strange thing is, though, (see the Debug.Print) that the Attachments Count in the To document does not change, even though the inline images are all where they should be and can be seen and Sent. Have Outlook fun! (The .olm files in the example are simply Outlook.MailItems that have been saved as Template files. They could just as easily be MailItems from an Outlook folder.)
Private Sub TestAttach()
'Places inline Attachment information into a different MailItem
Dim OlTo As Outlook.MailItem
Dim OlFrom As Outlook.MailItem
Dim DocTo As Word.Document
Dim DocFrom As Word.Document
Dim R As Word.Range
Dim R1 As Word.Range
Dim R2 As Word.Range
Dim lStart As Long
Dim lEnd As Long
Set OlFrom = Outlook.CreateItemFromTemplate("C:\Temp\OlTemplateWithSomeOtherAttachments.oft")
Set OlTo = Outlook.CreateItemFromTemplate("C:\Temp\OlTemplateWithSomeAttachments.oft")
Debug.Print "From file starts with " & OlFrom.Attachments.Count & " attachments."
Debug.Print "To file starts with " & OlTo.Attachments.Count & " attachments."
Set DocFrom = OlFrom.GetInspector.WordEditor
Set DocTo = OlTo.GetInspector.WordEditor
OlFrom.Display
OlTo.Display
Set R2 = DocFrom.Content
With R2.Find 'Note: Find settings are 'sticky' and do not need to be repeated on the next find.
.Forward = True
.Wrap = wdFindStop 'Do not loop back to the start of the document
.Format = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Text = "Start flag for Section with Attachments" 'Find the start of the section to move
.Execute
lStart = R2.Start
.Text = "End flag for Section with Attachments" 'Find the end of the section to move
R2.Collapse wdCollapseEnd
.Execute
lEnd = R2.Start
End With
'OlFrom.Display
Set R2 = DocFrom.Range(lStart, lEnd)
'R2.Select
R2.Copy
Set R = DocTo.Range(1, 1)
R.InsertParagraphBefore
'Place the new inline attachments in the To MailItem
Set R = DocTo.Range(1, 1)
R.Paste
OlTo.Display
Debug.Print OlTo.Attachments.Count; "To file ends with " & OlTo.Attachments.Count & " attachments, the same as the original number but all the inline images show."
End Sub